Minimum absolute difference of a number and its closest prime
Last Updated :
27 May, 2022
Given a positive integer N, the task is to find the absolute difference of N and the prime number closest to N .
Note: The closest prime to N can be either less than, equal to or greater than N.
Examples:
Input: N = 25
Output: 2
For N = 25
Closest prime greater than 25 is 29. So difference is 4.
Closest prime less than 25 is 23. So difference is 2.
The minimum of these two is 2.
Input: N = 2
Output: 0
As 2 itself is a prime number, closest prime number is 2. So difference is 0.
Approach:
- If N is prime then print 0.
- Else, find the first prime number > N and note its difference with N.
- Then, find the first prime number < N and note its difference with N.
- And print the minimum of these two differences obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int N)
{
for ( int i = 2; i <= sqrt (N); i++) {
if (N % i == 0)
return false ;
}
return true ;
}
int getDifference( int N)
{
if (N == 0)
return 2;
else if (N == 1)
return 1;
else if (isPrime(N))
return 0;
int aboveN = -1, belowN = -1;
int n1;
n1 = N + 1;
while ( true ) {
if (isPrime(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1;
while ( true ) {
if (isPrime(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
int diff1 = aboveN - N;
int diff2 = N - belowN;
return min(diff1, diff2);
}
int main()
{
int N = 25;
cout << getDifference(N) << endl;
return 0;
}
|
Java
class GFG {
static boolean isPrime( int N)
{
for ( int i = 2 ; i <= Math.sqrt(N); i++) {
if (N % i == 0 )
return false ;
}
return true ;
}
static int getDifference( int N)
{
if (N == 0 )
return 2 ;
else if (N == 1 )
return 1 ;
else if (isPrime(N))
return 0 ;
int aboveN = - 1 , belowN = - 1 ;
int n1;
n1 = N + 1 ;
while ( true ) {
if (isPrime(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1 ;
while ( true ) {
if (isPrime(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
int diff1 = aboveN - N;
int diff2 = N - belowN;
return Math.min(diff1, diff2);
}
public static void main(String args[])
{
int N = 25 ;
System.out.println(getDifference(N));
}
}
|
Python3
from math import sqrt
def isPrime(N):
k = int (sqrt(N)) + 1
for i in range ( 2 , k, 1 ):
if (N % i = = 0 ):
return False
return True
def getDifference(N):
if (N = = 0 ):
return 2
elif (N = = 1 ):
return 1
elif (isPrime(N)):
return 0
aboveN = - 1
belowN = - 1
n1 = N + 1
while ( True ):
if (isPrime(n1)):
aboveN = n1
break
else :
n1 + = 1
n1 = N - 1
while ( True ):
if (isPrime(n1)):
belowN = n1
break
else :
n1 - = 1
diff1 = aboveN - N
diff2 = N - belowN
return min (diff1, diff2)
if __name__ = = '__main__' :
N = 25
print (getDifference(N))
|
C#
using System;
class GFG {
static bool isPrime( int N)
{
for ( int i = 2; i <= Math.Sqrt(N); i++) {
if (N % i == 0)
return false ;
}
return true ;
}
static int getDifference( int N)
{
if (N == 0)
return 2;
else if (N == 1)
return 1;
else if (isPrime(N))
return 0;
int aboveN = -1, belowN = -1;
int n1;
n1 = N + 1;
while ( true ) {
if (isPrime(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1;
while ( true ) {
if (isPrime(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
int diff1 = aboveN - N;
int diff2 = N - belowN;
return Math.Min(diff1, diff2);
}
public static void Main()
{
int N = 25;
Console.WriteLine(getDifference(N));
}
}
|
PHP
<?php
function isPrime( $N )
{
for ( $i = 2; $i <= sqrt( $N ); $i ++)
{
if ( $N % $i == 0)
return false;
}
return true;
}
function getDifference( $N )
{
if ( $N == 0)
return 2;
else if ( $N == 1)
return 1;
else if (isPrime( $N ))
return 0;
$aboveN = -1; $belowN = -1;
$n1 = $N + 1;
while (true)
{
if (isPrime( $n1 ))
{
$aboveN = $n1 ;
break ;
}
else
$n1 ++;
}
$n1 = $N - 1;
while (true)
{
if (isPrime( $n1 ))
{
$belowN = $n1 ;
break ;
}
else
$n1 --;
}
$diff1 = $aboveN - $N ;
$diff2 = $N - $belowN ;
return min( $diff1 , $diff2 );
}
$N = 25;
echo getDifference( $N ) . "\n" ;
|
Javascript
function isPrime(N)
{
for (let i = 2; i <= Math.sqrt(N); i++)
{
if (N % i == 0)
return false ;
}
return true ;
}
function getDifference(N)
{
if (N == 0)
return 2;
else if (N == 1)
return 1;
else if (isPrime(N))
return 0;
let aboveN = -1;
let belowN = -1;
let n1 = N + 1;
while ( true )
{
if (isPrime(n1))
{
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1;
while ( true )
{
if (isPrime(n1))
{
belowN = n1;
break ;
}
else
n1--;
}
let diff1 = aboveN - N;
let diff2 = N - belowN;
return Math.min(diff1, diff2);
}
let N = 25;
document.write(getDifference(N) + "<br>" );
|
Time Complexity: O(N*sqrt(N)), where N represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...