Given an integer **N**, the task is to find the minimum possible integer **X** such that **X % M = 1** for all **M** from the range **[2, N]**

**Examples:**

Input:N = 5

Output:61

61 % 2 = 1

61 % 3 = 1

61 % 4 = 1

61 % 5 = 1

Input:N = 2

Output:3

**Approach:** Find the lcm of all the integers from the range **[2, N]** and store it in a variable **lcm**. Now we know that **lcm** is the smallest number which is divisible by all the elements from the range **[2, N]** and to make it leave a remainder of **1** on every division, just add **1** to it i.e. **lcm + 1** is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the smallest number ` `// which on dividing with any element from ` `// the range [2, N] leaves a remainder of 1 ` `long` `getMinNum(` `int` `N) ` `{ ` ` ` `// Find the LCM of the elements ` ` ` `// from the range [2, N] ` ` ` `int` `lcm = 1; ` ` ` `for` `(` `int` `i = 2; i <= N; i++) ` ` ` `lcm = ((i * lcm) / (__gcd(i, lcm))); ` ` ` ` ` `// Return the required number ` ` ` `return` `(lcm + 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` ` ` `cout << getMinNum(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the smallest number ` ` ` `// which on dividing with any element from ` ` ` `// the range [2, N] leaves a remainder of 1 ` ` ` `static` `long` `getMinNum(` `int` `N) ` ` ` `{ ` ` ` `// Find the LCM of the elements ` ` ` `// from the range [2, N] ` ` ` `int` `lcm = ` `1` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= N; i++) ` ` ` `lcm = ((i * lcm) / (__gcd(i, lcm))); ` ` ` ` ` `// Return the required number ` ` ` `return` `(lcm + ` `1` `); ` ` ` `} ` ` ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `System.out.println(getMinNum(N)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `gcd ` ` ` `# Function to return the smallest number ` `# which on dividing with any element from ` `# the range [2, N] leaves a remainder of 1 ` `def` `getMinNum(N) : ` ` ` ` ` `# Find the LCM of the elements ` ` ` `# from the range [2, N] ` ` ` `lcm ` `=` `1` `; ` ` ` `for` `i ` `in` `range` `(` `2` `, N ` `+` `1` `) : ` ` ` `lcm ` `=` `((i ` `*` `lcm) ` `/` `/` `(gcd(i, lcm))); ` ` ` ` ` `# Return the required number ` ` ` `return` `(lcm ` `+` `1` `); ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `5` `; ` ` ` ` ` `print` `(getMinNum(N)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the smallest number ` ` ` `// which on dividing with any element from ` ` ` `// the range [2, N] leaves a remainder of 1 ` ` ` `static` `long` `getMinNum(` `int` `N) ` ` ` `{ ` ` ` `// Find the LCM of the elements ` ` ` `// from the range [2, N] ` ` ` `int` `lcm = 1; ` ` ` `for` `(` `int` `i = 2; i <= N; i++) ` ` ` `lcm = ((i * lcm) / (__gcd(i, lcm))); ` ` ` ` ` `// Return the required number ` ` ` `return` `(lcm + 1); ` ` ` `} ` ` ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 5; ` ` ` `Console.WriteLine(getMinNum(N)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the smallest number ` `// which on dividing with any element from ` `// the range [2, N] leaves a remainder of 1 ` `function` `getMinNum(` `$N` `) ` `{ ` ` ` `// Find the LCM of the elements ` ` ` `// from the range [2, N] ` ` ` `$lcm` `= 1; ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$N` `; ` `$i` `++) ` ` ` `$lcm` `= ((` `$i` `* ` `$lcm` `) / (__gcd(` `$i` `, ` `$lcm` `))); ` ` ` ` ` `// Return the required number ` ` ` `return` `(` `$lcm` `+ 1); ` `} ` ` ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` `return` `__gcd(` `$b` `, ` `$a` `% ` `$b` `); ` `} ` ` ` `// Driver code ` ` ` `$N` `= 5; ` `echo` `(getMinNum(` `$N` `)); ` ` ` `// This code has been contributed by ajit.... ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

61

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:

- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Smallest subarray whose product leaves remainder K when divided by size of the array
- Minimum positive integer divisible by C and is not in range [A, B]
- Find Quotient and Remainder of two integer without using division operators
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Smallest number to make Array sum at most K by dividing each element
- Minimum decrements to make integer A divisible by integer B
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Smallest number dividing minimum number of elements in the Array
- Smallest number dividing minimum number of elements in the array | Set 2
- Biggest integer which has maximum digit sum in range from 1 to n
- Find an integer in the given range that satisfies the given conditions
- Maximum positive integer divisible by C and is in the range [A, B]
- Count of index pairs in array whose range product is a positive integer
- Smallest integer > 1 which divides every element of the given array
- Find an integer X which is divisor of all except exactly one element in an array
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Minimum distance between the maximum and minimum element of a given Array

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.