/* Population count (Rosetta code) in Picat. http://rosettacode.org/wiki/Population_count """ The population count is the number of 1's (ones) in the binary representation of a non-negative integer. Population count is also known as pop count, popcount, sideways sum, and Hamming weight. For example, 5 (which is 101 in binary) has a population count of 2. Evil numbers are non-negative integers that have an even population count. Odious numbers are positive integers that have an odd population count. Task requirements - write a function (or routine) to return the population count of a non-negative integer. - all computation of the lists below should start with 0 (zero). - display the pop count of the 1st thirty powers of 3 (3^0, 3^1, 3^2, 3^3, 3^4, ...). - display the 1st thirty evil numbers. - display the 1st thirty odious numbers. - display each list of integers on one line (which may or may not include a title), each set of integers being shown should be properly identified. """ 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(powers_of_three=[pop_count(3**I) : I in 0..29]), println('evil_numbers '=take_n($evil_number, 30,0)), println('odious_numbers '=take_n($odious_number, 30,0)), nl. 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. evil_number(N) => pop_count(N) mod 2 == 0. odious_number(N) => pop_count(N) mod 2 == 1. pop_count(N) = sum([1: I in N.to_binary_string(), I = '1']).