How to resolve the algorithm Chowla numbers step by step in the Groovy programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Chowla numbers step by step in the Groovy 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 Groovy programming language
Source code in the groovy programming language
class Chowla {
static int chowla(int n) {
if (n < 1) throw new RuntimeException("argument must be a positive integer")
int sum = 0
int i = 2
while (i * i <= n) {
if (n % i == 0) {
int j = (int) (n / i)
sum += (i == j) ? i : i + j
}
i++
}
return sum
}
static boolean[] sieve(int limit) {
// True denotes composite, false denotes prime.
// Only interested in odd numbers >= 3
boolean[] c = new boolean[limit]
for (int i = 3; i < limit / 3; i += 2) {
if (!c[i] && chowla(i) == 0) {
for (int j = 3 * i; j < limit; j += 2 * i) {
c[j] = true
}
}
}
return c
}
static void main(String[] args) {
for (int i = 1; i <= 37; i++) {
printf("chowla(%2d) = %d\n", i, chowla(i))
}
println()
int count = 1
int limit = 10_000_000
boolean[] c = sieve(limit)
int power = 100
for (int i = 3; i < limit; i += 2) {
if (!c[i]) {
count++
}
if (i == power - 1) {
printf("Count of primes up to %,10d = %,7d\n", power, count)
power *= 10
}
}
println()
count = 0
limit = 35_000_000
int i = 2
while (true) {
int p = (1 << (i - 1)) * ((1 << i) - 1) // perfect numbers must be of this form
if (p > limit) break
if (chowla(p) == p - 1) {
printf("%,d is a perfect number\n", p)
count++
}
i++
}
printf("There are %,d perfect numbers <= %,d\n", count, limit)
}
}
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Variables step by step in the Diego programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Quackery programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the Python programming language