×

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

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.