# Find the first natural number whose factorial is divisible by x

Given a number x, the task is to find first natural number i whose factorial is divisible by x.

```Input  : x = 10
Output : 5
5 is the smallest number such that
(5!) % 10 = 0

Input  : x = 16
Output : 6
6 is the smallest number such that
(6!) % 16 = 0
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A simple solution is to iterate from 1 to x-1 and for every number i check if i! is divisible by x.

```// A simple C++ program to find first natural
// number whose factorial divides x.
#include<bits/stdc++.h>
using namespace std;

// Returns first number whose factorial
// divides x.
int firstFactorialDivisibleNumber(int x)
{
int i = 1;  // Result
int fact = 1;
for (i=1; i<x; i++)
{
fact = fact * i;
if (fact % x == 0)
break;
}
return i;
}

// Driver code
int main(void)
{
int x = 16;
cout << firstFactorialDivisibleNumber(x);
return 0;
}
```

Output :

`6`

If we apply this naive approach, we wouldn’t go above 20! or 21! (long long int will have its upper limit).

A better solution avoids overflow. The solution is based on below observations.

• If i! is divisible by x, then (i+1)!, (i+2)!, … are also divisible by x.
• For a number x, all factorials i! are divisible by x when i >= x.
• If a number x is prime, then no factorial below x can divide it as x cannot be formed with multiplication of smaller numbers.

Below is algorithm

```1) Run a loop for i = 1 to n-1

a) Remove common factors
new_x /= gcd(i, new_x);

b) Check if we found first i.
if (new_x == 1)
break;

2) Return i```

Below is C++ implementation of above idea.

```// C++ program to find first natural number
// whose factorial divides x.
#include<bits/stdc++.h>
using namespace std;

// GCD function to compute the greatest
// divisor among a and b
int gcd(int a, int b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}

// Returns first number whose factorial
// divides x.
int firstFactorialDivisibleNumber(int x)
{
int i = 1;  // Result
int new_x = x;

for (i=1; i<x; i++)
{
// Remove common factors
new_x /= gcd(i, new_x);

// We found first i.
if (new_x == 1)
break;
}
return i;
}

// Driver code
int main(void)
{
int x = 16;
cout << firstFactorialDivisibleNumber(x);
return 0;
}
```

Output :

`6`

This article is contributed by Shubham Gupta. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.3 Average Difficulty : 3.3/5.0
Based on 16 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.