Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ways to write N as sum of two or more positive integers | Set-2

  • Last Updated : 12 Apr, 2021

Given a number N, the task is to find the number of ways N can be partitioned, i.e. the number of ways that N can be expressed as a sum of positive integers.
Note: N should also be considered itself a way to express it as a sum of positive integers. 
Examples:
 

Input: N = 5 
Output:
5 can be partitioned in the following ways: 

4 + 1 
3 + 2 
3 + 1 + 1 
2 + 2 + 1 
2 + 1 + 1 + 1 
1 + 1 + 1 + 1 + 1
Input: N = 10 
Output: 42 
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



This post has been already discussed in Ways to write n as sum of two or more positive integers. In this post, an efficient approach is discussed.
Approach(Using Euler’s recurrence): 
If p(n) is the number of partitions of N, then it can be generated by the following generating function:
\sum_{n=0}^\infty p(n)x^n = \prod_{k=1}^\infty \left(\frac {1}{1-x^k} \right)
Using this formula and Euler’s pentagonal number theorem, we can derive the following recurrence relation for p(n): (Check the Wikipedia article for more details)
p(n) = p(n - 1) + p (n - 2) - p(n - 5) - p(n - 7) + \cdots + (-1)^{|{k} - 1|} \frac{k(3k - 1)}{2}
where k = 1, -1, 2, -2, 3, -3, … and p(n) = 0 for n < 0.
Below is the implementation of above approach:
 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number
// of partitions of N
long long partitions(int n)
{
    vector<long long> p(n + 1, 0);
 
    // Base case
    p[0] = 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];
}
 
// Driver code
int main()
{
    int N = 20;
    cout << partitions(N);
    return 0;
}

Java




// Java implementation of above approach
class GFG
{
 
    // Function to find the number
    // of partitions of N
    static long partitions(int n)
    {
        long p[] = new long[n + 1];
 
        // Base case
        p[0] = 1;
 
        for (int i = 1; i <= n; ++i)
        {
            int k = 1;
            while ((k * (3 * k - 1)) / 2 <= i)
            {
                p[i] += (k % 2 != 0 ? 1 : -1) *
                    p[i - (k * (3 * k - 1)) / 2];
 
                if (k > 0)
                {
                    k *= -1;
                }
                else
                {
                    k = 1 - k;
                }
            }
        }
        return p[n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 20;
        System.out.println(partitions(N));
    }
}
 
// This code is contributed by Rajput-JI

Python 3




# Python 3 implementation of
# above approach
 
# Function to find the number
# of partitions of N
def partitions(n):
 
    p = [0] * (n + 1)
 
    # Base case
    p[0] = 1
 
    for i in range(1, n + 1):
        k = 1
        while ((k * (3 * k - 1)) / 2 <= i) :
            p[i] += ((1 if k % 2 else -1) *
                    p[i - (k * (3 * k - 1)) // 2])
 
            if (k > 0):
                k *= -1
            else:
                k = 1 - k
 
    return p[n]
 
# Driver code
if __name__ == "__main__":
    N = 20
    print(partitions(N))
 
# This code is contributed
# by ChitraNayal

C#




// C# implementation of above approach
using System;
 
class GFG
{
 
    // Function to find the number
    // of partitions of N
    static long partitions(int n)
    {
        long []p = new long[n + 1];
 
        // Base case
        p[0] = 1;
 
        for (int i = 1; i <= n; ++i)
        {
            int k = 1;
            while ((k * (3 * k - 1)) / 2 <= i)
            {
                p[i] += (k % 2 != 0 ? 1 : -1) *
                    p[i - (k * (3 * k - 1)) / 2];
 
                if (k > 0)
                {
                    k *= -1;
                }
                else
                {
                    k = 1 - k;
                }
            }
        }
        return p[n];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int N = 20;
        Console.WriteLine(partitions(N));
    }
}
 
// This code has been contributed by 29AjayKumar

PHP




<?php
// PHP implementation of above approach
 
// Function to find the number
// of partitions of N
function partitions($n)
{
    $p = array_fill(0, $n + 1, 0);
 
    // Base case
    $p[0] = 1;
 
    for ($i = 1; $i < $n + 1; $i++)
    {
        $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];
}
 
// Driver Code
$N = 20;
print(partitions($N));
 
// This code is contributed
// by mits
?>

Javascript




<script>
// javascript implementation of above approach   
// Function to find the number
    // of partitions of N
    function partitions(n)
    {
        var p = Array(n + 1).fill(0);
 
        // Base case
        p[0] = 1;
 
        for (i = 1; i <= n; ++i)
        {
            var k = 1;
            while ((k * (3 * k - 1)) / 2 <= i)
            {
                p[i] += (k % 2 != 0 ? 1 : -1) * p[i - (k * (3 * k - 1)) / 2];
 
                if (k > 0) {
                    k *= -1;
                } else {
                    k = 1 - k;
                }
            }
        }
        return p[n];
    }
 
    // Driver code
     
        var N = 20;
        document.write(partitions(N));
 
// This code is contributed by todaysgaurav
</script>
Output: 
627

 

Time Complexity: O(N√N) 
Space Complexity: O(N)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!