# Ways to write N as sum of two or more positive integers in C++

In this problem, we are given an integer n. Our task is to find the total number of ways in can be expressed as sum of two or more positive integers.

Let’s take an example to understand the problem,

## Input

N = 4

## Output

5

## Explanation

4 can be written as the sum in these ways,
4, 3+1, 2+2, 2+1+1, 1+1+1+1

To solve this problem, we will use Euler’s recurrence formula. For a number n the total number of ways it can be generated p(n) by,

Σ∞n=0 p(n)xn = Π∞k=1 (1/(1-xk ))

Using this formula, we will derive formula for p(n),p(n) = p(n-1) + p(n-2) - p(n-5) - p(n-7) + … + (-1)(k-1)((k(3k-1))/2)

Program to illustrate the implementation of our solution,

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
long long postiveSum(int n){
vector<long long> p(n + 1, 0);
p = 1;
for (int i = 1; i <= n; ++i) {
int k = 1;
while ((k * (3 * k - 1)) / 2 <= i) {
p[i] += (k % 2 ? 1 : -1) * p[i - (k * (3 * k - 1)) / 2];
if (k > 0)
k *= -1;
else
k = 1 - k;
}
}
return p[n];
}
int main(){
int N = 12;
cout<<"The number of ways "<<N<<" can be written as sum of two or more positive numbers is "      <<postiveSum(N);
return 0;
}

## Output

The number of ways 12 can be written as sum of two or more positive numbers is 77