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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 minimum number of merge operations to make an array palindrome
- Find the minimum number of elements that should be removed to make an array good
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- 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
- Find third number such that sum of all three number becomes prime
- Minimum prime number operations to convert A to B
- Minimum number of elements to be removed to make XOR maximum
- Minimum number of letters needed to make a total of n
- Minimum number of given moves required to make N divisible by 25
- Minimum absolute difference of a number and its closest prime
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.