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.

**Examples:**

Input:N = 10

Output:1

Explanation:

1 is the minimum number to be added to N such that 10 + 1 = 11 is a prime number

Input:N = 20

Output:3

**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:

## C++

`// C++ program to find the minimum ` `// number to be added to N to ` `// make it a prime number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if a given number ` `// is a prime or not ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Base cases ` ` ` `if` `(n <= 1) ` ` ` `return` `false` `; ` ` ` `if` `(n <= 3) ` ` ` `return` `true` `; ` ` ` ` ` `// This is checked so that we can skip ` ` ` `// middle five numbers in below loop ` ` ` `if` `(n % 2 == 0 || n % 3 == 0) ` ` ` `return` `false` `; ` ` ` ` ` `// For all the remaining numbers, check if ` ` ` `// any number is a factor if the number ` ` ` `// or not ` ` ` `for` `(` `int` `i = 5; i * i <= n; i = i + 6) ` ` ` `if` `(n % i == 0 || n % (i + 2) == 0) ` ` ` `return` `false` `; ` ` ` ` ` `// If none of the above numbers are the ` ` ` `// factors for the number, then the ` ` ` `// given number is prime ` ` ` `return` `true` `; ` `} ` ` ` `// Function to return the smallest ` `// number to be added to make a ` `// number prime ` `int` `findSmallest(` `int` `N) ` `{ ` ` ` ` ` `// Base case ` ` ` `if` `(N == 0) ` ` ` `return` `2; ` ` ` `if` `(N == 1) ` ` ` `return` `1; ` ` ` ` ` `int` `prime = N, counter = 0; ` ` ` `bool` `found = ` `false` `; ` ` ` ` ` `// Loop continuously until isPrime returns ` ` ` `// true for a number greater than n ` ` ` `while` `(!found) { ` ` ` `if` `(isPrime(prime)) ` ` ` `found = ` `true` `; ` ` ` `else` `{ ` ` ` ` ` `// If the number is not a prime, then ` ` ` `// increment the number by 1 and the ` ` ` `// counter which stores the number ` ` ` `// to be added ` ` ` `prime++; ` ` ` `counter++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 10; ` ` ` ` ` `cout << findSmallest(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the minimum ` `// number to be added to N to ` `// make it a prime number ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to check if a given number ` `// is a prime or not ` `static` `boolean` `isPrime(` `int` `n) ` `{ ` ` ` `// Base cases ` ` ` `if` `(n <= ` `1` `) ` ` ` `return` `false` `; ` ` ` `if` `(n <= ` `3` `) ` ` ` `return` `true` `; ` ` ` ` ` `// This is checked so that we can skip ` ` ` `// middle five numbers in below loop ` ` ` `if` `(n % ` `2` `== ` `0` `|| n % ` `3` `== ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `// For all the remaining numbers, check if ` ` ` `// any number is a factor if the number ` ` ` `// or not ` ` ` `for` `(` `int` `i = ` `5` `; i * i <= n; i = i + ` `6` `) ` ` ` `if` `(n % i == ` `0` `|| n % (i + ` `2` `) == ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `// If none of the above numbers are the ` ` ` `// factors for the number, then the ` ` ` `// given number is prime ` ` ` `return` `true` `; ` `} ` ` ` `// Function to return the smallest ` `// number to be added to make a ` `// number prime ` `static` `int` `findSmallest(` `int` `N) ` `{ ` ` ` ` ` `// Base case ` ` ` `if` `(N == ` `0` `) ` ` ` `return` `2` `; ` ` ` `if` `(N == ` `1` `) ` ` ` `return` `1` `; ` ` ` ` ` `int` `prime = N, counter = ` `0` `; ` ` ` `boolean` `found = ` `false` `; ` ` ` ` ` `// Loop continuously until isPrime returns ` ` ` `// true for a number greater than n ` ` ` `while` `(!found) { ` ` ` `if` `(isPrime(prime)) ` ` ` `found = ` `true` `; ` ` ` `else` `{ ` ` ` ` ` `// If the number is not a prime, then ` ` ` `// increment the number by 1 and the ` ` ` `// counter which stores the number ` ` ` `// to be added ` ` ` `prime++; ` ` ` `counter++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `10` `; ` ` ` ` ` `System.out.print(findSmallest(N)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the minimum ` `# number to be added to N to ` `# make it a prime number ` ` ` `# Function to check if a given number ` `# is a prime or not ` `def` `isPrime(n): ` ` ` ` ` `# Base cases ` ` ` `if` `(n <` `=` `1` `): ` ` ` `return` `False` ` ` `if` `(n <` `=` `3` `): ` ` ` `return` `True` ` ` ` ` `# This is checked so that we can skip ` ` ` `# middle five numbers in below loop ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `or` `n ` `%` `3` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `# For all the remaining numbers, check if ` ` ` `# any number is a factor if the number ` ` ` `# or not ` ` ` `i ` `=` `5` ` ` `while` `(i ` `*` `i <` `=` `n ): ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `or` `n ` `%` `(i ` `+` `2` `) ` `=` `=` `0` `): ` ` ` `return` `False` ` ` `i ` `+` `=` `6` ` ` ` ` `# If none of the above numbers are the ` ` ` `# factors for the number, then the ` ` ` `# given number is prime ` ` ` `return` `True` ` ` `# Function to return the smallest ` `# number to be added to make a ` `# number prime ` `def` `findSmallest(N): ` ` ` ` ` `# Base case ` ` ` `if` `(N ` `=` `=` `0` `): ` ` ` `return` `2` ` ` `if` `(N ` `=` `=` `1` `): ` ` ` `return` `1` ` ` ` ` `prime , counter ` `=` `N, ` `0` ` ` `found ` `=` `False` ` ` ` ` `# Loop continuously until isPrime returns ` ` ` `# true for a number greater than n ` ` ` `while` `(` `not` `found): ` ` ` `if` `(isPrime(prime)): ` ` ` `found ` `=` `True` ` ` `else` `: ` ` ` ` ` `# If the number is not a prime, then ` ` ` `# increment the number by 1 and the ` ` ` `# counter which stores the number ` ` ` `# to be added ` ` ` `prime ` `+` `=` `1` ` ` `counter ` `+` `=` `1` ` ` `return` `counter ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `10` ` ` ` ` `print` `(findSmallest(N)) ` ` ` `# This code is contributed by chitranayal ` ` ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the minimum ` `// number to be added to N to ` `// make it a prime number ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check if a given number ` `// is a prime or not ` `static` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Base cases ` ` ` `if` `(n <= 1) ` ` ` `return` `false` `; ` ` ` `if` `(n <= 3) ` ` ` `return` `true` `; ` ` ` ` ` `// This is checked so that we can skip ` ` ` `// middle five numbers in below loop ` ` ` `if` `(n % 2 == 0 || n % 3 == 0) ` ` ` `return` `false` `; ` ` ` ` ` `// For all the remaining numbers, check if ` ` ` `// any number is a factor if the number ` ` ` `// or not ` ` ` `for` `(` `int` `i = 5; i * i <= n; i = i + 6) ` ` ` `if` `(n % i == 0 || n % (i + 2) == 0) ` ` ` `return` `false` `; ` ` ` ` ` `// If none of the above numbers are the ` ` ` `// factors for the number, then the ` ` ` `// given number is prime ` ` ` `return` `true` `; ` `} ` ` ` `// Function to return the smallest ` `// number to be added to make a ` `// number prime ` `static` `int` `findSmallest(` `int` `N) ` `{ ` ` ` ` ` `// Base case ` ` ` `if` `(N == 0) ` ` ` `return` `2; ` ` ` `if` `(N == 1) ` ` ` `return` `1; ` ` ` ` ` `int` `prime = N, counter = 0; ` ` ` `bool` `found = ` `false` `; ` ` ` ` ` `// Loop continuously until isPrime returns ` ` ` `// true for a number greater than n ` ` ` `while` `(!found) { ` ` ` `if` `(isPrime(prime)) ` ` ` `found = ` `true` `; ` ` ` `else` `{ ` ` ` ` ` `// If the number is not a prime, then ` ` ` `// increment the number by 1 and the ` ` ` `// counter which stores the number ` ` ` `// to be added ` ` ` `prime++; ` ` ` `counter++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 10; ` ` ` ` ` `Console.Write(findSmallest(N)); ` `} ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

**Output:**

1

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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.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.