- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Expressing factorial n as sum of consecutive numbers

We will discuss two approaches to find out how we can express the factorial of a number as the sum of consecutive numbers. First one is a straightforward and simple approach while in the other approach we use the concept of arithmetic progression to make it less complex in terms of time and space occupied.

## Problem statement

We are given a number and we need to find out the number of ways in which we can represent the factorial of the number as a sum of consecutive natural numbers.

This involves two different functions −

To find the factorial of the number.

To find the number of ways in which we can represent the number as the sum of successive natural numbers.

Example 1

Given : Number = 3 Result: 1

As we know, Factorial of 3 is 6 which can be written as 1+2+3 hence our answer is: 1 way.

Example 2

Given: Number = 4 Result: 1

As we know, Factorial of 4 is 24 which can be written as 7+8+9 hence our answer is: 1 way.

## Approach 1

This is a simple approach in which we will first find out the factorial of the number and then we will calculate the number of ways it can be represented as a sum of successive natural numbers The approach is to represent the factorial as an arithmetic progression of length len+1 as −

Factorial of Number = p + (p+1) + (p+2) + … + (p+len) So, p = (Number- len*(len+1)/2)/(len+1) We will check for the values of len from 1 to len*(len+1)/2<Number

When we obtain len as a positive integer, we will count it as a solution.

### Example

In the example below, we try to find the number of ways in which the factorial of a number can be expressed as sum of consecutive numbers.

#include <bits/stdc++.h> using namespace std; // code for obtaining number of possible solutions long int Number_of_solutions(long int NUMBER){ long int counter = 0; for (long int len = 1; len * (len + 1) < 2 * NUMBER; len++) { double p = (1.0 * NUMBER - (len * (len + 1)) / 2) / (len + 1); if (p - (int)p == 0.0) counter++; } return counter; } // main program goes here int main(){ long int NUMBER = 15; cout << "Number of ways to write 15 as a sum of consecutive numbers: "; cout << Number_of_solutions(NUMBER) << endl; NUMBER = 10; cout << "Number of ways to write 10 as a sum of consecutive numbers: "; cout << Number_of_solutions(NUMBER) << endl; return 0; }

### Output

When you run the above C++ program, it will produce the following output −

Number of ways to write 15 as a sum of consecutive numbers: 3 Number of ways to write 10 as a sum of consecutive numbers: 1

## Approach 2: Optimized Approach

This is a better approach; the approach we saw above causes overflow.

The sum of len successive numbers starting from number p can be written as −

sum = (p+1) + (p+2) + (p+3) … + (p+len) Hence, sum = (len*(len + 2*p + 1))/2

Since *sum* is also equal to Number!.

We can write

2*Number! = (len*(len + 2*p + 1))

Here, we will count all the pairs of (len, (len + 2*p + 1)) instead of counting all the pairs of (len, p). This means we will count all the ordered pf (A, B) where AB=2*Number! And A< B and parity of A and B are different which means if len is odd then (len + 2*p + 1) is even and if len is Even then (len + 2*p + 1) is odd.

This means we are finding the odd divisors of 2*Number! Which is also the odd divisor of Number!.

To count the divisors in number!, we have to calculate the power of prime numbers in factorization and number of divisors is (f1 + 1)*(f2 + 1)* … *(fn + 1).

We will use Legendre’s formula to calculate the biggest powers of a prime number in the factorial of a number.

### Example

Code for this approach is given below −

#include <bits/stdc++.h> using namespace std; #define maximum 5002 vector<int> v; void sieve(){ bool Is_the_number_prime[maximum]; memset (Is_the_number_prime, true, sizeof(Is_the_number_prime) ); for (int prime = 2; prime * prime < maximum; prime++) { if (Is_the_number_prime[prime] == true) { for (int iterator = prime * 2; iterator < maximum; iterator += prime) Is_the_number_prime[iterator] = false; } } for (int prime = 2; prime < maximum; prime++) if (Is_the_number_prime[prime]) v.push_back(prime); } long long int calculate_largest_power(long long int a, long long int b){ long long int c = 0; long long int x = b; while (a >= x) { c += (a / x); x *= b; } return c; } long long int modular_mult(long long int a, long long int b, long long int m){ long long int result = 0; a = a % m; while (b > 0) { if (b % 2 == 1) result = (result + a) % m; a = (a * 2) % m; b /= 2; } return result % m; } long long int no_of_ways(long long int n, long long int m){ long long int answer = 1; for (int iterator = 1; iterator < v.size(); iterator++) { long long int powers = calculate_largest_power(n, v[iterator]); if (powers == 0) break; answer = modular_mult(answer, powers + 1, m)%m; } if (((answer - 1) % m) < 0) return (answer - 1 + m) ; else return (answer - 1) ; } int main(){ sieve(); long long int n = 4, m = 7; cout << "Number of solutions after performing modulo with 7 is " <<no_of_ways(n, m); return 0; }

### Output

When run the above C++ program, it will produce the following output −

Number of solutions after performing modulo with 7 is 1.

## Conclusion

In this article, we discussed two different ways to find out the number of ways in which we can represent the factorial of the number as a sum of consecutive natural numbers.