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.



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

22   is a pernicious number.



Task
 display the first   25   pernicious numbers   (in decimal).
 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
 Sequence   A052294 pernicious numbers on The On-Line Encyclopedia of Integer Sequences.
 Rosetta Code entry   population count, evil numbers, odious numbers.


 
EnableExplicit
 
Procedure.i SumBinaryDigits(Number)
  If Number < 0 : number = -number : EndIf; convert negative numbers to positive
  Protected sum = 0
  While Number > 0
    sum + Number % 2
    Number / 2
  Wend
  ProcedureReturn sum
EndProcedure
 
Procedure.i IsPrime(Number)
  If Number <= 1 
    ProcedureReturn #False
  ElseIf Number <= 3 
    ProcedureReturn #True 
  ElseIf Number % 2 = 0 Or Number % 3 = 0
    ProcedureReturn #False
  EndIf
  Protected i = 5
  While i * i <= Number
    If Number % i = 0 Or Number % (i + 2) = 0
      ProcedureReturn #False
    EndIf
    i + 6
  Wend
  ProcedureReturn #True
EndProcedure
 
Procedure.i IsPernicious(Number)
  Protected popCount = SumBinaryDigits(Number)
  ProcedureReturn Bool(IsPrime(popCount))
EndProcedure
 
Define n = 1, count = 0
If OpenConsole()
  PrintN("The following are the first 25 pernicious numbers :")
  PrintN("")
  Repeat
    If IsPernicious(n)
      Print(RSet(Str(n), 3))
      count + 1
    EndIf
    n + 1
  Until count = 25
  PrintN("")
  PrintN("")
  PrintN("The pernicious numbers between 888,888,877 and 888,888,888 inclusive are : ")
  PrintN("")
  For n = 888888877 To 888888888
    If IsPernicious(n)
      Print(RSet(Str(n), 10))
    EndIf
  Next
  PrintN("")
  PrintN("")
  PrintN("Press any key to close the console")
  Repeat: Delay(10) : Until Inkey() <> ""
  CloseConsole()
EndIf
 