# Find all the possible remainders when N is divided by all positive integers from 1 to N+1

Given a large integer **N**, the task is to find all the possible remainders when **N** is divided by all the positive integers from **1** to **N + 1**.

**Examples:**

Input:N = 5

Output:0 1 2 5

5 % 1 = 0

5 % 2 = 1

5 % 3 = 2

5 % 4 = 1

5 % 5 = 0

5 % 6 = 5

Input:N = 11

Output:0 1 2 3 5 11

**Naive approach:** Run a loop from **1** to **N + 1** and return all the unique remainders found when dividing **N** by any integer from the range. But this approach is not efficient for larger values of **N**.

**Efficient approach:** It can be observed that one part of the answer will always contain numbers between **0** to **ceil(sqrt(n))**. It can be proven by running the naive algorithm on smaller values of **N** and checking the remainders obtained or by solving the equation **ceil(N / k) = x** or **x ≤ (N / k) < x + 1** where **x** is one of the remainders for all integers **k** when **N** is divided by **k** for **k** from **1** to **N + 1**.

The solution to the above inequality is nothing but integers **k** from **(N / (x + 1), N / x]** of length **N / x – N / (x + 1) = N / (x ^{2} + x)**. Therefore, iterate from

**k = 1**to

**ceil(sqrt(N))**and store all the unique

**N % k**. What if the above

**k**is greater than

**ceil(sqrt(N))**? They will always correspond to values

**0 ≤ x < ceil(sqrt(N))**. So, again start storing remainders from

**N / (ceil(sqrt(N)) – 1**to

**0**and return the final answer with all the possible remainders.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `long` `long` `int` `ll; ` ` ` `// Function to find all the distinct ` `// remainders when n is divided by ` `// all the elements from ` `// the range [1, n + 1] ` `void` `findRemainders(ll n) ` `{ ` ` ` ` ` `// Set will be used to store ` ` ` `// the remainders in order ` ` ` `// to eliminate duplicates ` ` ` `set<ll> vc; ` ` ` ` ` `// Find the remainders ` ` ` `for` `(ll i = 1; i <= ` `ceil` `(` `sqrt` `(n)); i++) ` ` ` `vc.insert(n / i); ` ` ` `for` `(ll i = n / ` `ceil` `(` `sqrt` `(n)) - 1; i >= 0; i--) ` ` ` `vc.insert(i); ` ` ` ` ` `// Print the contents of the set ` ` ` `for` `(` `auto` `it : vc) ` ` ` `cout << it << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll n = 5; ` ` ` ` ` `findRemainders(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find all the distinct ` `// remainders when n is divided by ` `// all the elements from ` `// the range [1, n + 1] ` `static` `void` `findRemainders(` `long` `n) ` `{ ` ` ` ` ` `// Set will be used to store ` ` ` `// the remainders in order ` ` ` `// to eliminate duplicates ` ` ` `HashSet<Long> vc = ` `new` `HashSet<Long>(); ` ` ` ` ` `// Find the remainders ` ` ` `for` `(` `long` `i = ` `1` `; i <= Math.ceil(Math.sqrt(n)); i++) ` ` ` `vc.add(n / i); ` ` ` `for` `(` `long` `i = (` `long` `) (n / Math.ceil(Math.sqrt(n)) - ` `1` `); ` ` ` `i >= ` `0` `; i--) ` ` ` `vc.add(i); ` ` ` ` ` `// Print the contents of the set ` ` ` `for` `(` `long` `it : vc) ` ` ` `System.out.print(it+ ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `n = ` `5` `; ` ` ` ` ` `findRemainders(n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `ceil, floor, sqrt ` ` ` `# Function to find all the distinct ` `# remainders when n is divided by ` `# all the elements from ` `# the range [1, n + 1] ` `def` `findRemainders(n): ` ` ` ` ` `# Set will be used to store ` ` ` `# the remainders in order ` ` ` `# to eliminate duplicates ` ` ` `vc ` `=` `dict` `() ` ` ` ` ` `# Find the remainders ` ` ` `for` `i ` `in` `range` `(` `1` `, ceil(sqrt(n)) ` `+` `1` `): ` ` ` `vc[n ` `/` `/` `i] ` `=` `1` ` ` `for` `i ` `in` `range` `(n ` `/` `/` `ceil(sqrt(n)) ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `vc[i] ` `=` `1` ` ` ` ` `# Print the contents of the set ` ` ` `for` `it ` `in` `sorted` `(vc): ` ` ` `print` `(it, end ` `=` `" "` `) ` ` ` `# Driver code ` `n ` `=` `5` ` ` `findRemainders(n) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find all the distinct ` `// remainders when n is divided by ` `// all the elements from ` `// the range [1, n + 1] ` `static` `void` `findRemainders(` `long` `n) ` `{ ` ` ` ` ` `// Set will be used to store ` ` ` `// the remainders in order ` ` ` `// to eliminate duplicates ` ` ` `List<` `long` `> vc = ` `new` `List<` `long` `>(); ` ` ` ` ` `// Find the remainders ` ` ` ` ` `for` `(` `long` `i = 1; i <= Math.Ceiling(Math.Sqrt(n)); i++) ` ` ` `vc.Add(n / i); ` ` ` `for` `(` `long` `i = (` `long` `) (n / Math.Ceiling(Math.Sqrt(n)) - 1); ` ` ` `i >= 0; i--) ` ` ` `vc.Add(i); ` ` ` `vc.Reverse(); ` ` ` ` ` `// Print the contents of the set ` ` ` `foreach` `(` `long` `it ` `in` `vc) ` ` ` `Console.Write(it + ` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `long` `n = 5; ` ` ` ` ` `findRemainders(n); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

0 1 2 5

## Recommended Posts:

- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- Find K distinct positive odd integers with sum N
- Find n positive integers that satisfy the given equations
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Ways to write N as sum of two or more positive integers | Set-2
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Number of ways in which N can be represented as the sum of two positive integers
- Check whether product of integers from a to b is positive , negative or zero
- Count of digits after concatenation of first N positive integers
- Number of arrays of size N whose elements are positive integers and sum is K
- Count positive integers with 0 as a digit and maximum 'd' digits
- Maximum number of distinct positive integers that can be used to represent N
- Check whether a number can be represented as sum of K distinct positive integers
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count 'd' digit positive integers with 0 as a digit
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Minimum operations to make counts of remainders same in an array
- Find all the possible numbers in a range that can be evenly divided by its digits

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.