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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Check if N-factorial is divisible by X^Y
- 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 the number formed by the last digits of N numbers is divisible by 10 or not
- Find the Nth natural number which is not divisible by A
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Check if N is a Factorial Prime
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Find all factorial numbers less than or equal to n
- Expressing factorial n as sum of consecutive numbers
- Count factorial numbers in a given range
- Factorial of Large numbers using Logarithmic identity
- LCM of First n Natural Numbers
- Natural Numbers
- Sum of 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 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.