/*
Pernicious numbers (Rosetta code) in Picat.
http://rosettacode.org/wiki/Pernicious_numbers
"""
A pernicious number is a positive integer whose population count is a prime.
The population count (also known as pop count) is the number of 1's (ones) in
the binary representation of a non-negative integer.
For example, 22 (which is 10110 in binary) has a population count of 3, which
is prime and so 22 is a pernicious number.
Task requirements
- display the first 25 pernicious numbers.
- display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive).
- display each list of integers on one line (which may or may not include a title).
See also
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => go.
go =>
println(take_n($pernicious_number,25,1)),
println([J : J in 888888877..888888888, pernicious_number(J)]),
nl.
% Get the first N numbers that satisfies function F, starting with S
take_n(F,N,S) = L =>
I = S,
C = 0,
L = [],
while(C < N)
if call(F,I) then
L := L ++ [I],
C := C + 1
end,
I := I + 1
end.
pop_count(N) = sum([1: I in N.to_binary_string(), I = '1']).
pernicious_number(N) => prime(pop_count(N)).