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
- Minimize sum of prime numbers added to make an array non-decreasing
- Minimum number to be added to all digits of X to make X > Y
- 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
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Count of prime factors of N to be added at each step to convert N to M
- Minimum elements to be added in a range so that count of elements is divisible by K
- Minimum value to be added to X such that it is at least Y percent of N
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum numbers with one's place as 9 to be added to get N
- Minimum value of X that can be added to N to minimize sum of the digits to ≤ K
- Minimum insertions to make a Co-prime array
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Minimum changes required to make all Array elements Prime
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Find coordinates of a prime number in a Prime Spiral
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Find amount to be added to achieve target ratio in a given mixture
- Find the integers that doesnot ends with T1 or T2 when squared and added X
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.