Given a number ‘N’. Check whether factorial of ‘N’ is divisible by the sum of first ‘N’ natural numbers or not? If divisibility is possible, then print YES else print NO.
Input: N = 3 Output: YES As (1*2*3)%(1+2+3) = 0, Hence divisibility is possible. Input: N = 4 Output: NO Here (1*2*3*4)%(1+2+3+4) != 0, Hence divisibility doesn't occur.
- Sum of first ‘n’ natural numbers: s = (n)*(n+1)/2 . This can be expressed as (n+1)!/2*(n-1)!
- Now n!/s = 2*(n-1)!/(n+1).
- From the above formula the observation is derived as:
- If ‘n+1’ is prime then ‘n!’ is not divisible by sum of first ‘n’ natural numbers.
- If ‘n+1’ is not prime then ‘n!’ is divisible by sum of first ‘n’ natural numbers.
- Let n = 4.
- Hence ‘n!/s’ = 2*(3!)/5. = 1*2*3*2/5 .
- Here for n! to be divisible by ‘s’ we need the presence at least a multiple of ‘5’ in the numerator, i.e. in the given example numerator is expressed as the product of 3! and 2, For the entire product to be divisible by ‘5’
at least one multiple of 5 should be there i.e. 5*1 or 5*2 or 5*3 and so on. Since in the factorial term the highest number present is ‘n-1’ the product i.e. the numerator can never be expressed with terms of ‘n+1’ if ‘n+1’ is prime. Hence divisibility is never possible.
- In any other case whether ‘n+1’ is even or odd but not ‘prime’ the divisibility is always possible.
Note: Special care is to be taken for the case n=1. As 1! is always divisible by 1.
Below is the implementation of the above approach:
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Check if product of first N natural numbers is divisible by their sum
- Find the first natural number whose factorial is divisible by x
- Sum of first N natural numbers which are divisible by X or Y
- Sum of first N natural numbers which are divisible by 2 and 7
- Count natural numbers whose factorials are divisible by x but not y
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Check if N is a Factorial Prime
- Expressing factorial n as sum of consecutive numbers
- Count factorial numbers in a given range
- Find all factorial numbers less than or equal to n
- LCM of First n Natural Numbers
- Sum of sum of first n natural numbers
- Natural Numbers
- Sum of fifth powers of the first n natural 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 firstname.lastname@example.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.