How to resolve the algorithm Chowla numbers step by step in the Go programming language
How to resolve the algorithm Chowla numbers step by step in the Go 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 Go programming language
Understanding the Code Structure
The code is divided into four main functions:
chowla
: Calculates the Chowla sum of a given integer.sieve
: Uses the Sieve of Chowla to generate a list of composite numbers.commatize
: Formats a large integer with thousand separators (commas).main
: Runs the program.
Chowla Numbers (Function: chowla
)
The chowla
function calculates the Chowla sum of an integer n
. The Chowla sum is the sum of all distinct divisors of n
. For example, the Chowla sum of 12 is 28 (1 + 2 + 3 + 4 + 6 + 12).
The function loops through all integers from 2 to the square root of n
, checking if n
is divisible by each integer. If it is, it adds the corresponding divisors to the sum.
Sieve of Chowla (Function: sieve
)
The sieve
function uses the Sieve of Chowla to generate a list of composite numbers. It creates a boolean array c
, where true
indicates a composite number and false
indicates a prime number. The sieve starts with odd numbers greater than or equal to 3.
For each odd number i
that is not composite (i.e., c[i] == false
), the function checks if chowla(i) == 0
. If this condition is true, it means i
is a "Chowla number."
Chowla numbers have an interesting property: If i
is a Chowla number, then all multiples of i
(except for i
itself) are composite. Therefore, the function marks all multiples of i
as composite in the c
array.
Commatizing Numbers (Function: commatize
)
The commatize
function converts a large integer into a string with thousand separators (commas).
Main Program (Function: main
)
The main
function is the entry point of the program. It does the following:
- Prints the Chowla sums for integers from 1 to 37.
- Uses the
sieve
function to generate a list of composite numbers up to a limit of 10 million. - Displays the count of prime numbers up to various powers of 10.
- Searches for perfect numbers up to a limit of 35 million. A perfect number is a number that is equal to the sum of its proper divisors (divisors excluding the number itself). The function uses the Chowla sum to identify perfect numbers.
Source code in the go programming language
package main
import "fmt"
func chowla(n int) int {
if n < 1 {
panic("argument must be a positive integer")
}
sum := 0
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
if i == j {
sum += i
} else {
sum += i + j
}
}
}
return sum
}
func sieve(limit int) []bool {
// True denotes composite, false denotes prime.
// Only interested in odd numbers >= 3
c := make([]bool, limit)
for i := 3; i*3 < limit; i += 2 {
if !c[i] && chowla(i) == 0 {
for j := 3 * i; j < limit; j += 2 * i {
c[j] = true
}
}
}
return c
}
func commatize(n int) string {
s := fmt.Sprintf("%d", n)
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
return s
}
func main() {
for i := 1; i <= 37; i++ {
fmt.Printf("chowla(%2d) = %d\n", i, chowla(i))
}
fmt.Println()
count := 1
limit := int(1e7)
c := sieve(limit)
power := 100
for i := 3; i < limit; i += 2 {
if !c[i] {
count++
}
if i == power-1 {
fmt.Printf("Count of primes up to %-10s = %s\n", commatize(power), commatize(count))
power *= 10
}
}
fmt.Println()
count = 0
limit = 35000000
for i := uint(2); ; i++ {
p := 1 << (i - 1) * (1<<i - 1) // perfect numbers must be of this form
if p > limit {
break
}
if chowla(p) == p-1 {
fmt.Printf("%s is a perfect number\n", commatize(p))
count++
}
}
fmt.Println("There are", count, "perfect numbers <= 35,000,000")
}
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Ecstasy programming language
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Maxima programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Java programming language