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.


 
' FreeBASIC v1.05.0 win64
 
Function SumBinaryDigits(number As Integer) As Integer
  If number < 0 Then number = -number ' convert negative numbers to positive
  Var sum = 0
  While number > 0
    sum += number Mod 2
    number \= 2
  Wend
  Return sum
End Function
 
Function IsPrime(number As Integer) As Boolean
  If number <= 1 Then 
    Return false 
  ElseIf number <= 3 Then
    Return true 
  ElseIf number Mod 2 = 0 OrElse number Mod 3 = 0 Then
    Return false
  End If
  Var i = 5
  While i * i <= number
    If number Mod i = 0 OrElse number Mod (i + 2) = 0 Then
      Return false
    End If
    i += 6
  Wend
  Return True
End Function
 
Function IsPernicious(number As Integer) As Boolean
  Dim popCount As Integer = SumBinaryDigits(number)
  Return IsPrime(popCount)
End Function
 
Dim As Integer n = 1, count = 0
Print "The following are the first 25 pernicious numbers :"
Print
 
Do
  If IsPernicious(n) Then
    Print Using "###"; n;
    count += 1
  End If
  n += 1
Loop Until count = 25
 
Print : Print
Print "The pernicious numbers between 888,888,877 and 888,888,888 inclusive are :"
Print
For n = 888888877 To 888888888
  If IsPernicious(n) Then Print Using "##########"; n;
Next
Print : Print
Print "Press any key to exit the program"
Sleep
End
 