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.
Input: N = 25
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
As 2 itself is a prime number, closest prime number is 2. So difference is 0.
- 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:
- Pair of prime numbers with a given sum and minimum absolute difference
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Minimum absolute difference between N and a power of 2
- Co-prime pair with given sum minimum difference
- Minimum prime number operations to convert A to B
- Absolute difference between the first X and last X Digits of N
- Minimize the absolute difference of sum of two subsets
- Maximum absolute difference in an array
- Absolute Difference of even and odd indexed elements in an Array
- Absolute difference between sum and product of roots of a quartic equation
- Absolute Difference of all pairwise consecutive elements in an array
- Largest subset where absolute difference of any two element is a power of 2
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.