Skip to content
Related Articles

Related Articles

Improve Article

Pernicious number

  • Difficulty Level : Easy
  • Last Updated : 08 Apr, 2021

A pernicious number is a positive integer which has prime number of ones in its binary representation. The first pernicious number is 3 since 3 = (11)(in binary representation) and 1 + 1 = 2, which is a prime.
Properties of Pernicious Numbers :
1. There isn’t any pernicious number which is also power of 2 because powers of two in binary form are represented as a one followed by zeros. So, 1 is not considered as a prime number. 
2. Every number of the form 2^n  + 1 with n > 0 is a pernicious number as the number of ones in binary form is 2 which is prime. 
3. A number of the form 2^p  – 1 with prime p is a pernicious number known as a Mersenne number .
 

The idea to print first n Pernicious numbers is simple. 
Do following for every number from 1 to n. 
1) Count set bits in current number 
2) Print current number if count of set bits is prime. We use simple primality check for this purpose.
Here is the program to print first 25 pernicious number.
Below is the implementation of the above approach. 
 

C++




// CPP program to print first n pernicious numbers
#include <bits/stdc++.h>
using namespace std;
 
// function to check prime number
bool isPrime(int x)
{
    if (x < 2)
        return false;
    for (int i = 2; i < x; i++) {
        if (x % i == 0)
            return false;
    }
    return true;
}
 
// Prints first n Pernicious numbers
void printPernicious(int n)
{
    for (int i=1,count=0; count<n; i++) {
 
        // "__builtin_popcount(i)" count no
        // of ones in binary representation
        if (isPrime(__builtin_popcount(i))) {
            cout << i << " ";
             
            count++;
        }
    }
}
 
int main()
{
   int n = 25;
   printPernicious(n);
   return 0;
}

Java




// Java program to print first
// n pernicious numbers
import java.util.*;
 
class GFG {
    // function to count no of
    // ones in binary representation
    static int countSetBits(int n)
    {
        int count = 0;
         
        while (n > 0)
        {
            n &= (n - 1) ;
            count++;
        }
        return count;
    }
     
    // function to check prime number
    static boolean isPrime(int x)
    {
        if (x < 2)
            return false;
        for (int i = 2; i < x; i++) {
            if (x % i == 0)
                return false;
        }
        return true;
    }
     
    // Prints first n Pernicious numbers
    static void printPernicious(int n)
    {
        for (int i=1,count=0; count<n; i++) {
     
            if (isPrime(countSetBits(i))) {
                System.out.print( i + " ");
                 
                count++;
            }
        }
    }
 
    // Driver Code
    public static void main (String[] args) {
        int n = 25;
        printPernicious(n);
    }
}
 
// This code is contributed by Ansu Kumari

Python3




# Python program to print
# first n pernicious numbers
 
# function to check
# prime number
def isPrime(x):
     
    if x < 2:
        return False
     
    for i in range(2, x):
        if not x % i:
            return False
     
    return True
 
# Prints first n Pernicious
# numbers
def printPernicious(n):
 
    i, count = 1, 0
 
    while count < n:
 
        # "bin(i).count('1')" count
        # no of ones in binary
        # representation
        if (isPrime(bin(i).count('1'))):
            print(i, end=' ')
            count += 1
         
        i += 1
 
# Driver Code
n = 25
printPernicious(n)
 
# This code is contributed by Ansu Kumari

C#




// C#program to print first
// n pernicious numbers
using System;
 
class GFG
{
    // function to count no of
    // ones in binary representation
    static int countSetBits(int n)
    {
        int count = 0;
         
        while (n > 0)
        {
            n &= (n - 1) ;
            count++;
        }
        return count;
    }
     
    // function to check prime number
    static bool isPrime(int x)
    {
        if (x < 2)
            return false;
        for (int i = 2; i < x; i++) {
            if (x % i == 0)
                return false;
        }
        return true;
    }
     
    // Prints first n Pernicious numbers
    static void printPernicious(int n)
    {
        for (int i=1,count=0; count<n; i++) {
     
            if (isPrime(countSetBits(i))) {
                Console.Write( i + " ");
                 
                count++;
            }
        }
    }
 
    // Driver Code
    public static void Main ()
    {
        int n = 25;
        printPernicious(n);
    }
}
 
// This code is contributed by vt_m

PHP




<?php
// PHP program to print first
// n pernicious numbers
 
// function to check prime number
function isPrime($x)
{
    if ($x < 2)
        return false;
    for ($i = 2; $i < $x; $i++)
    {
        if ($x % $i == 0)
            return false;
    }
    return true;
}
//this function count no of
// ones in binary representation
function getBitCount($value)
{
 
    $count = 0;
    while($value)
    {
        $count += ($value & 1);
        $value = $value >> 1;
    }
 
    return $count;
}
 
// Prints first n Pernicious numbers
function printPernicious($n)
{
    for ($i = 1, $count = 0;
                $count < $n; $i++)
    {
 
        //count no of ones in
        // binary representation
        if (isPrime(getBitCount($i)))
        {
            echo $i." ";
             
            $count++;
        }
    }
}
 
// Driver code
$n = 25;
printPernicious($n);
 
// This code is contributed by mits
?>

Javascript




<script>
 
// JavaScript program to print first
// n pernicious numbers
 
    // function to count no of
    // ones in binary representation
    function countSetBits(n)
    {
        let count = 0;
           
        while (n > 0)
        {
            n &= (n - 1) ;
            count++;
        }
        return count;
    }
       
    // function to check prime number
    function isPrime(x)
    {
        if (x < 2)
            return false;
        for (let i = 2; i < x; i++) {
            if (x % i == 0)
                return false;
        }
        return true;
    }
       
    // Prints first n Pernicious numbers
    function printPernicious(n)
    {
        for (let i=1,count=0; count<n; i++) {
       
            if (isPrime(countSetBits(i))) {
                document.write( i + " ");
                   
                count++;
            }
        }
    }
 
// Driver code
 
        let n = 25;
        printPernicious(n);
 
</script>

Output : 
 

3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36

References : 
Wiki
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :