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 andn).
Algorithm:
- Initialize a sum variable to 0.
- Start a loop with
iinitialized to 2 and incrementing by 1 in each iteration. - Inside the loop, check if
nis divisible byi.- If it is, add
ito the sum. - Calculate
j, which isndivided byi. - If
i != j, addjto the sum as well.
- If it is, add
- If
i * iexceedsn, the loop ends. - Return the sum as the result.
Function main
- Line 10: Main execution begins with the
mainfunction.
Part 1: Testing chowla for 1 to 37
- Line 12: Uses the loop
for n in 1 .. 37to test thechowlafunction for values ofnfrom 1 to 37. - Line 13: Prints the result of
chowla(n)for each value ofn.
Part 2: Counting Primes Using chowla
- Line 17: Initializes
countto 0 andpowerto 100. - Line 19: Uses the loop
for n in 2 .. 10000000to 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 thatnis prime. - Line 21: Increments the
countifnis 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
countto 0 and sets the limit to 350000000. - Line 29: Initializes variables
kandkkto 2 and 3, respectively. - Line 30: Enters an infinite loop using
loop do. - Line 31: Calculates
pas the product ofkandkk. - Line 32: Checks if
pexceeds the limit. - Line 34: Checks if
chowla(p) == p - 1, indicating thatpis a perfect number. - Line 35: If
pis perfect, prints it and increments thecount. - Line 37: Updates
kandkkfor the next iteration. - Line 41: Breaks out of the loop when
pexceeds 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 Scope/Function names and labels step by step in the Ruby programming language
You may also check:How to resolve the algorithm HTTP step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Ruby programming language
You may also check:How to resolve the algorithm Substitution cipher step by step in the Ruby programming language