Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
70
Task/Chowla-numbers/CLU/chowla-numbers.clu
Normal file
70
Task/Chowla-numbers/CLU/chowla-numbers.clu
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
% 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue