×

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

Related Topics

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Java Do While Loop

When we wish to test the exit condition at the end of the loop, we use a do-while loop. The do-while loop always executes its body at least once, because...

1 minute read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.