Given two numbers N and M. Find the number of ways in which factorial N can be expressed as a sum of two or more consecutive numbers. Print the result modulo M.
Input : N = 3, M = 7 Output : 1 Explanation: 3! can be expressed in one way, i.e. 1 + 2 + 3 = 6. Hence 1 % 7 = 1 Input : N = 4, M = 7 Output : 1 Explanation: 4! can be expressed in one way, i.e. 7 + 8 + 9 = 24 Hence 1 % 7 = 1
A simple solution is to first compute factorial, then count number of ways to represent factorial as sum of consecutive numbers using Count ways to express a number as sum of consecutive numbers. This solution causes overflow.
Below is a better solution to avoid overflow.
Let us consider that sum of r consecutive numbers be expressed as:
(a + 1) + (a + 2) + (a + 3) + … + (a + r), which simplifies as (r * (r + 2*a + 1)) / 2
Hence, (a + 1) + (a + 2) + (a + 3) + … + (a + r) = (r * (r + 2*a + 1)) / 2. Since the above expression is equal to factorial N, we write it as
2 * N! = r * (r + 2*a + 1)
Instead of counting all the pairs (r, a), we will count all pairs (r, r + 2*a + 1). Now, we are just counting all ordered pairs (X, Y) with XY = 2 * N! where X < Y and X, Y have different parity, that means if (r) is even, (r + 2*a + 1) is odd or if (r) is odd then (r + 2*a + 1) is even. This is equivalent to finding the odd divisors of 2 * N! which will be same as odd divisors of N!.
For counting the number of divisors in N!, we calculate the power of primes in factorization and total count of divisors become (p1 + 1) * (p2 + 1) * … * (pn + 1). To calculate the largest power of a prime in N!, we will use legendre’s formula.
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.
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find the last digit when factorial of A divides factorial of B
- 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
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count factorial numbers in a given range
- Find all factorial numbers less than or equal to n
- Factorial of Large numbers using Logarithmic identity
- Sum of divisors of factorial of a number
- Find sum of digits in factorial of a number
- Range and Update Sum Queries with Factorial
- Find the prime numbers which can written as sum of most consecutive primes
- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Count digits in a factorial | Set 2
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Count digits in a factorial | Set 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.