How to resolve the algorithm Chowla numbers step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Chowla numbers step by step in the Ruby programming language

Table of Contents

Problem Statement

Chowla numbers are also known as:

The chowla number of   n   is   (as defined by Chowla's function):

The sequence is named after   Sarvadaman D. S. Chowla,   (22 October 1907 ──► 10 December 1995), a London born Indian American mathematician specializing in number theory.

German mathematician Carl Friedrich Gauss (1777─1855) said:

Chowla numbers can also be expressed as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Chowla numbers step by step in the Ruby programming language

Function chowla

  • Input: An integer n.
  • Purpose: Calculates the sum of the proper divisors of n (excluding 1 and n).

Algorithm:

  1. Initialize a sum variable to 0.
  2. Start a loop with i initialized to 2 and incrementing by 1 in each iteration.
  3. Inside the loop, check if n is divisible by i.
    • If it is, add i to the sum.
    • Calculate j, which is n divided by i.
    • If i != j, add j to the sum as well.
  4. If i * i exceeds n, the loop ends.
  5. Return the sum as the result.

Function main

  • Line 10: Main execution begins with the main function.

Part 1: Testing chowla for 1 to 37

  • Line 12: Uses the loop for n in 1 .. 37 to test the chowla function for values of n from 1 to 37.
  • Line 13: Prints the result of chowla(n) for each value of n.

Part 2: Counting Primes Using chowla

  • Line 17: Initializes count to 0 and power to 100.
  • Line 19: Uses the loop for n in 2 .. 10000000 to calculate the number of primes up to 10^7.
  • Line 20: Checks if chowla(n) equals 0, indicating the absence of proper divisors, which implies that n is prime.
  • Line 21: Increments the count if n is a prime.
  • Line 23: Periodically prints the number of primes found up to certain powers of 10 (100, 1000, 10000, etc.).

Part 3: Finding Perfect Numbers Using chowla

  • Line 27: Initializes count to 0 and sets the limit to 350000000.
  • Line 29: Initializes variables k and kk to 2 and 3, respectively.
  • Line 30: Enters an infinite loop using loop do.
  • Line 31: Calculates p as the product of k and kk.
  • Line 32: Checks if p exceeds the limit.
  • Line 34: Checks if chowla(p) == p - 1, indicating that p is a perfect number.
  • Line 35: If p is perfect, prints it and increments the count.
  • Line 37: Updates k and kk for the next iteration.
  • Line 41: Breaks out of the loop when p exceeds the limit.
  • Line 42: Prints the count of perfect numbers found up to the limit.

Source code in the ruby programming language

def chowla(n)
    sum = 0
    i = 2
    while i * i <= n do
        if n % i == 0 then
            sum = sum + i
            j = n / i
            if i != j then
                sum = sum + j
            end
        end
        i = i + 1
    end
    return sum
end

def main
    for n in 1 .. 37 do
        puts "chowla(%d) = %d" % [n, chowla(n)]
    end

    count = 0
    power = 100
    for n in 2 .. 10000000 do
        if chowla(n) == 0 then
            count = count + 1
        end
        if n % power == 0 then
            puts "There are %d primes < %d" % [count, power]
            power = power * 10
        end
    end

    count = 0
    limit = 350000000
    k = 2
    kk = 3
    loop do
        p = k * kk
        if p > limit then
            break
        end
        if chowla(p) == p - 1 then
            puts "%d is a perfect number" % [p]
            count = count + 1
        end
        k = kk + 1
        kk = kk + k
    end
    puts "There are %d perfect numbers < %d" % [count, limit]
end

main()


  

You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the jq programming language
You may also check:How to resolve the algorithm Sierpinski pentagon step by step in the D programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the RPL programming language
You may also check:How to resolve the algorithm Wireworld step by step in the Go programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the FutureBasic programming language