Prime Points in JavaPoints that separates a number into two parts such that each part is a prime number, then those points become prime points. The task is to print all those prime points of a given number. Let's understand it through examples. Example 1: int n = 5717; Applying cut at the index 1, we get the numbers 5 and 717. Here, 5 is a prime number, and 717 is not a prime number (717 is divisible by 3). Thus, we got a number 717, which is not prime; hence, the cut at the index 1 cannot be counted as a prime point of the number 5717. Now we apply cut at the index 2, and we get the numbers 57 and 17. Here, 57 is not a prime number (57 is divisible by 3), and 17 is a prime number. Thus, we got a number 57, which is not prime; hence, the cut at the index 2 cannot be considered as a prime point of the number 5717. Now, we apply cut at the index 3, and we get the numbers 571 and 7. Here, 571 is a prime number, and 7 is also a prime number. Therefore, the cut at index 3 can be counted as a prime point of the number 5717. Thus, we see that we got only one prime point (index 3) for the number 5717. Example 2: int n = 67793; Applying cut at the index 1, we get the numbers 6, and 7793. Here, 6 is not a prime number (6 is divisible by 2) and 7793 is a prime number. Thus, we got a number 6, which is not prime; hence, the cut at the index 1 can not be counted as a prime point of the number 67793. Now we apply cut at the index 2, and we get the numbers 67 and 793. Here, 67 is a prime number, and 793 is not a prime number (793 is divisible by 13). Thus, we got a number 67, which is not prime; hence, the cut at the index 2 cannot be considered as a prime point of the number 67793. Now, we apply cut at index 3, and we get the numbers 677 and 93. Here, 677 is a prime number, and 93 is not a prime number (93 is divisible by 3). Hence, the cut at the index 3 cannot be considered as a prime point of the number 67793. Now, we apply cut at index 4, and we get the numbers 6779 and 3. Here, 6779 is a prime number, and 3 is also a prime number. Hence, the cut at index 4 can be considered as a prime point of the number 67793 AlgorithmStep 1: Take a number n. Step 2: Apply cut at every index to separate the number into two parts. Step 3: Check whether each part is a prime or not. Step 4: If every part is prime, then that index is the prime point of the number n; otherwise, not. Step 5: Repeat from step 2 for the other index too. Java Prime Points ProgramLet's see the implementation of the algorithm mentioned above. FileName: PrimePointEx.java Output: Prime points for the number 67793 are: [4] Prime points for the number 5717 are: [3] Prime points for the number 2317 are: [1, 2] Let's see another approach. In this approach, we will be using strings. Observe the following program. FileName: PrimePointEx1.java Output: Prime points for the number 67793 are: [4] Prime points for the number 5717 are: [3] Prime points for the number 2317 are: [1, 2]
Next TopicButterfly Pattern in Java
|