Prime points (Points that split a number into two primes)
Last Updated :
16 Oct, 2022
Given a n-digit number. Prime point is the index of the digit whose left and right side numbers
are prime. Print all the prime points of the number. If no prime point exists print -1.
Examples:
Input : 2317
Output : 1 2
Explanation : Left and right side numbers of index
point 1 are 2 and 17 respectively and
both are primes. Left and right side
numbers of index point 2 are 23 and 7
respectively and both are prime.
Input : 2418
Output : -1
Explanation : No index point has both the left
and right side numbers as prime.
Note: First and last index can never be a prime
point as they do not have left and right
side numbers pair.
Algorithm
Count number of digits of the given number, n.
If count == 1 || count == 2
print "Not Possible"
Else
{
For index points = 1 to (count - 1)
{
Calculate left number(L) and right number(R)
If both L and R are prime
print index point i
}
}
How to find L and R for an index point 'i'?
L = n / (10(count-i))
R = n % (10(count-i-1))
Prime number checking is based on optimized school method
C++
#include <bits/stdc++.h>
using namespace std;
int countDigits( int n)
{
int count = 0;
while (n > 0)
{
count++;
n = n/10;
}
return count;
}
int checkPrime( int n)
{
if (n <= 1)
return -1;
if (n <= 3)
return 0;
if (n%2 == 0 || n%3 == 0)
return -1;
for ( int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return -1;
return 0;
}
void printPrimePoints( int n)
{
int count = countDigits(n);
if (count==1 || count==2)
{
cout << "-1" ;
return ;
}
bool found = false ;
for ( int i=1; i<(count-1); i++)
{
int left = n / (( int ) pow (10,count-i));
int right = n % (( int ) pow (10,count-i-1));
if (checkPrime(left) == 0 &&
checkPrime(right) == 0)
{
cout << i << " " ;
found = true ;
}
}
if (found == false )
cout << "-1" ;
}
int main()
{
int n = 2317;
printPrimePoints(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countDigits( int n)
{
int count = 0 ;
while (n > 0 )
{
count++;
n = n / 10 ;
}
return count;
}
static int checkPrime( int n)
{
if (n <= 1 )
return - 1 ;
if (n <= 3 )
return 0 ;
if (n % 2 == 0 || n % 3 == 0 )
return - 1 ;
for ( int i = 5 ;
i * i <= n; i = i + 6 )
if (n % i == 0 ||
n % (i + 2 ) == 0 )
return - 1 ;
return 0 ;
}
static void printPrimePoints( int n)
{
int count = countDigits(n);
if (count == 1 || count == 2 )
{
System.out.print( "-1" );
return ;
}
boolean found = false ;
for ( int i = 1 ; i < (count - 1 ); i++)
{
int left = n / (( int )Math.pow( 10 ,
count - i));
int right = n % (( int )Math.pow( 10 ,
count - i - 1 ));
if (checkPrime(left) == 0 &&
checkPrime(right) == 0 )
{
System.out.print(i + " " );
found = true ;
}
}
if (found == false )
System.out.print( "-1" );
}
public static void main (String[] args)
{
int n = 2317 ;
printPrimePoints(n);
}
}
|
Python3
def countDigits(n):
count = 0
while (n > 0 ):
count + = 1
n = n / / 10
return count
def checkPrime(n):
if (n < = 1 ):
return - 1
if (n < = 3 ):
return 0
if (n % 2 = = 0 or n % 3 = = 0 ):
return - 1
i = 5
while i * i< = n:
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return - 1
i + = 6
return 0
def printPrimePoints(n):
count = countDigits(n)
if (count = = 1 or count = = 2 ):
print ( "-1" )
return
found = False
for i in range ( 1 ,(count - 1 )):
left = n / / ( pow ( 10 ,count - i))
right = n % ( pow ( 10 ,count - i - 1 ))
if (checkPrime(left) = = 0 and
checkPrime(right) = = 0 ):
print (i ,end = " " )
found = True
if (found = = False ):
print ( "-1" )
if __name__ = = "__main__" :
n = 2317
printPrimePoints(n)
|
C#
using System;
class GFG
{
static int countDigits( int n)
{
int count = 0;
while (n > 0)
{
count++;
n = n / 10;
}
return count;
}
static int checkPrime( int n)
{
if (n <= 1)
return -1;
if (n <= 3)
return 0;
if (n % 2 == 0 ||
n % 3 == 0)
return -1;
for ( int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return -1;
return 0;
}
static void printPrimePoints( int n)
{
int count = countDigits(n);
if (count == 1 ||
count == 2)
{
Console.Write( "-1" );
return ;
}
bool found = false ;
for ( int i = 1;
i < (count - 1); i++)
{
int left = n / (( int )Math.Pow(10,
count - i));
int right = n % (( int )Math.Pow(10,
count - i - 1));
if (checkPrime(left) == 0 &&
checkPrime(right) == 0)
{
Console.Write(i + " " );
found = true ;
}
}
if (found == false )
Console.Write( "-1" );
}
static public void Main ()
{
int n = 2317;
printPrimePoints(n);
}
}
|
PHP
<?php
function countDigits( $n )
{
$count = 0;
while ( $n > 0)
{
$count ++;
$n = (int)( $n / 10);
}
return $count ;
}
function checkPrime( $n )
{
if ( $n <= 1)
return -1;
if ( $n <= 3)
return 0;
if ( $n % 2 == 0 || $n % 3 == 0)
return -1;
for ( $i = 5; $i * $i <= $n ; $i = $i + 6)
if ( $n % $i == 0 || $n % ( $i + 2) == 0)
return -1;
return 0;
}
function printPrimePoints( $n )
{
$count = countDigits( $n );
if ( $count == 1 || $count == 2)
{
echo "-1" ;
return ;
}
$found = false;
for ( $i = 1; $i < ( $count - 1); $i ++)
{
$left = (int)( $n /
((int)pow(10, $count - $i )));
$right = $n % ((int)pow(10, $count - $i - 1));
if (checkPrime( $left ) == 0 &&
checkPrime( $right ) == 0)
{
echo $i , " " ;
$found = true;
}
}
if ( $found == false)
echo "-1" ;
}
$n = 2317;
printPrimePoints( $n );
?>
|
Javascript
<script>
function countDigits(n)
{
let count = 0;
while (n > 0)
{
count++;
n = Math.floor(n / 10);
}
return count;
}
function checkPrime(n)
{
if (n <= 1)
return -1;
if (n <= 3)
return 0;
if (n % 2 == 0 || n % 3 == 0)
return -1;
for (let i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return -1;
return 0;
}
function printPrimePoints(n)
{
let count = countDigits(n);
if (count == 1 || count == 2)
{
document.write( "-1" );
return ;
}
let found = false ;
for (let i = 1; i < (count - 1); i++)
{
let left = Math.floor(
n / (Math.pow(10, count - i)));
let right = n % (Math.pow(
10, count - i - 1));
if (checkPrime(left) == 0 &&
checkPrime(right) == 0)
{
document.write(i + " " );
found = true ;
}
}
if (found == false )
document.write( "-1" );
}
let n = 2317;
printPrimePoints(n);
</script>
|
Output:
1 2
Time complexity: O(log10n*sqrt(n))
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...