Chowla numbers: Difference between revisions

Add CLU
(Add CLU)
Line 903:
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|CLU}}==
<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</lang>
{{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}}
2,094

edits