×

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's use examples to grasp it better.

Example:

int a = 5717

The cut is applied at index 1, and the results are 5 and 717.

In this case, 717 is not a prime number, while 5 is a prime Integer (717 is divisible by 3). As a result, we obtained the number 717, which is not prime; as a result, the cut at index 1 cannot be regarded as the number 5717's prime point.

The values 57 and 17 are obtained when we now apply cut on index 2.

In this case, 17 is a prime number, whereas 57 is not a prime because it may be divided by 3. As a result, we obtained the number 57, which is not a prime number; as a result, the cut at index 2 could be regarded as the number 5717's prime point.

Using cut at index 3, we now obtain the numbers 571 and 7.

In this case, both 571 and 7 are prime numbers. As a result, the cutoff at index 3 could be considered one of the 5717's prime points.

As a result, we can observe that the number 5717 only received a single prime point (index 3).

Now let's take the example of 67793

int a = 67793

Cut at index 1 produces the numbers 6 and 7793.

In this case, 7793 is a prime number, whereas 6 is not a prime because it may be divided by 2. As a result, we obtained the number 6, which is not a prime number; as a result, the cut at index 1 cannot be regarded as a prime point of the number 67793.

The values 67 and 793 are obtained when we now apply cut to index 2.

In this case, 793 is not a prime number, whereas 67 is a prime number. Thus, we obtained the nonprime number 67; as a result, the cut at index 2 cannot be regarded as the prime point of the number 67793.

In this case, 93 is not a prime number, while 677 is a prime number (93 is divisible by 3). As a result, the cut at index 3 cannot be regarded as the number 67793's prime point.

The digits 6779 and 3 are obtained when we now apply to cut at index 4.

In this case, both 3 and 6779 are prime numbers. As a result, the cutting at index 4 could be regarded as the number 67793's prime point.

Algorithm

Step1: Choose a number n in step one.

Step 2: Use cuts to divide the number into two halves at each index.

Step 3: Determine whether or not each component is prime.

Step 4: That position is the prime point of a number n if each part is a prime integer; otherwise, it is not.

Step 5: Carry out step 2 again for the other index.

Java Program to Find Prime Points

Let's see how the abovementioned method was implemented.

PrimePoints.java

// Importing arll the required packages
import java.util.*;  
  
public class PrimePoints  
{  
// counting the no.of digits using this method
public int noofdigits(int n)  
{  
int c = 0;  
while (n > 0)  
{  
c = c + 1;  
n = n / 10;  
}  
return c;  
}  
  
// This method checks whether the number is prime or not
// The method returns 0 if the number is prime and 1 if the number is not prime  
public int Primernot(int n)  
{  
// Edges case for prime or not
if (n <= 1)  
{  
return 0;  
}  
  
if (n <= 3)  
{  
return 1;  
}  
  
// By using this, we can skip checking whether the middle five numbers are prime or not in this loop. 
if (n % 3 == 0 || n % 2 == 0)  
{  
return 0;  
}  
  
for (int j = 5; j * j <= n; j = j + 6)  
{  
if (n % j == 0 || n % (j + 2) == 0)  
{  
return 0;  
}  
  
}  
  
return 1;  
  
}  
  
// This method displays the prime point  
public ArrayList PrimePoints(int n)  
{  
// Object creation for the Array list using the array data structure. 
ArrayList arr = new ArrayList();  
  
// Finding the number of digits in the number  
int co = noofdigits(n);  
  
// if the count of the digits is one, we cannot split the number, so we return it as it is. 
if (co == 1)  
{  
return arr;  
}  
  
// identifying each left and right pair showing the prime points appropriately
// Leaving off the first and last index points, this is due to   
boolean flag = false;  
for (int j = 1; j < c; j++)  
{  
// Finding the left number 
int lt = n / ((int)Math.pow(10, co - j));  
  
// Finding the right number
int rt = n % ((int)Math.pow(10, co - j));  
  
// Conditions that show the number is the prime point  
if (Primernot(lt) == 1 && Primernot(rt) == 1)  
{  
arr.add(j);  
flag = true;  
}  
}  
  
// If no prime number is found 
if (flag == false)  
{  
System. out.print("There are no prime points of the given number " + n);  
}  
  
return arr;  
}  
  
// main method starts here
public static void main (String argvs[])  
{  
int n = 67793;  
  
// Object creation of the PrimePoints class
PrimePoints obj = new PrimePoints();  
  
// Calling the prime method points in the number 
ArrayList arr = obj.PrimePoints(n);  
  
// Finding the no.of Prime Points of the number  
int size = arr.size();   
  
if(size == 0)     
{  
System.out.print("There are no prime points of the given number " + n);  
}  
else  
{  
System.out.println("The no.of prime points of the given number " + n  + " are :");  
System.out.println(arr + "\n");  
}  
  
n = 5717;  
  
arr = obj.PrimePoints(n);  
  
// Finding the no.of Prime Points of the number   
size = arr.size();   
  
if(size == 0)     
{  
System.out.print("There are no prime points of the given number " + n);  
}  
else  
{  
System.out.println("The no.of prime points of the given number " + n  + " are:");  
System.out.println(arr + "\n");  
}  
  
n = 2317;  
  
arr = obj.PrimePoints(n);  
  
// Finding the no.of Prime Points of the number   
size = arr.size();   
  
if(size == 0)     
{  
System.out.print("There are no prime points of the given number " + n);  
}  
else  
{  
System.out.println("The no.of prime points of the given number " + n  + " are:");  
System.out.println(arr + "\n");  
}  
}  
}  

Output:

The no.of prime points of the given number 67793 are:
[4]
The no.of prime points of the given number 5717 are:
[3]
The no.of prime points of the given number 2317 are:
[1, 2]

PrimePoints1.java

// Importing arrl the required packages
import java.util.*;  


  


public class PrimePoints1  


{  


public static boolean Primes[] = new boolean[6880];  


  


public void Prime()  


{  


int s = Primes.length - 1;  


  


for(int i= 0; i <= s; i++)  


{  


Primes[i] = true;  


}  


  


for(int j = 2; j * j <= s; j++)  


{  


// if prime[i] has not changed, then it indicates that we have found a prime  


if(Primes[j] == true)  


{  


// Finding all multiples of the number 


for(int i = j * j; i <= s; i += j)  


{  


Primes[i] = false;  


}  


}  


}  




}  
public boolean Primernot(int num)  


{  


return Primes[num];  


}  


  


// This method displays the prime points of a number  
public ArrayList displayPrimePoints(int num)  
{  
// Object creation for the Array list using the array data structure. 
ArrayList arr = new ArrayList();  
String string = Integer.toString(n);  
// Finding the no.of digits in a number n
int c = string.length();  
if (c == 1)  


{  


return arr;  


}  


  


// identifying each left and right pair showing the prime points appropriately


// Leaving off the first and last index points, this is due to   


for (int j = 1; j < cnt; j++)  


{  


// Finding the left part  


String s1 = str.substring(0, j);  


  


// Finding the Right part 


String s2 = str.substring(j);  


  


// Finding the left number   


int n1 = Integer.parseInt(s1);  


  


// Finding the left number  


int n2 = Integer.parseInt(s2);  
if (Primernot(n1) && Primernot(n2))  


{  
arr.add(j);  


}  
}  
return arr;  
}  
// main method starts here 


public static void main (String argvs[])  
{  
int n = 67793;  
// Object creation of the PrimePoints1 class  
obj.Prime();  
// Calling the prime method points in the number  
ArrayList arr = obj.displayPrimePoints(n);  
// Finding the no. of Prime Points of the number   
int s = arr.s();   


  


if(s == 0)     


{  


System.out.println("There are no prime points of the given number " + n);  


}  


else  


{  


System.out.println("The no.of prime points of the given number " + n  + " are:");  


System.out.println(arr + "\n");  


}  


  


n = 5717;  


  


arr = obj.displayPrimePoints(n);  


  


// Finding the no.of Prime Points of the number   


s = arr.s();   


  


if(s == 0)     


{  


System.out.println("There are no prime points of the given number " + n);  


}  


else  


{  


System.out.println("The no.of prime points of the given number " + n  + " are:");  


System.out.println(arr + "\n");  


}  


  


n = 2317;  


  


arr = obj.displayPrimePoints(n);  


  


// Finding the no.of Prime Points of the number   


s = arr.s();   


  


if(s == 0)     


{  


System.out.println("There are no prime points of the given number " + n);  


}  


else  


{  


System.out.println("The no.of prime points of the given number " + n  + " are:");  


System.out.println(arr + "\n");  


}  


}  


}  

Output:

The no. of prime points of the given number 67793 are:
[4]
The no.of prime points of the given number 5717 are:
[3]
The no.of prime points of the given number 2317 are:
[1, 2]

Related Topics

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

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...

4 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.