×

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

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.