Pernicious Number in Java
If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to a prime number.
Pernicious Number Examples
- The number is not the pernicious number. The binary form of 1 contains only 1, which is not the prime number.
- Two is not a bad number. The binary form of 2 has 0 1 0. It contains 2 one’s in binary form, and one is not a prime integer.
- The number 3 is a pernicious number. This is due to the binary representation of 3 is 0 1 1. The Binary form of the 3 has 3 ones, and two is a prime number.
- 4 is not a Pernicious number. As the binary form of 4 is 1 0 0. In the binary form of 4 contains only one 1, and one is not a prime integer.
- 5 is a Pernicious number. In the binary form, the number 5 has 101 contains 2 ones, which is prime number, so the number 5 is a pernicious number.
Procedure for Determining the Pernicious Number
Step 1: Consider a number and get its binary form by dividing it by 2 indefinitely.
Step 2: Put the number's binary form in the array
Step 3: Create a counter and set it to zero.
Step 4: By using a loop, traverse the array's elements. If the element is 1, the counter value is increased by one; otherwise, it is not.
Step 5: Examine the value of both the counter and determine if it is odd or even.
Step 6: If the count value is an odd number, then the number chosen in step 1 is bad; else, it is not.
Java Program for finding the Pernicious number
The program below will describe whether or not the given number is a Pernicious number.
PerniciousNumberEx1.java
// this Program is for finding the pernicious number in java
//importing the required packages
import java.util.*;
import java.io.*;
public class PerniciousNumberEx1
{
private ArrayList<Integer> binaryRepresent(int num)
{
int reminder;
// an object was created for the class ArrayList
ArrayList<Integer> arrlist = new ArrayList();
// loop was used for finding the binary representation of the number
// let the number be num
while(num != 0)
{
//finding the remainder
reminder = num % 2;
// the value of the remainder is stored in remainder
// arraylist contains the elements
arrlist.add(reminder);
// the given number was divided by 2
// the reminder will become zero
num = num/ 2;
}
// the arrlist is returned
// the arrlist contains the binary value of the given number
// the binary form of the number num
return arrlist;
}
private boolean isPrime(int count)
{
// the value of f is used to count the prime factors
int f= 0;
// 1 is not a prime number
if(count== 1)
{
return false;
}
// iteration for determining the numerical counter's factors
// Take note that the loop begins at 2 and continues until count / 2
// hence 1, and the integer itself is omitted.
for(int i = 2; i <= count / 2; i++)
{
if(count % i == 0)
{
f = f + 1;
}
}
if(f >= 1)
{
//Unless the check reaches this point
//it signifies that we have discovered another component
//by eliminating 1 and the number itself.
//As a result, at least three major factors of the number. As a result,
//that number is not a prime number.
return false;
}
return true;
}
public boolean isThePernicious(int num)
{
// The binary form of the integer n is stored in the arrays list al.
ArrayList<Integer> arrlist = binaryRepresent(num);
int s = arrlist.size();
// computing size of the array list al
// for counting the numbers of 1's present in the array list, al
int c = 0;
for(int i = 0; i < s; i++)
{
int value = arrlist.get(i);
if(value == 1)
{
//Increase the value of c by one when a 1 appears in the list al.
c = c + 1;
}
}
// the condition for checking the given number
if(isPrime(c))
{
return true;
}
return false;
}
// main section of the program
public static void main(String argvs[])
{
// an object was created for the class PerniciousNumberEx1
PerniciousNumberEx1 object = new PerniciousNumberEx1();
// constructing an object of the form PerniciousNumberEx1
for(int i = 1; i <= 20; i++)
{
if(object.isThePernicious(i))
{
System.out.println("The number " +i+" is the pernicious number");
}
else
{
System.out.println("The number " +i+" is not the pernicious number");
}
}
}
}
Output
Number 1 is not pernicious number
Number 2 is not pernicious number
Number 3 is pernicious number
Number 4 is not pernicious number
Number 5 is pernicious number
Number 6 is pernicious number
Number 7 is pernicious number
Number 8 is not pernicious number
Number 9 is pernicious number
Number 10 is pernicious number
Number 11 is pernicious number
Number 12 is pernicious number
Number 13 is pernicious number
Number 14 is pernicious number
Number 15 is not pernicious number
Number 16 is not pernicious number
Number 17 is pernicious number
Number 18 is pernicious number
Number 19 is pernicious number
Number 20 is pernicious number