Given an integer N, the task is to find the minimum number K to be added to N such that N + K becomes a prime number.
Input: N = 10
1 is the minimum number to be added to N such that 10 + 1 = 11 is a prime number
Input: N = 20
Approach: The idea is to check whether the number is a prime or not by incrementing the value to be added K by 1 in each iteration. Therefore, the following steps can be followed to compute the answer:
- Initially, check whether the given number is prime or not. If it is, then the value to be added(K) is 0.
- Now, in every iteration, increment the value of N by 1 and check if the number is prime or not. Let the first value at which N becomes a prime is M. Then, the minimum value that needs to be added to make N prime is M – N.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the minimum number to be added to N to make it a power of K
- Minimum number to be added to all digits of X to make X > Y
- Find minimum number to be divided to make a number a perfect square
- Find minimum number of coins that make a given value
- Least number to be added to or subtracted from N to make it a Perfect Square
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Find the minimum number of elements that should be removed to make an array good
- Find minimum number of merge operations to make an array palindrome
- Find the minimum number of operations required to make all array elements equal
- Minimum number of swaps required to make a number divisible by 60
- Minimum divisor of a number to make the number perfect cube
- Find coordinates of a prime number in a Prime Spiral
- Minimum number of changes required to make the given array an AP
- Minimum prime number operations to convert A to B
- Minimum number of letters needed to make a total of n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.