Project Euler 76: How many different ways can one hundred be written as a sum of at least two positive integers?

Project Euler 76: How many different ways can one hundred be written as a sum of at least two positive integers?

Written by on 24 December 2011

Topics: Project Euler

Problem 76 of Project Euler reads

It is possible to write five as a sum in exactly six different ways:

4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1

How many different ways can one hundred be written as a sum of at least two positive integers?

It is one of the shortest descriptions I have read for a long time and it will be one of the shortest solutions I have written for a short while. When I solved this I thought I was really clever to realise we could reuse an old solution for this. But apparantly the majority of the 9000+ other users who have solved it realised the same thing.

So which solution am I talking about? The solution to problem 31, the one about English currency denominations. We can reuse the dynamic programming solution for this problem. I think I have written the best explanation for that I can possibly come up with, so if you are not familiar with the solution for problem 31, I will suggest that you check it out before proceeding.

The difference here, is that instead of using the currency denominations we need to use every integer from 1 to 99 as possible values. This makes the solution look like

int target = 100;
int[] ways = new int[target + 1];
ways[0] = 1;

for (int i = 1; i <= 99; i++) {
    for (int j = i; j <= target; j++) {
        ways[j] += ways[j - i];
    }
}

The result is the last field of the ways array. Executing this piece of code is

100 can be generated in 190569291 ways
Solution took 0,0092 ms

Wrapping up

I know this has been quite a short post, but since I just reused the solution from a previous exercise, I don’t really have much to add.

You can find the source code here, if you prefer it as a .cs file instead of snippets pasted onto a website.

Comments, alternative solutions and questions are as always very welcome.

The photo for todays blog post is kindly shared by Rodrigo Denúbila under the creative commons license. My derivative crop is of course shared under the same license.

8 Comments For This Post I'd Love to Hear Yours!

  1. JM says:

    Hii.. Sir Iam new to Dynamic Programming.. Please can you explain me how you choose the trival case her ways[0] = 1.

  2. Kristian says:

    It is merely a matter to get the calculations to fit, and not really something that makes a whole lot of physical sense. However, I think you should read the solution to problem 31, as that is more elaborated.

  3. JM says:

    Thanks for your reply sir.. the explanation to problem 31 helps me a lot..

  4. Ryan Gallagher says:

    How does this explain 100 you only do the number 4

  5. Kristian says:

    I don’t know how to answer this, but I think you are missing an integral of the post. The answer is that there are a little less than 200 mio ways to write 100 as a sum of 2 or more positive integers, so I don’t think it will be wise to write all of them.

  6. Afshin says:

    There is a more efficient way to solve this problem ( by using petagonal number theorem)
    take a look at

    http://en.wikipedia.org/wiki/Pentagonal_number_theorem

    p(n) = p(n-1) + p(n-2) – p(n-5) – p(n-7) + …

  7. Kristian says:

    Hi Afshin

    Thanks for noting that. I realized that when I solved problem 78 where I use a generating function based on the pentagonal numbers.

  8. Akkad says:

    DP solution to coin change problem solves it by using a 2D array. The function comes out to be something like:
    C(N,m) = C(N,m – 1) + C(N – Sm,m)

    I am finding it hard to figure out how this fit into a single array. Can you give me the recursive function? Or the logic behind this?

Leave a Comment Here's Your Chance to Be Heard!

You can use these html tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

You can use short tags like: [code language="language"][/code]

The code tag supports the following languages: as3, bash, coldfusion, c, cpp, csharp, css, delphi, diff,erlang, groovy, javascript, java, javafx, perl, php, plain, powershell, python, ruby, scala, sql, tex, vb, xml

You can use [latex]your formula[/latex] if you want to format something using latex

Notify me of comments via e-mail. You can also subscribe without commenting.