Jump to content

Chowla numbers: Difference between revisions

(→‎{{header|zkl}}: added code)
Line 1,159:
 
=={{header|zkl}}==
{{trans|Go}}
<lang zkl></lang>
<lang zkl></lang>fcn chowla(n){
if(n<1)
throw(Exception.ValueError("Chowla function argument must be positive"));
sum:=0;
foreach i in ([2..n.toFloat().sqrt()]){
if(n%i == 0){
j:=n/i;
if(i==j) sum+=i;
else sum+=i+j;
}
}
sum
}
 
fcn chowlaSieve(limit){
// True denotes composite, false denotes prime.
// Only interested in odd numbers >= 3
c:=Data(limit+100).fill(0); # slop at the end (for reverse wrap around)
foreach i in ([3..limit/3,2]){
if(not c[i] and chowla(i)==0)
{ foreach j in ([3*i..limit,2*i]){ c[j]=True } }
}
c
<lang zkl>}</lang>
<lang zkl>fcn testChowla{
println("The first 37 Chowla numbers:\n",
[1..37].apply(chowla).concat(" ","[","]"), "\n");
 
count,limit,power := 1, (1e7).toInt(), 100;
c:=chowlaSieve(limit);
foreach i in ([3..limit-1,2]){
if(not c[i]) count+=1;
if(i == power - 1){
println("The count of the primes up to %10,d is %8,d".fmt(power,count));
power*=10;
}
}
 
println();
count, limit = 0, 35_000_000;
foreach i in ([2..]){
p:=(1).shiftLeft(i - 1) * ((1).shiftLeft(i)-1); // perfect numbers must be of this form
if(p>limit) break;
if(p-1 == chowla(p)){
println("%,d is a perfect number".fmt(p));
count+=1;
}
}
println("There are %,d perfect numbers <= %,d".fmt(count,limit));
}();</lang>
 
{{out}}
<pre>
The first 37 Chowla numbers:
[0 0 0 2 0 5 0 6 3 7 0 15 0 9 8 14 0 20 0 21 10 13 0 35 5 15 12 27 0 41 0 30 14 19 12 54 0]
 
The count of the primes up to 100 is 25
The count of the primes up to 1,000 is 168
The count of the primes up to 10,000 is 1,229
The count of the primes up to 100,000 is 9,592
The count of the primes up to 1,000,000 is 78,498
The count of the primes up to 10,000,000 is 664,579
 
6 is a perfect number
28 is a perfect number
496 is a perfect number
8,128 is a perfect number
33,550,336 is a perfect number
There are 5 perfect numbers <= 35,000,000
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.