Chowla numbers: Difference between revisions

m
no edit summary
(Add MAD)
imported>Wilm
mNo edit summary
 
(36 intermediate revisions by 19 users not shown)
Line 74:
:*   the OEIS entry for   [http://oeis.org/A048050 A48050 Chowla's function].
<br><br>
=={{header|11l}}==
{{trans|C}}
 
<syntaxhighlight lang="11l">F chowla(n)
V sum = 0
V i = 2
L i * i <= n
I n % i == 0
sum += i
V j = n I/ i
I i != j
sum += j
i++
R sum
 
L(n) 1..37
print(‘chowla(’n‘) = ’chowla(n))
 
V count = 0
V power = 100
L(n) 2..10'000'000
I chowla(n) == 0
count++
I n % power == 0
print(‘There are ’count‘ primes < ’power)
power *= 10
 
count = 0
V limit = 350'000'000
V k = 2
V kk = 3
L
V p = k * kk
I p > limit
L.break
I chowla(p) == p - 1
print(p‘ is a perfect number’)
count++
k = kk + 1
kk += k
print(‘There are ’count‘ perfect numbers < ’limit)</syntaxhighlight>
 
{{out}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
There are 25 primes < 100
There are 168 primes < 1000
There are 1229 primes < 10000
There are 9592 primes < 100000
There are 78498 primes < 1000000
There are 664579 primes < 10000000
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
33550336 is a perfect number
There are 5 perfect numbers < 350000000
</pre>
=={{header|Ada}}==
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Chowla_Numbers is
Line 146 ⟶ 239:
Put_Prime;
Put_Perfect;
end Chowla_Numbers;</langsyntaxhighlight>
 
{{out}}
Line 198 ⟶ 291:
33550336 is a perfect number
There are 5 perfect numbers < 350000000</pre>
=={{header|ALGOL 68}}==
{{Trans|C}}
<syntaxhighlight lang="algol68">BEGIN # find some Chowla numbers ( Chowla n = sum of divisors of n exclusing n and 1 ) #
# returs the Chowla number of n #
PROC chowla = ( INT n )INT:
BEGIN
INT sum := 0;
FOR i FROM 2 WHILE i * i <= n DO
IF n MOD i = 0 THEN
INT j = n OVER i;
sum +:= i + IF i = j THEN 0 ELSE j FI
FI
OD;
sum
END # chowla # ;
 
FOR n TO 37 DO print( ( "chowla(", whole( n, 0 ), ") = ", whole( chowla( n ), 0 ), newline ) ) OD;
INT count := 0, power := 100;
FOR n FROM 2 TO 10 000 000 DO
IF chowla( n ) = 0 THEN count +:= 1 FI;
IF n MOD power = 0 THEN
print( ( "There are ", whole( count, 0 ), " primes < ", whole( power, 0 ), newline ) );
power *:= 10
FI
OD;
count := 0;
INT limit = 350 000 000;
INT k := 2, kk := 3;
WHILE INT p = k * kk;
p <= limit
DO
IF chowla( p ) = p - 1 THEN
print( ( whole( p, 0 ), " is a perfect number", newline ) );
count +:= 1
FI;
k := kk + 1; kk +:= k
OD;
print( ( "There are ", whole( count, 0 ), " perfect numbers < ", whole( limit, 0 ), newline ) )
END</syntaxhighlight>
{{out}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
There are 25 primes < 100
There are 168 primes < 1000
There are 1229 primes < 10000
There are 9592 primes < 100000
There are 78498 primes < 1000000
There are 664579 primes < 10000000
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
33550336 is a perfect number
There are 5 perfect numbers < 350000000
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">chowla: function [n]-> sum remove remove factors n 1 n
countPrimesUpTo: function [limit][
count: 1
loop 3.. .step: 2 limit 'x [
if zero? chowla x -> count: count + 1
]
return count
]
 
loop 1..37 'i -> print [i "=>" chowla i]
print ""
 
loop [100 1000 10000 100000 1000000 10000000] 'lim [
print ["primes up to" lim "=>" countPrimesUpTo lim]
]
print ""
print "perfect numbers up to 35000000:"
i: 2
while [i < 35000000][
if (chowla i) = i - 1 -> print i
i: i + 2
]</syntaxhighlight>
 
{{out}}
 
<pre>1 => 0
2 => 0
3 => 0
4 => 2
5 => 0
6 => 5
7 => 0
8 => 6
9 => 3
10 => 7
11 => 0
12 => 15
13 => 0
14 => 9
15 => 8
16 => 14
17 => 0
18 => 20
19 => 0
20 => 21
21 => 10
22 => 13
23 => 0
24 => 35
25 => 5
26 => 15
27 => 12
28 => 27
29 => 0
30 => 41
31 => 0
32 => 30
33 => 14
34 => 19
35 => 12
36 => 54
37 => 0
 
primes up to 100 => 25
primes up to 1000 => 168
primes up to 10000 => 1229
primes up to 100000 => 9592
primes up to 1000000 => 78498
primes up to 10000000 => 664579
 
perfect numbers up to 35000000:
6
28
496
8128
33550336</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CHOWLA_NUMBERS.AWK
# converted from Go
Line 276 ⟶ 538:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 328 ⟶ 590:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
unsigned chowla(const unsigned n) {
Line 361 ⟶ 622:
printf("There are %u perfect numbers < %u\n", count, limit);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 412 ⟶ 673:
33550336 is a perfect number
There are 5 perfect numbers < 350000000</pre>
=={{header|C sharp|C#}}==
 
=={{header|C#|csharp}}==
{{trans|Go}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace chowla_cs
Line 473 ⟶ 733:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 525 ⟶ 785:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
 
Line 584 ⟶ 843:
cout << "There are " << count << " perfect numbers <= 35,000,000\n";
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 635 ⟶ 894:
33,550,336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Chowla's function
chowla = proc (n: int) returns (int)
sum: int := 0
i: int := 2
while i*i <= n do
if n//i = 0 then
sum := sum + i
j: int := n/i
if i ~= j then
sum := sum + j
end
end
i := i + 1
end
return(sum)
end chowla
 
% A number is prime iff chowla(n) is 0
prime = proc (n: int) returns (bool)
return(chowla(n) = 0)
end prime
 
% A number is perfect iff chowla(n) equals n-1
perfect = proc (n: int) returns (bool)
return(chowla(n) = n-1)
end perfect
 
start_up = proc ()
LIMIT = 35000000
po: stream := stream$primary_output()
% Show chowla(1) through chowla(37)
for i: int in int$from_to(1, 37) do
stream$putl(po, "chowla(" || int$unparse(i) || ") = "
|| int$unparse(chowla(i)))
end
% Count primes up to powers of 10
pow10: int := 2 % start with 100
primecount: int := 1 % assume 2 is prime, then test only odd numbers
candidate: int := 3
while pow10 <= 7 do
if candidate >= 10**pow10 then
stream$putl(po, "There are "
|| int$unparse(primecount)
|| " primes up to "
|| int$unparse(10**pow10))
pow10 := pow10 + 1
end
if prime(candidate) then primecount := primecount + 1 end
candidate := candidate + 2
end
% Find perfect numbers up to 35 million
perfcount: int := 0
k: int := 2
kk: int := 3
while true do
n: int := k * kk
if n >= LIMIT then break end
if perfect(n) then
perfcount := perfcount + 1
stream$putl(po, int$unparse(n) || " is a perfect number.")
end
k := kk + 1
kk := kk + k
end
stream$putl(po, "There are " || int$unparse(perfcount) ||
" perfect numbers < 35,000,000.")
end start_up</syntaxhighlight>
{{out}}
<pre>chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
There are 25 primes up to 100
There are 168 primes up to 1000
There are 1229 primes up to 10000
There are 9592 primes up to 100000
There are 78498 primes up to 1000000
There are 664579 primes up to 10000000
6 is a perfect number.
28 is a perfect number.
496 is a perfect number.
8128 is a perfect number.
33550336 is a perfect number.
There are 5 perfect numbers < 35,000,000.</pre>
=={{header|Cowgol}}==
{{trans|C}}
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub chowla(n: uint32): (sum: uint32) is
Line 704 ⟶ 1,083:
print(" perfect numbers < ");
print_i32(LIMIT);
print_nl();</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 755 ⟶ 1,134:
33550336 is a perfect number.
There are 5 perfect numbers < 35000000</pre>
 
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.stdio;
 
int chowla(int n) {
Line 820 ⟶ 1,198:
}
writefln("There are %d perfect numbers <= 35,000,000", count);
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 871 ⟶ 1,249:
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|Delphi}}==
See [[#Pascal]].
Line 878 ⟶ 1,255:
{{trans|C#}}
 
<langsyntaxhighlight lang="dyalect">func chowla(n) {
var sum = 0
var i = 2
Line 884 ⟶ 1,261:
while i * i <= n {
if n % i == 0 {
var app = if i == (j = n / i) {
var app = if i == j {
0
} else {
Line 895 ⟶ 1,273:
return sum
}
 
func sieve(limit) {
var c = Array.emptyEmpty(limit)
var i = 3
while i * 3 < limit {
Line 911 ⟶ 1,289:
return c
}
 
for i in 1..37 {
print("chowla(\(i)) = \(chowla(i))")
}
 
var count = 1
var limit = 10000000
var power = 100
var c = sieve(limit);
 
var i = 3
while i < limit {
Line 932 ⟶ 1,310:
i += 2
}
 
count = 0
limit = 35000000;
var k = 2
var kk = 3
var p
i = 2
 
while true {
if (p = k * kk) > limit {
if p > limit {
break
}
Line 951 ⟶ 1,330:
kk += k
}
 
print("There are \(count) perfect numbers <= 35,000,000")</langsyntaxhighlight>
 
{{out}}
Line 1,005 ⟶ 1,384:
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight lang="text">
<lang>func chowla n . sum .
fastfunc chowla n .
sum = 0
i sum = 20
while i * i <= n2
while ifi n mod* i <= 0n
j =if n divmod i = 0
if i = j = n div i
sum +=if i = j
else sum += i
sum += i + jelse
sum += i + j
.
.
. i += 1
i += 1.
return sum
.
.
funcproc sieve . c[] .
i = 3
while i * 3 <= len c[]
if c[i] = 0
call if chowla i h= 0
if h j = 03 * i
while j <= 3 *len ic[]
while j < len c[j] = 1
c[ j] += 12 * i
j += 2 * i.
.
.
. i += 2
i += 2.
.
.
funcproc commatize n . s$ .
s$[] = str_charsstrchars n
s$ = ""
l = len s$[]
for i range= 1 to len s$[]
if i > 01 and l mod 3 = 0
s$ &= ","
.
l -= 1
s$ &= s$[i]
.
.
print "chowla number from 1 to 37"
for i = 1 to 37
print " " & i & ": " & chowla i
call chowla i h
print " " & i & ": " & h
.
funcproc main . .
print ""
len c[] 10000000
count = 1
call sieve c[]
power = 100
i = 3
while i <= len c[]
if c[i] = 0
count += 1
.
if i = power - 1
call commatize power p$
call commatize count c$
print "There are " & c$ & " primes up to " & p$
power *= 10
.
i += 2
.
print ""
limit = 35000000
count = 0
i = 2
k = 2
kk = 3
repeat
p = k * kk
until p > limit
call if chowla p h= p - 1
if h = p - 1commatize p s$
print s$ & " is a perfect number"
call commatize p s$
print s$ & "count is+= a perfect number"1
count += 1.
. k = kk + 1
k = kk += 1k
kk i += k1
i += 1.
commatize limit s$
.
print "There are " & count & " perfect mumbers up to " & s$
call commatize limit s$
print "There are " & count & " perfect mumbers up to " & s$
.
call main</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 1,152 ⟶ 1,530:
33,550,336 is a perfect number
There are 5 perfect mumbers up to 35,000,000
</pre>
 
=={{header|EMal}}==
{{trans|Go}}
<syntaxhighlight lang="emal">
fun chowla = int by int n
int sum = 0
int j = 0
for int i = 2; i * i <= n; i++ do
if n % i == 0 do sum += i + when(i == (j = n / i), 0, j) end
end
return sum
end
fun sieve = List by int limit
List c = logic[].with(limit)
for int i = 3; i * 3 < limit; i += 2
if c[i] or chowla(i) != 0 do continue end
for int j = 3 * i; j < limit; j += 2 * i do c[j] = true end
end
return c
end
# find and display (1 per line) for the 1st 37 integers
for int i = 1; i <= 37; i++ do writeLine("chowla(" + i + ") = " + chowla(i)) end
int count = 1
int limit = 10000000
int power = 100
List c = sieve(limit)
for int i = 3; i < limit; i += 2
if not c[i] do count++ end
if i == power - 1
writeLine("Count of primes up to " + power + " = " + count)
power *= 10
end
end
count = 0
limit = 35000000
int k = 2
int kk = 3
int p
for int i = 2; ; i++
if (p = k * kk) > limit do break end
if chowla(p) == p - 1
writeLine(p + " is a number that is perfect")
count++
end
k = kk + 1
kk += k
end
writeLine("There are " + count + " perfect numbers <= 35,000,000")
</syntaxhighlight>
It takes about fifteen minutes to complete on my i7-8650U with 8.00GB of RAM.
{{out}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
Count of primes up to 100 = 25
Count of primes up to 1000 = 168
Count of primes up to 10000 = 1229
Count of primes up to 100000 = 9592
Count of primes up to 1000000 = 78498
Count of primes up to 10000000 = 664579
6 is a number that is perfect
28 is a number that is perfect
496 is a number that is perfect
8128 is a number that is perfect
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry grouping.extras io kernel math
math.primes.factors math.ranges math.statistics sequences
tools.memory.private ;
Line 1,182 ⟶ 1,662:
count-primes nl 35e7 show-perfect ;
 
MAIN: chowla-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,236 ⟶ 1,716:
33,550,336 is perfect
</pre>
=={{header|Fortran}}==
{{works with|VAX Fortran|V4.6-244}}
{{libheader|VAX/VMS V4.6}}This compiler implements the Fortran-77 standard. The VAX/VMS operating system runs on simulated hardware using the open source [https://opensimh.org/ opensimh] platform.
{{trans|Ada}}
 
Run time on a Raspberry Pi 4 Model B Rev 1.1 (Raspbian GNU/Linux 10 buster) was 7h 21m
=={{header|FreeBASIC}}==
<syntaxhighlight lang="fortran" line="1">
 
PROGRAM CHOWLA
 
CALL PUT_1ST_37
CALL PUT_PRIME
CALL PUT_PERFECT
 
END
 
INTEGER*4 FUNCTION CHOWLA1(N)
 
C The Chowla number of N is the sum of the divisors of N
C excluding unity and N where N is a positive integer
 
IMPLICIT INTEGER*4 (A-Z)
 
IF (N .LE. 0) STOP 'Argument to Chowla function must be > 0'
 
SUM = 0
I = 2
 
100 CONTINUE
IF (I * I .GT. N) GOTO 200
 
IF (MOD(N, I) .NE. 0) GOTO 110
J = N / I
SUM = SUM + I
IF ( I .NE. J) SUM = SUM + J
110 CONTINUE
 
I = I + 1
GOTO 100
 
200 CONTINUE
 
CHOWLA1 = SUM
 
RETURN
 
END
 
SUBROUTINE PUT_1ST_37
IMPLICIT INTEGER*4 (A-Z)
 
DO 100 I = 1, 37
PRINT 900, I, CHOWLA1(I)
100 CONTINUE
 
RETURN
 
900 FORMAT(1H , 'CHOWLA(', I2, ') = ', I2)
 
END
 
SUBROUTINE PUT_PRIME
IMPLICIT INTEGER*4 (A-Z)
PARAMETER LIMIT = 10000000
 
COUNT = 0
POWER = 100
 
DO 200 N = 2, LIMIT
 
IF (CHOWLA1(N) .EQ. 0) COUNT = COUNT + 1
 
IF (MOD(N, POWER) .NE. 0) GOTO 100
 
PRINT 900, COUNT, POWER
POWER = POWER * 10
 
100 CONTINUE
 
200 CONTINUE
 
RETURN
 
900 FORMAT(1H ,'There are ', I12, ' primes < ', I12)
 
END
 
SUBROUTINE PUT_PERFECT
IMPLICIT INTEGER*4 (A-Z)
PARAMETER LIMIT = 35000000
 
COUNT = 0
K = 2
KK = 3
 
100 CONTINUE
 
P = K * KK
 
IF (P .GT. LIMIT) GOTO 300
 
IF (CHOWLA1(P) .NE. P - 1) GOTO 200
PRINT 900, P
COUNT = COUNT + 1
 
200 CONTINUE
 
K = KK + 1
KK = KK + K
 
GOTO 100
 
300 CONTINUE
 
PRINT 910, COUNT, LIMIT
 
RETURN
 
900 FORMAT(1H , I10, ' is a perfect number')
910 FORMAT(1H , 'There are ', I10, ' perfect numbers < ', I10)
 
END
</syntaxhighlight>
{{out}}<pre>
CHOWLA( 1) = 0
CHOWLA( 2) = 0
CHOWLA( 3) = 0
CHOWLA( 4) = 2
CHOWLA( 5) = 0
CHOWLA( 6) = 5
CHOWLA( 7) = 0
CHOWLA( 8) = 6
CHOWLA( 9) = 3
CHOWLA(10) = 7
CHOWLA(11) = 0
CHOWLA(12) = 15
CHOWLA(13) = 0
CHOWLA(14) = 9
CHOWLA(15) = 8
CHOWLA(16) = 14
CHOWLA(17) = 0
CHOWLA(18) = 20
CHOWLA(19) = 0
CHOWLA(20) = 21
CHOWLA(21) = 10
CHOWLA(22) = 13
CHOWLA(23) = 0
CHOWLA(24) = 35
CHOWLA(25) = 5
CHOWLA(26) = 15
CHOWLA(27) = 12
CHOWLA(28) = 27
CHOWLA(29) = 0
CHOWLA(30) = 41
CHOWLA(31) = 0
CHOWLA(32) = 30
CHOWLA(33) = 14
CHOWLA(34) = 19
CHOWLA(35) = 12
CHOWLA(36) = 54
CHOWLA(37) = 0
There are 25 primes < 100
There are 168 primes < 1000
There are 1229 primes < 10000
There are 9592 primes < 100000
There are 78498 primes < 1000000
There are 664579 primes < 10000000
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
33550336 is a perfect number
There are 5 perfect numbers < 35000000
</pre>
 
== {{header|FreeBASIC}} ==
{{trans|Visual Basic}}
<langsyntaxhighlight lang="freebasic">
' Chowla_numbers
 
Line 1,322 ⟶ 1,976:
Sleep
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,383 ⟶ 2,037:
Pulsa una tecla para salir
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn Chowla( n as NSUInteger ) as NSUInteger
NSUInteger i, j, r = 0
i = 2
while ( i * i <= n )
j = n / i
if ( n mod i == 0 )
r += i
if ( i != j )
r += j
end if
end if
i++
wend
end fn = r
 
local fn DoIt
NSUInteger n, count = 0, power = 100, limit, k, kk, p = 0
for n = 1 to 37
printf @"chowla(%u) = %u", n, fn Chowla( n )
next
for n = 2 to 10000000
if ( fn Chowla(n) == 0 ) then count ++
if ( n mod power == 0 ) then printf @"There are %u primes < %-7u", count, power : power *= 10
next
count = 0
limit = 350000000
k = 2 : kk = 3
do
p = k * kk
if ( fn Chowla( p ) == p - 1 )
printf @"%9u is a perfect number", p
count++
end if
k = kk + 1
kk = kk + k
until ( p > limit )
printf @"There are %u perfect numbers < %u", count, limit
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
There are 25 primes < 100
There are 168 primes < 1,000
There are 1,229 primes < 10,000
There are 9,592 primes < 100,000
There are 78,498 primes < 1,000,000
There are 664,579 primes < 10,000,000
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>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,464 ⟶ 2,221:
}
fmt.Println("There are", count, "perfect numbers <= 35,000,000")
}</langsyntaxhighlight>
 
{{out}}
Line 1,520 ⟶ 2,277:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|Groovy}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="groovy">class Chowla {
static int chowla(int n) {
if (n < 1) throw new RuntimeException("argument must be a positive integer")
Line 1,587 ⟶ 2,343:
printf("There are %,d perfect numbers <= %,d\n", count, limit)
}
}</langsyntaxhighlight>
{{out}}
<pre>chowla( 1) = 0
Line 1,643 ⟶ 2,399:
Uses arithmoi Library: https://hackage.haskell.org/package/arithmoi-0.11.0.0
compiled with "-O2 -threaded -rtsopts"<br/>
<langsyntaxhighlight lang="haskell">import Control.Concurrent (setNumCapabilities)
import Control.Monad.Par (runPar, get, spawnP)
import Control.Monad (join, (>=>))
Line 1,705 ⟶ 2,461:
. fmap (chowlaPrimes $ take (10^7) allChowlas)
perfects = chowlaPerfects allChowlas
allChowlas = chowlas [1..35*10^6]</langsyntaxhighlight>
{{out}}
<pre>Using 4 cores
Line 1,757 ⟶ 2,513:
33,550,336 is a perfect number.
There are 5 perfect numbers < 35,000,000</pre>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">chowla=: >: -~ >:@#.~/.~&.q: NB. sum of factors - (n + 1)
 
intsbelow=: (2 }. i.)"0
countPrimesbelow=: +/@(0 = chowla)@intsbelow
findPerfectsbelow=: (#~ <: = chowla)@intsbelow</langsyntaxhighlight>
'''Tasks:'''
<langsyntaxhighlight lang="j"> (] ,. chowla) >: i. 37 NB. chowla numbers 1-37
1 0
2 0
Line 1,807 ⟶ 2,562:
25 168 1229 9592 78498 664579
findPerfectsbelow 35000000
6 28 496 8128 33550336</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C}}
<syntaxhighlight lang="java">
<lang Java>
public class Chowla {
 
Line 1,897 ⟶ 2,651:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,952 ⟶ 2,706:
There are 5 perfect numbers < 35,000,000
</pre>
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The "brute-force" computation of the perfect number beyond 8,128 took many hours.
<syntaxhighlight lang="jq">def add(stream): reduce stream as $x (0; . + $x);
 
# input should be an integer
def commatize:
def digits: tostring | explode | reverse;
if . == null then ""
elif . < 0 then "-" + ((- .) | commatize)
else [foreach digits[] as $d (-1; .+1;
# "," is 44
(select(. > 0 and . % 3 == 0)|44), $d)]
| reverse
| implode
end;
 
def count(stream): reduce stream as $i (0; . + 1);
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
# unordered
def proper_divisors:
. as $n
| if $n > 1 then 1,
( range(2; 1 + (sqrt|floor)) as $i
| if ($n % $i) == 0 then $i,
(($n / $i) | if . == $i then empty else . end)
else empty
end)
else empty
end;
 
def chowla:
if . == 1 then 0
else add(proper_divisors) - 1
end;
 
# Input: a positive integer
def is_chowla_prime:
. > 1 and chowla == 0;
 
# In the interests of green(er) computing ...
def chowla_primes($n):
2, range(3; $n; 2) | select(is_chowla_prime);
 
def report_chowla_primes:
reduce range(2; 10000000) as $i (null;
if $i | is_chowla_prime
then if $i < 10000000 then .[7] += 1 else . end
| if $i < 1000000 then .[6] += 1 else . end
| if $i < 100000 then .[5] += 1 else . end
| if $i < 10000 then .[4] += 1 else . end
| if $i < 1000 then .[3] += 1 else . end
| if $i < 100 then .[2] += 1 else . end
else . end)
| (range(2;8) as $i
| "10 ^ \($i) \(.[$i]|commatize|lpad(16))") ;
 
def is_chowla_perfect:
(. > 1) and (chowla == . - 1);
def task:
" n\("chowla"|lpad(16))",
(range(1;38) | "\(lpad(3)): \(chowla|lpad(10))"),
"\n n \("Primes < n"|lpad(10))",
report_chowla_primes,
# "\nPerfect numbers up to 35e6",
# (range(1; 35e6) | select(is_chowla_perfect) | commatize)
""
;
 
task</syntaxhighlight>
{{out}}
<pre>
n chowla
1: 0
2: 0
3: 0
4: 2
5: 0
6: 5
7: 0
8: 6
9: 3
10: 7
11: 0
12: 15
13: 0
14: 9
15: 8
16: 14
17: 0
18: 20
19: 0
20: 21
21: 10
22: 13
23: 0
24: 35
25: 5
26: 15
27: 12
28: 27
29: 0
30: 41
31: 0
32: 30
33: 14
34: 19
35: 12
36: 54
37: 0
 
n Primes < n
10 ^ 2 25
10 ^ 3 168
10 ^ 4 1,229
10 ^ 5 9,592
10 ^ 6 78,498
10 ^ 7 664,579
 
Perfect numbers up to 35e6
6
28
496
8,128
33,550,336
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function chowla(n)
Line 1,994 ⟶ 2,881:
 
testchowla()
</langsyntaxhighlight>{{out}}
<pre>
The first 37 chowla numbers are:
Line 2,047 ⟶ 2,934:
The count of perfect numbers up to 35,000,000 is 5.
</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.3.21
 
fun chowla(n: Int): Int {
Line 2,110 ⟶ 2,996:
}
println("There are $count perfect numbers <= 35,000,000")
}</langsyntaxhighlight>
 
{{output}}
Line 2,116 ⟶ 3,002:
Same as Go example.
</pre>
 
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang="lua">function chowla(n)
local sum = 0
local i = 2
Line 2,196 ⟶ 3,081:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 2,247 ⟶ 3,132:
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|MAD}}==
{{trans|C}}
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
Line 2,300 ⟶ 3,184:
DONE PRINT FORMAT PRFCNT, COUNT, LIMIT
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre>CHOWLA( 1) = 0
Line 2,351 ⟶ 3,235:
33550336 IS A PERFECT NUMBER.
THERE ARE 5 PERFECT NUMBERS BELOW 35000000</pre>
 
=={{header|Maple}}==
 
{{incorrect|Maple| <br><br> The output for Chowla(1) is incorrect. <br><br> }}
 
<langsyntaxhighlight Maplelang="maple">ChowlaFunction := n -> NumberTheory:-SumOfDivisors(n) - n - 1;
 
PrintChowla := proc(n::posint) local i;
Line 2,384 ⟶ 3,267:
countPrimes(1000000);
countPrimes(10000000);
findPerfect(35000000)</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,432 ⟶ 3,315:
664579
[6, 28, 496, 8128, 33550336]</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<syntaxhighlight lang="mathematica">ClearAll[Chowla]
Chowla[0 | 1] := 0
Chowla[n_] := DivisorSigma[1, n] - 1 - n
Table[{i, Chowla[i]}, {i, 37}] // Grid
PrintTemporary[Dynamic[n]];
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 100, 2}]; i
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 1000, 2}]; i
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 10000, 2}]; i
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 100000, 2}]; i
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 1000000, 2}]; i
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 10000000, 2}]; i
Reap[Do[If[Chowla[n] == n - 1, Sow[n]], {n, 1, 35 10^6}]][[2, 1]]</syntaxhighlight>
{{out}}
<pre>25
168
1229
9592
78498
664579
{1, 6, 28, 496, 8128, 33550336}</pre>
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import strformat
import strutils
 
Line 2,477 ⟶ 3,380:
k = kk + 1
kk += k
echo &"There are {count} perfect numbers < {insertSep($limit, ',')}"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,530 ⟶ 3,433:
There are 5 perfect numbers < 350,000,000
</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
chowla(n) = {
if (n < 1, error("Chowla function argument must be positive"));
if (n < 4, return(0));
my(divs = divisors(n));
sum(i=1, #divs, divs[i]) - n - 1;
}
 
\\ Function to count Chowla numbers
countchowlas(n, asperfect = 1, verbose = 1) = {
my(count = 0, chow, i);
for (i = 2, n,
chow = chowla(i);
if ( (asperfect && (chow == i - 1)) || ((!asperfect) && (chow == 0)),
count++;
if (verbose, print("The number " i " is " if (asperfect, "perfect.", "prime.")));
);
);
count;
}
 
\\ Main execution block
{
print("The first 37 chowla numbers are:");
for (i = 1, 37, printf("Chowla(%s) is %s\n", Str(i), Str(chowla(i)) ) );
m=100;
while(m<=10000000, print("The count of the primes up to " m " is " countchowlas(m, 0, 0)); m=m*10);
print("The count of perfect numbers up to 35,000,000 is " countchowlas(35000000, 1, 1));
}
</syntaxhighlight>
{{out}}
<pre>
The first 37 chowla numbers are:
Chowla(1) is 0
Chowla(2) is 0
Chowla(3) is 0
Chowla(4) is 2
Chowla(5) is 0
Chowla(6) is 5
Chowla(7) is 0
Chowla(8) is 6
Chowla(9) is 3
Chowla(10) is 7
Chowla(11) is 0
Chowla(12) is 15
Chowla(13) is 0
Chowla(14) is 9
Chowla(15) is 8
Chowla(16) is 14
Chowla(17) is 0
Chowla(18) is 20
Chowla(19) is 0
Chowla(20) is 21
Chowla(21) is 10
Chowla(22) is 13
Chowla(23) is 0
Chowla(24) is 35
Chowla(25) is 5
Chowla(26) is 15
Chowla(27) is 12
Chowla(28) is 27
Chowla(29) is 0
Chowla(30) is 41
Chowla(31) is 0
Chowla(32) is 30
Chowla(33) is 14
Chowla(34) is 19
Chowla(35) is 12
Chowla(36) is 54
Chowla(37) is 0
The count of the primes up to 100 is 25
The count of the primes up to 1000 is 168
The count of the primes up to 10000 is 1229
The count of the primes up to 100000 is 9592
The count of the primes up to 1000000 is 78498
The count of the primes up to 10000000 is 664579
The number 6 is perfect.
The number 28 is perfect.
The number 496 is perfect.
The number 8128 is perfect.
The number 33550336 is perfect.
The count of perfect numbers up to 35000000 is 5.
</pre>
 
 
=={{header|Pascal}}==
Line 2,536 ⟶ 3,526:
{{trans|Go}} but not using a sieve, cause a sieve doesn't need precalculated small primes.<BR>
So runtime is as bad as trial division.
<langsyntaxhighlight lang="pascal">program Chowla_numbers;
 
{$IFDEF FPC}
Line 2,641 ⟶ 3,631:
Count10Primes(10 * 1000 * 1000);
CheckPerf;
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,697 ⟶ 3,687:
real 1m54,534s
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'divisor_sum';
Line 2,735 ⟶ 3,724:
my @perfect = perfect(my $limit = 35_000_000);
printf "\nThere are %d perfect numbers up to %s: %s\n",
1+$#perfect, comma($limit), join(' ', map { comma($_) } @perfect);</langsyntaxhighlight>
{{out}}
<pre>chowla( 1) = 0
Line 2,784 ⟶ 3,773:
 
There are 5 perfect numbers up to 35,000,000: 6 28 496 8,128 33,550,336</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function chowla(atom n)
<span style="color: #008080;">function</span> <span style="color: #000000;">chowla</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
return sum(factors(n))
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function sieve(integer limit)
<span style="color: #008080;">function</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
-- True denotes composite, false denotes prime.
<span style="color: #000080;font-style:italic;">-- True denotes composite, false denotes prime.
-- Only interested in odd numbers >= 3
-- Only interested in odd numbers &gt;= 3</span>
sequence c = repeat(false,limit)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
for i=3 to floor(limit/3) by 2 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
-- if not c[i] and chowla(i)==0 then
<span style="color: #000080;font-style:italic;">-- if not c[i] then --and chowla(see note belowi)==0 then</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (see note below)</span>
for j=3*i to limit by 2*i do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
c[j] = true
<span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return c
<span style="color: #008080;">return</span> <span style="color: #000000;">c</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
atom limit = 1e7, count = 1, pow10 = 100, t0 = time()
<span style="color: #004080;">atom</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pow10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
sequence s = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
for i=1 to 37 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">37</span> <span style="color: #008080;">do</span>
s &= chowla(i)
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">chowla</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"chowla[1..37]: %v\n",{s})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"chowla[1..37]: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
s = sieve(limit)
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
for i=3 to limit by 2 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
if not s[i] then count += 1 end if
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if i==pow10-1 then
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">==</span><span style="color: #000000;">pow10</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
printf(1,"Count of primes up to %,d = %,d\n", {pow10, count})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Count of primes up to %,d = %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">pow10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
pow10 *= 10
<span style="color: #000000;">pow10</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
count = 0
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
limit = iff(machine_bits()=32?1.4e11:2.4e18)
<span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1.4e11</span><span style="color: #0000FF;">:</span><span style="color: #000000;">2.4e18</span><span style="color: #0000FF;">)</span>
--limit = power(2,iff(machine_bits()=32?53:64)) -- (see note below)
<span style="color: #000080;font-style:italic;">--limit = power(2,iff(machine_bits()=32?53:64)) -- (see note below)</span>
integer i=2
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span>
while true do
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
atom p = power(2,i-1)*(power(2,i)-1) -- perfect numbers must be of this form
<span style="color: #004080;">atom</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- perfect numbers must be of this form</span>
if p>limit then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">></span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if chowla(p)==p-1 then
<span style="color: #008080;">if</span> <span style="color: #000000;">chowla</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">p</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
printf(1,"%,d is a perfect number\n", p)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,d is a perfect number\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
count += 1
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
i += 1
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
printf(1,"There are %d perfect numbers <= %,d\n",{count,limit})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %d perfect numbers &lt;= %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">})</span>
?elapsed(time()-t0)</lang>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
The use of chowla() in sieve() does not actually achieve anything other than slow it down, so I took it out.
{{out}}
Line 2,859 ⟶ 3,849:
become 4s and 90s respectively, without finding anything else. Obviously 1.4e11 and 2.4e18
were picked to minimise the run times.
=={{header|Picat}}==
{{trans|Prolog}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
table
chowla(1) = 0.
chowla(2) = 0.
chowla(3) = 0.
chowla(N) = C, N>3 =>
Max = floor(sqrt(N)),
Sum = 0,
foreach (X in 2..Max, N mod X == 0)
Y := N div X,
Sum := Sum + X + Y
end,
if (N == Max * Max) then
Sum := Sum - Max
end,
C = Sum.
 
main =>
foreach (I in 1..37)
printf("chowla(%d) = %d\n", I, chowla(I))
end,
Ranges = {100, 1000, 10000, 100000, 1000000, 10000000},
foreach (Range in Ranges)
Count = 0,
foreach (I in 2..Range)
if (chowla(I) == 0) then
Count := Count + 1
end
end,
printf("There are %d primes less than %d.\n", Count, Range)
end,
Limit = 35000000,
Count = 0,
foreach (I in 2..Limit)
if (chowla(I) == I-1) then
printf("%d is a perfect number\n", I),
Count := Count + 1
end
end,
printf("There are %d perfect numbers less than %d.\n", Count, Limit).
</syntaxhighlight>
{{out}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
There are 25 primes less than 100.
There are 168 primes less than 1000.
There are 1229 primes less than 10000.
There are 9592 primes less than 100000.
There are 78498 primes less than 1000000.
There are 664579 primes less than 10000000.
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
33550336 is a perfect number
There are 5 perfect numbers less than 35000000.
</pre>
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de accu1 (Var Key)
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 2,924 ⟶ 4,009:
(prinl "Count of primes up to 1000000 = " (countP 1000000))
(prinl "Count of primes up to 10000000 = " (countP 10000000))
(println (listP 35000000))</langsyntaxhighlight>
 
{{out}}
Line 2,973 ⟶ 4,058:
(6 28 496 8128 33550336)
</pre>
 
=={{header|PowerBASIC}}==
 
Line 2,979 ⟶ 4,063:
 
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
Line 3,076 ⟶ 4,160:
CON.PRINT "press any key to exit program"
CON.WAITKEY$
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 3,131 ⟶ 4,215:
There are 8 perfect numbers <= 2,305,843,009,213,693,950
press any key to exit program</pre>
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">
chowla(1, 0).
chowla(2, 0).
chowla(N, C) :-
N > 2,
Max is floor(sqrt(N)),
findall(X, (between(2, Max, X), N mod X =:= 0), Xs),
findall(Y, (member(X1, Xs), Y is N div X1, Y \= Max), Ys),
!,
sum_list(Xs, S1),
sum_list(Ys, S2),
C is S1 + S2.
 
prime_count(Upper, Upper, Count, Count) :-
!.
 
prime_count(Lower, Upper, Add, Count) :-
chowla(Lower, 0),
!,
Lower1 is Lower + 1,
Add1 is Add + 1,
prime_count(Lower1, Upper, Add1, Count).
 
prime_count(Lower, Upper, Add, Count) :-
Lower1 is Lower + 1,
prime_count(Lower1, Upper, Add, Count).
 
perfect_numbers(Upper, Upper, AccNums, Nums) :-
!,
reverse(AccNums, Nums).
 
perfect_numbers(Lower, Upper, AccNums, Nums) :-
Perfect is Lower - 1,
chowla(Lower, Perfect),
!,
Lower1 is Lower + 1,
AccNums1 = [Lower|AccNums],
perfect_numbers(Lower1, Upper, AccNums1, Nums).
 
perfect_numbers(Lower, Upper, AccNums, Nums) :-
Lower1 is Lower + 1,
perfect_numbers(Lower1, Upper, AccNums, Nums).
 
main :-
% Chowla numbers
forall(between(1, 37, N), (
chowla(N, C),
format('chowla(~D) = ~D\n', [N, C])
)),
 
% Prime numbers
Ranges = [100, 1000, 10000, 100000, 1000000, 10000000],
forall(member(Range, Ranges), (
prime_count(2, Range, 0, PrimeCount),
format('There are ~D primes less than ~D.\n', [PrimeCount, Range])
)),
 
% Perfect numbers
Limit = 35000000,
perfect_numbers(2, Limit, [], Nums),
forall(member(Perfect, Nums), (
format('~D is a perfect number.\n', [Perfect])
)),
length(Nums, PerfectCount),
format('There are ~D perfect numbers < ~D.\n', [PerfectCount, Limit]).
</syntaxhighlight>
{{out}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
There are 25 primes less than 100.
There are 168 primes less than 1,000.
There are 1,229 primes less than 10,000.
There are 9,592 primes less than 100,000.
There are 78,498 primes less than 1,000,000.
There are 664,579 primes less than 10,000,000.
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>
=={{header|Python}}==
Uses [https://www.python.org/dev/peps/pep-0515/ underscores to separate digits] in numbers, and th [https://www.sympy.org/en/index.htm sympy library] to aid calculations.
 
<langsyntaxhighlight lang="python"># https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.divisors
from sympy import divisors
 
Line 3,166 ⟶ 4,369:
for i in range(6, 8):
print(f"primes_to({10**i:_}) == {primes_to(10**i):_}")
perfect_between(1_000_000, 35_000_000)</langsyntaxhighlight>
 
{{out}}
Line 3,232 ⟶ 4,435:
* Splitting one function for the jit compiler to digest.
 
<langsyntaxhighlight lang="python">from numba import jit
 
# https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.divisors
Line 3,265 ⟶ 4,468:
print(f" {i:_}")
c += 1
print(f"Found {c} Perfect numbers between [{n:_}, {m:_})")</langsyntaxhighlight>
 
{{out}}
Line 3,271 ⟶ 4,474:
 
Speedup - not much, subjectively...
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require racket/fixnum)
Line 3,318 ⟶ 4,520:
 
(let ((ns (for/list ((n (sequence-filter perfect?/chowla (in-range 2 35000000)))) n)))
(printf "There are ~a perfect numbers <= 35000000: ~a~%" (length ns) ns)))</langsyntaxhighlight>
 
{{out}}
Line 3,366 ⟶ 4,568:
Primes < 10000000: 664579
There are 5 perfect numbers <= 35000000: (6 28 496 8128 33550336)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 3,375 ⟶ 4,576:
(For a more reasonable test, reduce the orders-of-magnitude range in the "Primes count" line from 2..7 to 2..5)
 
<syntaxhighlight lang="raku" perl6line>sub comma { $^i.flip.comb(3).join(',').flip }
 
sub schnitzel (\Radda, \radDA = 0) {
Line 3,402 ⟶ 4,603:
say "\nPerfect numbers less than 35,000,000";
 
.&comma.say for mung-daal[^5];</langsyntaxhighlight>
 
{{out}}
Line 3,457 ⟶ 4,658:
33,550,336
</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program computes/displays chowla numbers (and may count primes & perfect numbers.*/
parse arg LO HI . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 3,487 ⟶ 4,687:
return s /*return " " " " " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do k=length(_)-3 to 1 by -3; _= insert(',', _, k); end; return _</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; &nbsp; 37 </tt>}}
<pre>
Line 3,564 ⟶ 4,764:
33,550,336 is a perfect number.
</pre>
 
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">def chowla(n)
sum = 0
i = 2
Line 3,619 ⟶ 4,818:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 3,670 ⟶ 4,869:
33550336 is a perfect number
There are 5 perfect numbers < 350000000</pre>
 
=={{header|Rust}}==
{{trans|C++}}
<syntaxhighlight lang="Rust">
fn chowla(n: usize) -> usize {
let mut sum = 0;
let mut i = 2;
while i * i <= n {
if n % i == 0 {
sum += i;
let j = n / i;
if i != j {
sum += j;
}
}
i += 1;
}
sum
}
 
fn sieve(limit: usize) -> Vec<bool> {
let mut c = vec![false; limit];
let mut i = 3;
while i * i < limit {
if !c[i] && chowla(i) == 0 {
let mut j = 3 * i;
while j < limit {
c[j] = true;
j += 2 * i;
}
}
i += 2;
}
c
}
 
fn main() {
for i in 1..=37 {
println!("chowla({}) = {}", i, chowla(i));
}
 
let mut count = 1;
let limit = 1e7 as usize;
let mut power = 100;
let c = sieve(limit);
for i in (3..limit).step_by(2) {
if !c[i] {
count += 1;
}
if i == power - 1 {
println!("Count of primes up to {} = {}", power, count);
power *= 10;
}
}
 
count = 0;
let limit = 35000000;
let mut k = 2;
let mut kk = 3;
loop {
let p = k * kk;
if p > limit {
break;
}
if chowla(p) == p - 1 {
println!("{} is a number that is perfect", p);
count += 1;
}
k = kk + 1;
kk += k;
}
println!("There are {} perfect numbers <= 35,000,000", count);
}
</syntaxhighlight>
{{out}}
<pre>
chowla(1) = 0
chowla(2) = 0
chowla(3) = 0
chowla(4) = 2
chowla(5) = 0
chowla(6) = 5
chowla(7) = 0
chowla(8) = 6
chowla(9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
Count of primes up to 100 = 25
Count of primes up to 1000 = 168
Count of primes up to 10000 = 1229
Count of primes up to 100000 = 9592
Count of primes up to 1000000 = 78498
Count of primes up to 10000000 = 664579
6 is a number that is perfect
28 is a number that is perfect
496 is a number that is perfect
8128 is a number that is perfect
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000
 
</pre>
 
=={{header|Scala}}==
This solution uses a lazily-evaluated iterator to find and sum the divisors of a number, and speeds up the large searches using parallel vectors.
<langsyntaxhighlight lang="scala">object ChowlaNumbers {
def main(args: Array[String]): Unit = {
println("Chowla Numbers...")
Line 3,687 ⟶ 5,013:
def chowlaNum(num: Int): Int = Iterator.range(2, math.sqrt(num).toInt + 1).filter(n => num%n == 0).foldLeft(0){case (s, n) => if(n*n == num) s + n else s + n + (num/n)}
}</langsyntaxhighlight>
 
{{out}}
Line 3,743 ⟶ 5,069:
4: 8,128
5: 33,550,336</pre>
 
=={{header|Swift}}==
 
Uses Grand Central Dispatch to perform concurrent prime counting and perfect number searching
 
<langsyntaxhighlight lang="swift">import Foundation
 
@inlinable
Line 3,831 ⟶ 5,156:
print("\(p) is a perfect number")
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,883 ⟶ 5,208:
8128 is a perfect number
33550336 is a perfect number</pre>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|6}}
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="vb">Option Explicit
 
Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
Line 3,997 ⟶ 5,321:
FreeConsole
 
End Sub</langsyntaxhighlight>
{{out}}
<pre>chowla(1)=0
Line 4,049 ⟶ 5,373:
There are 5 perfect numbers <= 35.000.000
press enter to quit program.</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|Go}}
<langsyntaxhighlight lang="vbnet">Imports System
 
Module Program
Line 4,100 ⟶ 5,423:
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 4,154 ⟶ 5,477:
===More Cowbell===
{{libheader|System.Numerics}}One can get a little further, but that 8th perfect number takes nearly a minute to verify. The 9th takes longer than I have patience. If you care to see the 9th and 10th perfect numbers, change the 31 to 61 or 89 where indicated by the comment.
<langsyntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Program
Line 4,212 ⟶ 5,535:
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 4,266 ⟶ 5,589:
2,305,843,008,139,952,128 is a number that is perfect
There are 8 perfect numbers <= 25,000,000,000,000,000,000</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn chowla(n int) int {
if n < 1 {
panic("argument must be a positive integer")
}
mut 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
}
fn sieve(limit int) []bool {
// True denotes composite, false denotes prime.
// Only interested in odd numbers >= 3
mut c := []bool{len: 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
}
fn main() {
for i := 1; i <= 37; i++ {
println("chowla(${i:2}) = ${chowla(i)}")
}
println('')
mut count := 1
mut limit := int(1e7)
c := sieve(limit)
mut power := 100
for i := 3; i < limit; i += 2 {
if !c[i] {
count++
}
if i == power-1 {
println("Count of primes up to ${power:-10} = $count")
power *= 10
}
}
println('')
count = 0
limit = 35000000
for i := 2; ; i++ {
p := (1 << (i -1)) * ((1<<i) - 1)
if p > limit {
break
}
if chowla(p) == p-1 {
println("$p is a perfect number")
count++
}
}
println("There are $count perfect numbers <= 35,000,000")
}</syntaxhighlight>
 
{{out}}
<pre>
chowla( 1) = 0
chowla( 2) = 0
chowla( 3) = 0
chowla( 4) = 2
chowla( 5) = 0
chowla( 6) = 5
chowla( 7) = 0
chowla( 8) = 6
chowla( 9) = 3
chowla(10) = 7
chowla(11) = 0
chowla(12) = 15
chowla(13) = 0
chowla(14) = 9
chowla(15) = 8
chowla(16) = 14
chowla(17) = 0
chowla(18) = 20
chowla(19) = 0
chowla(20) = 21
chowla(21) = 10
chowla(22) = 13
chowla(23) = 0
chowla(24) = 35
chowla(25) = 5
chowla(26) = 15
chowla(27) = 12
chowla(28) = 27
chowla(29) = 0
chowla(30) = 41
chowla(31) = 0
chowla(32) = 30
chowla(33) = 14
chowla(34) = 19
chowla(35) = 12
chowla(36) = 54
chowla(37) = 0
 
Count of primes up to 100 = 25
Count of primes up to 1,000 = 168
Count of primes up to 10,000 = 1,229
Count of primes up to 100,000 = 9,592
Count of primes up to 1,000,000 = 78,498
Count of primes up to 10,000,000 = 664,579
 
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
33550336 is a perfect number
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int, Nums
 
var chowla = Fn.new { |n| (n > 1) ? Nums.sum(Int.properDivisors(n)) - 1 : 0 }
 
for (i in 1..37) SystemFmt.print("chowla(%(Fmt.$2d) = $d(2", i))), = %(chowla.call(i))")
System.print()
var count = 1
Line 4,285 ⟶ 5,732:
if (!c[i]) count = count + 1
if (i == power - 1) {
SystemFmt.print("Count of primes up to %(Fmt.dc($,-10, power))10d = %(Fmt.dc(0$,d", power, count))")
power = power * 10
}
Line 4,298 ⟶ 5,745:
if (p > limit) break
if (chowla.call(p) == p-1) {
SystemFmt.print("%(Fmt.dc(0$, p))d is a perfect number", p)
count = count + 1
}
i = i + 1
}
System.print("There are %(count) perfect numbers <= 35,000,000")</langsyntaxhighlight>
 
{{out}}
Line 4,360 ⟶ 5,807:
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func Chowla(N); \Return sum of divisors
int N, Div, Sum, Quot;
[Div:= 2; Sum:= 0;
loop [Quot:= N/Div;
if Quot < Div then quit;
if Quot = Div and rem(0) = 0 then \N is a square
[Sum:= Sum+Quot; quit];
if rem(0) = 0 then
Sum:= Sum + Div + Quot;
Div:= Div+1;
];
return Sum;
];
 
int N, C, P;
[for N:= 1 to 37 do
[IntOut(0, N); Text(0, ": ");
IntOut(0, Chowla(N)); CrLf(0);
];
C:= 1; \count 2 as prime
N:= 3; \only check odd numbers
repeat if Chowla(N) = 0 then \N is prime
C:= C+1;
case N+1 of 100, 1000, 10_000, 100_000, 1_000_000, 10_000_000:
[Text(0, "There are "); IntOut(0, C); Text(0, " primes < ");
IntOut(0, N+1); CrLf(0)]
other [];
N:= N+2;
until N >= 10_000_000;
P:= 1; \perfect numbers are of form: 2^(P-1) * (2^P - 1)
loop [P:= P*2;
N:= P*(P*2-1);
if N > 35_000_000 then quit;
if Chowla(N) = N-1 then \N is perfect
[IntOut(0, N); CrLf(0)];
];
]</syntaxhighlight>
 
{{out}}
<pre>
1: 0
2: 0
3: 0
4: 2
5: 0
6: 5
7: 0
8: 6
9: 3
10: 7
11: 0
12: 15
13: 0
14: 9
15: 8
16: 14
17: 0
18: 20
19: 0
20: 21
21: 10
22: 13
23: 0
24: 35
25: 5
26: 15
27: 12
28: 27
29: 0
30: 41
31: 0
32: 30
33: 14
34: 19
35: 12
36: 54
37: 0
There are 25 primes < 100
There are 168 primes < 1000
There are 1229 primes < 10000
There are 9592 primes < 100000
There are 78498 primes < 1000000
There are 664579 primes < 10000000
6
28
496
8128
33550336
</pre>
=={{header|zkl}}==
{{trans|Go}}
<langsyntaxhighlight lang="zkl">fcn chowla(n){
if(n<1)
throw(Exception.ValueError("Chowla function argument must be positive"));
Line 4,385 ⟶ 5,922:
}
c
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn testChowla{
println("The first 37 Chowla numbers:\n",
[1..37].apply(chowla).concat(" ","[","]"), "\n");
Line 4,411 ⟶ 5,948:
}
println("There are %,d perfect numbers <= %,d".fmt(count,limit));
}();</langsyntaxhighlight>
 
{{out}}
Anonymous user