48 lines
1.2 KiB
Text
48 lines
1.2 KiB
Text
% The population count of an integer is never going to be
|
|
% higher than the amount of bits in it (max 64)
|
|
% so we can get away with a very simple primality test.
|
|
is_prime = proc (n: int) returns (bool)
|
|
if n<=2 then return(n=2) end
|
|
if n//2=0 then return(false) end
|
|
for i: int in int$from_to_by(n+2, n/2, 2) do
|
|
if n//i=0 then return(false) end
|
|
end
|
|
return(true)
|
|
end is_prime
|
|
|
|
% Find the population count of a number
|
|
pop_count = proc (n: int) returns (int)
|
|
c: int := 0
|
|
while n > 0 do
|
|
c := c + n // 2
|
|
n := n / 2
|
|
end
|
|
return(c)
|
|
end pop_count
|
|
|
|
% Is N pernicious?
|
|
pernicious = proc (n: int) returns (bool)
|
|
return(is_prime(pop_count(n)))
|
|
end pernicious
|
|
|
|
start_up = proc ()
|
|
po: stream := stream$primary_output()
|
|
|
|
stream$putl(po, "First 25 pernicious numbers: ")
|
|
n: int := 1
|
|
seen: int := 0
|
|
while seen<25 do
|
|
if pernicious(n) then
|
|
stream$puts(po, int$unparse(n) || " ")
|
|
seen := seen + 1
|
|
end
|
|
n := n + 1
|
|
end
|
|
|
|
stream$putl(po, "\nPernicious numbers between 888,888,877 and 888,888,888:")
|
|
for i: int in int$from_to(888888877,888888888) do
|
|
if pernicious(i) then
|
|
stream$puts(po, int$unparse(i) || " ")
|
|
end
|
|
end
|
|
end start_up
|