Given an integer **N**, the task is to check whether the product of first **N** natural numbers is divisible by the sum of first **N** natural numbers.

**Examples:**

Input:N = 3

Output:Yes

Product = 1 * 2 * 3 = 6

Sum = 1 + 2 + 3 = 6

Input:N = 6

Output:No

**Naive Approach:** Find the sum and product of first **N** natural numbers and check whether the product is divisible by the sum.

**Efficient Approach:** We know that the sum and product of first **N** naturals are **sum = (N * (N + 1)) / 2** and **product = N!** respectively. Now to check whether the product is divisible by the sum, we need to check if the remainder of the following equation is 0 or not.

N! / (N *(N + 1) / 2)

2 * (N – 1)! / N + 1

i.e. every factor of(N + 1)should be in(2 * (N – 1)!). So, if(N + 1)is a prime then we are sure that the product is not divisible by the sum.

So ultimately just check if(N + 1)is prime or not.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if n is prime ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Corner 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` `(` `int` `i = 5; i * i <= n; i = i + 6) ` ` ` `if` `(n % i == 0 || n % (i + 2) == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function that return true if the product ` `// of the first n natural numbers is divisible ` `// by the sum of first n natural numbers ` `bool` `isDivisible(` `int` `n) ` `{ ` ` ` `if` `(isPrime(n + 1)) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` `if` `(isDivisible(n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function that returns true if n is prime ` `static` `boolean` `isPrime(` `int` `n) ` `{ ` ` ` `// Corner 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` `(` `int` `i = ` `5` `; i * i <= n; i = i + ` `6` `) ` ` ` `if` `(n % i == ` `0` `|| n % (i + ` `2` `) == ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function that return true if the product ` `// of the first n natural numbers is divisible ` `// by the sum of first n natural numbers ` `static` `boolean` `isDivisible(` `int` `n) ` `{ ` ` ` `if` `(isPrime(n + ` `1` `)) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` `if` `(isDivisible(n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` `from` `math ` `import` `sqrt ` ` ` `# Function that returns true if n is prime ` `def` `isPrime(n): ` ` ` ` ` `# Corner 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` `and` `n ` `%` `3` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `for` `i ` `in` `range` `(` `5` `, ` `int` `(sqrt(n)) ` `+` `1` `, ` `6` `): ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `and` `n ` `%` `(i ` `+` `2` `) ` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Function that return true if the product ` `# of the first n natural numbers is divisible ` `# by the sum of first n natural numbers ` `def` `isDivisible(n): ` ` ` `if` `(isPrime(n ` `+` `1` `)): ` ` ` `return` `False` ` ` `return` `True` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `6` ` ` `if` `(isDivisible(n)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true if n is prime ` `static` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Corner 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` `(` `int` `i = 5; i * i <= n; i = i + 6) ` ` ` `if` `(n % i == 0 || n % (i + 2) == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function that return true if the product ` `// of the first n natural numbers is divisible ` `// by the sum of first n natural numbers ` `static` `bool` `isDivisible(` `int` `n) ` `{ ` ` ` `if` `(isPrime(n + 1)) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 6; ` ` ` `if` `(isDivisible(n)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function that returns true if n is prime ` `function` `isPrime(` `$n` `) ` `{ ` ` ` `// Corner 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` `(` `$i` `= 5; ` `$i` `* ` `$i` `<= ` `$n` `; ` `$i` `= ` `$i` `+ 6) ` ` ` `if` `(` `$n` `% ` `$i` `== 0 || ` `$n` `% (` `$i` `+ 2) == 0) ` ` ` `return` `false; ` ` ` ` ` `return` `true; ` `} ` ` ` `// Function that return true if the product ` `// of the first n natural numbers is divisible ` `// by the sum of first n natural numbers ` `function` `isDivisible(` `$n` `) ` `{ ` ` ` `if` `(isPrime(` `$n` `+ 1)) ` ` ` `return` `false; ` ` ` `return` `true; ` `} ` ` ` `// Driver code ` `$n` `= 6; ` `if` `(isDivisible(` `$n` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

No

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:

- Check if the concatenation of first N natural numbers is divisible by 3
- Check whether factorial of N is divisible by sum of first N natural numbers
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are divisible by X or Y
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count natural numbers whose factorials are divisible by x but not y
- Product of all Subsets of a set formed by first N natural numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Sum and Product of all Composite numbers which are divisible by k in an array
- Check whether product of digits at even places of a number is divisible by K
- Check whether product of 'n' numbers is even or odd
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Check if N can be expressed as product of 3 distinct numbers
- Check if all sub-numbers have distinct Digit product
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if each element of the given array is the product of exactly K prime numbers
- Check if product of array containing prime numbers is a perfect square
- Check whether a number can be expressed as a product of single digit numbers

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.