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.
Input: N = 3
Product = 1 * 2 * 3 = 6
Sum = 1 + 2 + 3 = 6
Input: N = 6
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:
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.
- 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
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Check if the concatenation of first N natural numbers is divisible by 3
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- 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
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Partition first N natural number into two sets such that their sum is not coprime
- 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
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Split N natural numbers into two sets having GCD of their sums greater than 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.