×

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment of the new number is a prime number. We must also be careful to reduce the number of splits. The minimum split is 1 because we are taking the input string as a whole if it represents a prime number. The numbers discovered must fall within the range of 1 to 10 ^ 6. Let's break it down with the aid of some examples.

In simple words, The aim is to determine the smallest number of segments into which a string representing a huge number, str, can be divided, each segment being a prime number between 1 and 10 ^ 6.

Example 1:

Input: "13477317"

Output: 2

Reason: If we take 1 split, then we must take 13477317 as a whole, and 13477317 can be divided by 3. As a result, only one split is not feasible. After performing two splits, we can see that both 317 and 13477 are prime numbers. As a result, we obtained all the segments as prime numbers in just two splits.

Example 2:

Input: "17"

Output: 1

Reason: The number 13477317 is a prime number and can be explained by the fact that if we take 1 split, we must also take 17 as a whole. So, one split will be sufficient.

Filename: Examplee.java

//Java implementation of the above strategy
import java.util.*;
 
class Examplee{
  
//Function to determine whether 
//or not a String is a prime number.
static boolean checkPrime(String number)
{
    int num = Integer.valueOf(number);
    for (int i = 2; i * i <= num; i++)
        if ((num % i) == 0)
            return false;
    return true;
}
  
//A recursive method to determine
//the smallest number of segments that can
//be created from the given String 
//so that each one is a prime.
static int splitIntoPrimes(String number)
{
    // If the number is null
    if (number.length() == 0)
        return 0;
//check To determine whether a number is
//a prime number or not, 
//the prime function is invoked.
    if (number.length() <= 6 && checkPrime(number))
        return 1;
    else {
        int numLen = number.length();
        // A large number indicating the maximum
        int ans = 1000000;
        // Consider a minimum of 6 and a length
        // since the primes are less than 10 ^ 6
        for (int i = 1; i <= 6 && i <= numLen; i++) {
            if (checkPrime(number.substring(0, i))) {
                // Recursively call the function
                // to check for the remaining String
                int val = splitIntoPrimes(number.substring(i));
                if (val != -1) {
                    // Evaluating minimum splits
                    // into Primes for the suffix
                    ans = Math.min(ans, 1 + val);
                }
            }
        }
        // Checks if no combination is found
        if (ans == 1000000)
            return -1;
        return ans;
    }
}
// Driver code
public static void main(String[] args)
{
    System.out.print(splitIntoPrimes("13499315")+ "\n");
    System.out.print(splitIntoPrimes("43")+ "\n");
}
}

Output

Split the Number String into Primes in Java

Explanation

Input: str = “13499315”

Output: 3

Explanation: The number can be segmented as [13499, 31, 5]

Input: str = “43”

Output: 1

Explanation: The number can be segmented as [43]

Using Recursion

The idea is to take into account each prefix up to six digits (since the prime numbers are already known to be < 10 ^ 6) and verify if the number is prime or not. Recursively call the method to verify the string that is left if the input is a prime number. An appropriate message is displayed on the console if any combination fails to produce the desired arrangement.

Filename: SplitStringPrimes.java

// statement for importing
import java.util.* ;  
public class SplitStringPrimes  
{  
//The below method tells 
//if the string num is prime or not
public boolean isPrime(String num)  
{  
int no = Integer.valueOf(num) ;  
for (int j = 2; j * j <= no; j++)  
{  
if ((no % j) == 0)  
{  
//A factor is found that differs from 1 and 
//the number itself. Therefore, the response is false.
return false;  
}  
}  
//If the control comes to this point, the
// number is considered to be a prime number.
return true ;  
}  
//Using a recursive method, determine how many
// segments of the string str must be prime 
//in order for there to be a minimum number of segments.
public int splitIntoPrimes(String str)  
{  
//If the string str contains no numbers
if (str.length() == 0)  
{  
return 0 ;  
}  
  
//To determine whether a number is a prime number
// or not, the isPrime() method is called.
if (str.length() <= 6 && isPrime(str))  
{  
return 1 ;  
}  
  
else   
{  
int numLength = str.length() ;  
  
//A large number to indicate the maximum
int answer = 1000000 ;  
//As 10 7 is not a prime number and is the 
//highest possible input number, the most
// digits that can be present in a prime number are 6, (which is six.).
for (int j = 1; j <= 6 && j <= numLength; j++)   
{  
if (isPrime(str.substring(0, j)))   
{  
  
//Recursively call the method to check the remaining 
//characters in the string.
int value = splitIntoPrimes(str.substring(j)) ;  
if (value != -1)   
{  
//The minimum splits into the Primes are
 //calculated for the suffix.
answer = Math.min(answer, 1 + value) ;  
}  
}  
}  
//Verifying if the combination is present
if (answer == 1000000)  
{  
return -1 ;  
}  
return answer ;  
}  
}  
// main method  
public static void main(String[] argvs)  
{  
//creating a SplitStringPrimes object of this particular class
SplitStringPrimes obj = new SplitStringPrimes() ;  
String inputStr = "343" ;  
int ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
else  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
inputStr = "37";  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
else  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
inputStr = "44" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
else  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;    
inputStr = "367555" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
}  
}  

Output

Split the Number String into Primes in Java

Complexity Analysis

The given code has an O(N^5 / 2) time complexity, where it takes O(N^2) time to identify every conceivable combination iteratively. Since we additionally need to determine whether the integer is prime, this task has an O(N^1/2) time complexity. As a result, the code described above has an overall time complexity of O(N^2) * O(N^1 / 2) = O(N^5 / 2), where N is the total number of digits in the input string.

Using Iteration

Dynamic programming can be used to fix the issue through iteration.

We can create the array splitArr[] and use it, where splitArr[q] denotes the minimum number of splits needed to separate a prefix string of the length 'q' into its subdivision of primes.

The splitArr[] array is being filled in the way described below:

  • All of the input string's indices are iterated over in a loop.
  • Another loop is started from 1 to 6 for each index 'q' from the above loop to determine whether the substring from the (p + q)th index is a prime number or not.
  • The array splitArr[] can be updated as follows if a prime number is formed: splitArr [p + q] = min(splitArr[p + q], 1 + splitArr[p]);

The value at the last index, which has the fewest splits over the entire input string, is our answer when all of the values have been updated.

Filename: SplitStringPrimes1.java

// significant import statement  
import java.util.* ;  
  
public class SplitStringPrimes1  
{  
  
// determining whether the input string num is a
//whether or not a prime number using a method
public boolean isPrime(String num)  
{  
if(num.length() == 0)  
{  
return true ;  
}  
int nmbr = Integer.parseInt(num) ;  
for (int i = 2; i * i <= nmbr; i++)  
{  
//We have discovered a number that divides the nmbr value exactly. 
//That value is also not equivalent to either 1 or nmbr
//. As a result, nmbr is not a prime number.
if ((nmbr % i) == 0)  
{  
return false ;  
}  
}  
  
//if control is reached at this point, the
// number nmbr is a prime number.
return true ;  
}  
  
// a method used to determine the smallest number
// of splits necessary to ensure that each segment 
//of the input String that results from
// the split represents a prime number.
public int splitIntoPrimes(String num)  
{  
int numLength = num.length() ;  
  
//Declaring an array splitArr[] and 
//setting its initial value to -1.
int splitArr[] = new int[numLength + 1] ;  
Arrays.fill(splitArr, -1) ;  
  
// Constructing a table in the
// Bottom-up approach
for (int j = 1; j <= numLength; j++)   
{  
  
//To begin with, we determine whether or not an entire prefix is a prime number.
if (j <= 6 && isPrime(num.substring(0, j)))  
{  
splitArr[j] = 1 ;  
}  
  
//If it is possible to divide the string into prime numbers, 
//check to see if the remaining String from
// j to i is a prime number or not. 
//Calculate the minimum split 
//until i if the answer is yes.
if (splitArr[j] != -1)   
{  
for (int i = 1; i <= 6 && j + i <= numLength; i++)   
{  
  
//Verifying if the substring from j to i exists
//is the given integer a prime number?
if (isPrime(num.substring(j, j + i)))   
{  
  
//Update the split array if the value is prime.
if (splitArr[j + i] == -1)  
{  
splitArr[j + i] = 1 + splitArr[j] ;  
}  
else  
{  
splitArr[j + i] = Math.min(splitArr[j + i], 1 + splitArr[j]) ;  
}  
}  
}  
}  
}  
  
// Returning the fewest possible splits
// to get the full input String
return splitArr[numLength] ;  
}  
  
// main method  
public static void main(String[] argvs)  
{  
//constructing a SplitStringPrimes1 object of the specified class
SplitStringPrimes1 obj = new SplitStringPrimes1() ;  
  
String inputStr = "343" ;  
  
int ans = obj.splitIntoPrimes(inputStr) ;  
  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
inputStr = "37" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
  
inputStr = "44" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
  
inputStr = "367555" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
}  
}  

Output

Split the Number String into Primes in Java

Complexity Analysis

The given code has an O(N^3 / 2) time complexity, where it takes O(N) time to walk over all of the indices. Additionally, we must determine if the number is a prime, which has a time complexity of O(N^1 / 2). With N being the total number of digits in the input string, the whole time complexity of the above algorithm is, therefore O(N) * O(N^1 / 2) = O(N^3 / 2).

We are aware that in this problem, the prime numbers range from 1 to 10 ^ 6. This information can be used to improve the system even more. When we precompute the checks of the prime number and store it in an array, additional optimization is achievable.

Filename: SplitStringPrimes2.java

// significant import statement  
import java.util.* ;  
  
public class SplitStringPrimes2  
{  
  
//The Sieve of Eratosthenes approach is used
// to precompute all of the primes up to 1000000
// and keep them in the hash set.
public void retrievePrimesFromSeive(HashSet<String> hashSetPrm)  
{  
boolean []primeArr = new boolean[1000001] ;  
Arrays.fill(primeArr, true) ;  
primeArr[0] = primeArr[1] = false ;  
  
for (int j = 2; j * j <= 1000000; j++)   
{  
if (primeArr[j] == true)   
{  
//a multiple of a number is never a prime number.
// Let us take a prime number 2, for instance.
// However, because they are multiples of 2, 
//the numbers 4, 6, 8, 10, and so on are not prime numbers.
for (int i = j * j; i <= 1000000; i += j)  
{  
primeArr[i] = false ;  
}  
}  
}  
  
//Prime numbers are first converted to strings,
// and then they are added to the hash set.
for (int i = 2; i <= 1000000; i++)   
{  
if (primeArr[i] == true)  
{  
hashSetPrm.add(String.valueOf(i)) ;  
}  
}  
}  
  
//A technique that determines the smallest number 
//of splits necessary to ensure that each segment 
//of the input String that results from 
//the split represents a prime number.  
public int splitIntoPrimes(String num)  
{  
int numLength = num.length() ;  
  
//Define an array called splitArr[] and initialize it to -1.
int splitArr[] = new int[numLength + 1] ;  
Arrays.fill(splitArr, -1) ;  
//To keep the prime numbers in the
// hash set, invoke the sieve method.
HashSet<String> hashSetPrm = new HashSet<String>() ;  
retrievePrimesFromSeive(hashSetPrm) ;  
//A bottom-up approach is used for constructing
// the dynamic programming table.
for (int j = 1; j <= numLength; j++)   
{  
  
//If the prefix is a prime number, the hash
// set will contain the prefix if it was discovered.
if (j <= 6 && (hashSetPrm.contains(num.substring(0, j))))  
splitArr[j] = 1 ;  
//The remaining String from i to j should be checked
// if the given prefix can be divided into primes.
// Identify Prime. Determine the minimum split 
//until j if the answer is affirmative.
if (splitArr[j] != -1)   
{  
for (int i = 1; i <= 6 && j + i <= numLength; i++)   
{  
//determining whether or not the substring
// from j to i is a prime number
if (hashSetPrm.contains(num.substring(j, j + i)))   
{  
//If a prime is discovered, the split array must be changed.
if (splitArr[i + j] == -1)  
{  
splitArr[i + j] = 1 + splitArr[j] ;  
}  
else  
{  
splitArr[i + j] = Math.min(splitArr[i + j], 1 + splitArr[j]) ;  
}  
}  
}  
}  
}  
// Obtaining a result with the fewest splits
//for the entire input String
return splitArr[numLength] ;  
}  
// main method  
public static void main(String[] argvs)  
{  
//constructing a SplitStringPrimes2 object of the specified class
SplitStringPrimes2 obj = new SplitStringPrimes2() ;  
String inputStr = "343" ;  
int ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of split is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of split is not possible. \n") ;  
}  
inputStr = "37" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
  
inputStr = "44" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
inputStr = "367555" ;  
ans = obj.splitIntoPrimes(inputStr) ;  
if(ans != -1)  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is: " + ans + "\n") ;  
}  
else  
{  
System.out.print("For the number " + inputStr + ", the minimum number of splits is not possible. \n") ;  
}  
  
}  
}  

Output

Split the Number String into Primes in Java

Complexity Analysis

The time complexity of the preceding code, where N is the total number of digits present in the input string, is O(N), making it the most efficient method. The sieve has an O(N * log(N)) time complexity, but we may ignore it since it is only computed once. This means that, in contrast to previous programs, we can determine whether a number is a prime number or not in simply O(1) time.


Related Topics

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

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

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.