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]