How to resolve the algorithm Pernicious numbers step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pernicious numbers step by step in the Picat programming language

Table of Contents

Problem Statement

A   pernicious number   is a positive integer whose   population count   is a prime. The   population count   is the number of   ones   in the binary representation of a non-negative integer.

22   (which is   10110   in binary)   has a population count of   3,   which is prime,   and therefore
22   is a pernicious number.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pernicious numbers step by step in the Picat programming language

Source code in the picat programming language

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)).

  

You may also check:How to resolve the algorithm Constrained genericity step by step in the E programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Necromantus programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Scheme programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Input loop step by step in the TypeScript programming language