# Check if factorial of N is divisible by the sum of squares of first N natural numbers

Given an integer N, the task is to find whether fact(N) is divisible by sum(N) where fact(N) is the factorial of N and sum(N) = 12 + 22 + 32 + … + N2.

Examples:

Input: N = 5
Output: No
fact(N) = 120, sum(N) = 55
And, 120 is not divisible by 55

Input: N = 7
Output: Yes

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. It is important here to first realize the closed formula for summation of squares of all numbers. Summation of Squares of first N natural numbers.
2. Now since, n is a common factor of both N factorial and summation we can remove it.
3. Now for every prime P in Value (N + 1) * (2N + 1), say there are X factors of P in Value then, find the number of factors of P in Factorial (N – 1), say they are Y. If Y < X, then two are never divisible, else continue.
4. To calculate the number of factors of P in factorial (N), we can simply use Lengendre Formula.
5. In point 4, increase the count of Prime Number 2, 3 with 1 to account for the 6 in the formula of summation.
6. Check individually for all the prime P in Value, and if all satisfy condition 3, then answer is Yes.
7. Point 2 will help us to reduce our time complexity with a factor of N.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `#define ll long long int ` `using` `namespace` `std; ` ` `  `// Function to count number of times ` `// prime P divide factorial N ` `bool` `checkfact(``int` `N, ``int` `countprime, ``int` `prime) ` `{ ` `    ``int` `countfact = 0; ` `    ``if` `(prime == 2 || prime == 3) ` `        ``countfact++; ` `    ``int` `divide = prime; ` ` `  `    ``// Lengendre Formula ` `    ``while` `(N / divide != 0) { ` `        ``countfact += N / divide; ` `        ``divide = divide * divide; ` `    ``} ` ` `  `    ``if` `(countfact >= countprime) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Function to find count number of times ` `// all prime P divide summation ` `bool` `check(``int` `N) ` `{ ` ` `  `    ``// Formula for summation of square after removing n ` `    ``// and constant 6 ` `    ``int` `sumsquares = (N + 1) * (2 * N + 1); ` `    ``int` `countprime = 0; ` ` `  `    ``// Loop to traverse over all prime P which divide ` `    ``// summation ` `    ``for` `(``int` `i = 2; i <= ``sqrt``(sumsquares); i++) { ` `        ``int` `flag = 0; ` ` `  `        ``while` `(sumsquares % i == 0) { ` `            ``flag = 1; ` `            ``countprime++; ` `            ``sumsquares /= i; ` `        ``} ` ` `  `        ``if` `(flag) { ` `            ``if` `(!checkfact(N - 1, countprime, i)) ` `                ``return` `false``; ` `            ``countprime = 0; ` `        ``} ` `    ``} ` ` `  `    ``// If Number itself is a Prime Number ` `    ``if` `(sumsquares != 1) ` `        ``if` `(!checkfact(N - 1, 1, sumsquares)) ` `            ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 5; ` `    ``if` `(check(N)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GfG ` `{  ` ` `  `// Function to count number of times  ` `// prime P divide factorial N  ` `static` `boolean` `checkfact(``int` `N, ``int` `countprime,  ` `                                    ``int` `prime)  ` `{  ` `    ``int` `countfact = ``0``;  ` `    ``if` `(prime == ``2` `|| prime == ``3``)  ` `        ``countfact++;  ` `    ``int` `divide = prime;  ` ` `  `    ``// Lengendre Formula  ` `    ``while` `(N / divide != ``0``) ` `    ``{  ` `        ``countfact += N / divide;  ` `        ``divide = divide * divide;  ` `    ``}  ` ` `  `    ``if` `(countfact >= countprime)  ` `        ``return` `true``;  ` `    ``else` `        ``return` `false``;  ` `}  ` ` `  `// Function to find count number of times  ` `// all prime P divide summation  ` `static` `boolean` `check(``int` `N)  ` `{  ` ` `  `    ``// Formula for summation of square after removing n  ` `    ``// and constant 6  ` `    ``int` `sumsquares = (N + ``1``) * (``2` `* N + ``1``);  ` `    ``int` `countprime = ``0``;  ` ` `  `    ``// Loop to traverse over all prime P which divide  ` `    ``// summation  ` `    ``for` `(``int` `i = ``2``; i <= Math.sqrt(sumsquares); i++)  ` `    ``{  ` `        ``int` `flag = ``0``;  ` ` `  `        ``while` `(sumsquares % i == ``0``)  ` `        ``{  ` `            ``flag = ``1``;  ` `            ``countprime++;  ` `            ``sumsquares /= i;  ` `        ``}  ` ` `  `        ``if` `(flag == ``1``)  ` `        ``{  ` `            ``if` `(!checkfact(N - ``1``, countprime, i))  ` `                ``return` `false``;  ` `            ``countprime = ``0``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// If Number itself is a Prime Number  ` `    ``if` `(sumsquares != ``1``)  ` `        ``if` `(!checkfact(N - ``1``, ``1``, sumsquares))  ` `            ``return` `false``;  ` ` `  `    ``return` `true``;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `N = ``5``;  ` `    ``if` `(check(N))  ` `        ``System.out.println(``"Yes"``);  ` `    ``else` `        ``System.out.println(``"No"``);  ` `} ` `}  ` ` `  `// This code is contributed by Prerna Saini `

## Python3

 `# Python 3 implementation of the approach ` `from` `math ``import` `sqrt ` ` `  `# Function to count number of times ` `# prime P divide factorial N ` `def` `checkfact(N, countprime, prime): ` `    ``countfact ``=` `0` `    ``if` `(prime ``=``=` `2` `or` `prime ``=``=` `3``): ` `        ``countfact ``+``=` `1` `    ``divide ``=` `prime ` ` `  `    ``# Lengendre Formula ` `    ``while` `(``int``(N ``/` `divide ) !``=` `0``): ` `        ``countfact ``+``=` `int``(N ``/` `divide) ` `        ``divide ``=` `divide ``*` `divide ` ` `  `    ``if` `(countfact >``=` `countprime): ` `        ``return` `True` `    ``else``: ` `        ``return` `False` ` `  `# Function to find count number of times ` `# all prime P divide summation ` `def` `check(N): ` `     `  `    ``# Formula for summation of square after  ` `    ``# removing n and constant 6 ` `    ``sumsquares ``=` `(N ``+` `1``) ``*` `(``2` `*` `N ``+` `1``) ` `    ``countprime ``=` `0` ` `  `    ``# Loop to traverse over all prime P  ` `    ``# which divide summation ` `    ``for` `i ``in` `range``(``2``, ``int``(sqrt(sumsquares)) ``+` `1``, ``1``): ` `        ``flag ``=` `0` ` `  `        ``while` `(sumsquares ``%` `i ``=``=` `0``): ` `            ``flag ``=` `1` `            ``countprime ``+``=` `1` `            ``sumsquares ``/``=` `i ` ` `  `        ``if` `(flag): ` `            ``if` `(checkfact(N ``-` `1``,  ` `                ``countprime, i) ``=``=` `False``): ` `                ``return` `False` `            ``countprime ``=` `0` ` `  `    ``# If Number itself is a Prime Number ` `    ``if` `(sumsquares !``=` `1``): ` `        ``if` `(checkfact(N ``-` `1``, ``1``,  ` `            ``sumsquares) ``=``=` `False``): ` `            ``return` `False` ` `  `    ``return` `True` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``N ``=` `5` `    ``if``(check(N)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` `         `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{  ` ` `  `// Function to count number of times  ` `// prime P divide factorial N  ` `static` `bool` `checkfact(``int` `N, ``int` `countprime,  ` `                              ``int` `prime)  ` `{  ` `    ``int` `countfact = 0;  ` `    ``if` `(prime == 2 || prime == 3)  ` `        ``countfact++;  ` `    ``int` `divide = prime;  ` ` `  `    ``// Lengendre Formula  ` `    ``while` `(N / divide != 0) ` `    ``{  ` `        ``countfact += N / divide;  ` `        ``divide = divide * divide;  ` `    ``}  ` ` `  `    ``if` `(countfact >= countprime)  ` `        ``return` `true``;  ` `    ``else` `        ``return` `false``;  ` `}  ` ` `  `// Function to find count number of times  ` `// all prime P divide summation  ` `static` `bool` `check(``int` `N)  ` `{  ` ` `  `    ``// Formula for summation of square  ` `    ``// after removing n and constant 6  ` `    ``int` `sumsquares = (N + 1) * (2 * N + 1);  ` `    ``int` `countprime = 0;  ` ` `  `    ``// Loop to traverse over all prime P  ` `    ``// which divide summation  ` `    ``for` `(``int` `i = 2; i <= Math.Sqrt(sumsquares); i++)  ` `    ``{  ` `        ``int` `flag = 0;  ` ` `  `        ``while` `(sumsquares % i == 0)  ` `        ``{  ` `            ``flag = 1;  ` `            ``countprime++;  ` `            ``sumsquares /= i;  ` `        ``}  ` ` `  `        ``if` `(flag == 1)  ` `        ``{  ` `            ``if` `(!checkfact(N - 1, countprime, i))  ` `                ``return` `false``;  ` `            ``countprime = 0;  ` `        ``}  ` `    ``}  ` ` `  `    ``// If Number itself is a Prime Number  ` `    ``if` `(sumsquares != 1)  ` `        ``if` `(!checkfact(N - 1, 1, sumsquares))  ` `            ``return` `false``;  ` ` `  `    ``return` `true``;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `N = 5;  ` `    ``if` `(check(N))  ` `        ``Console.WriteLine(``"Yes"``);  ` `    ``else` `        ``Console.WriteLine(``"No"``);  ` `} ` `}  ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## PHP

 `= ``\$countprime``)  ` `        ``return` `true;  ` `    ``else` `        ``return` `false;  ` `}  ` ` `  `// Function to find count number of times  ` `// all prime P divide summation  ` `function` `check(``\$N``)  ` `{  ` ` `  `    ``// Formula for summation of square  ` `    ``// after removing n and constant 6  ` `    ``\$sumsquares` `= (``\$N` `+ 1) * (2 * ``\$N` `+ 1);  ` `    ``\$countprime` `= 0;  ` ` `  `    ``// Loop to traverse over all prime P  ` `    ``// which divide summation  ` `    ``for` `(``\$i` `= 2; ``\$i` `<= sqrt(``\$sumsquares``); ``\$i``++)  ` `    ``{  ` `        ``\$flag` `= 0;  ` ` `  `        ``while` `(``\$sumsquares` `% ``\$i` `== 0)  ` `        ``{  ` `            ``\$flag` `= 1;  ` `            ``\$countprime``++;  ` `            ``\$sumsquares` `= (int)(``\$sumsquares` `/ ``\$i``);  ` `        ``}  ` ` `  `        ``if` `(``\$flag` `== 1)  ` `        ``{  ` `            ``if` `(checkfact(``\$N` `- 1, ``\$countprime``, ``\$i``))  ` `                ``return` `false;  ` `            ``\$countprime` `= 0;  ` `        ``}  ` `    ``}  ` ` `  `    ``// If Number itself is a Prime Number  ` `    ``if` `(``\$sumsquares` `!= 1)  ` `        ``if` `(checkfact(``\$N` `- 1, 1, ``\$sumsquares``))  ` `            ``return` `false;  ` ` `  `    ``return` `true;  ` `}  ` ` `  `// Driver Code  ` `\$N` `= 5;  ` `if` `(check(``\$N``))  ` `    ``echo``(``"Yes"``);  ` `else` `    ``echo``(``"No"``);  ` ` `  `// This code is contributed by Code_Mech ` `?> `

Output:

```No
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.