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.
- 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
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Find all factorial numbers less than or equal to n
- Count factorial numbers in a given range
- 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
- Find the last digit when factorial of A divides factorial of B
- Expressing a fraction as a natural number under modulo 'm'
- Fibbinary Numbers (No consecutive 1s in binary)
- Express a number as sum of consecutive numbers
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Find the number of consecutive zero at the end after multiplying n numbers
- Check if a number can be expressed as a sum of consecutive numbers
- Prove that atleast one of three consecutive even numbers is divisible by 6
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.
Improved By : AnkitRai01