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.

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

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.

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

How does this explain 100 you only do the number 4

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.

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) + …

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.

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?

@afshin,

It seems to me that the pentagonal number theorem and the DP coin change problem are exactly the same. According to the wiki article on partitions, the algorithm for computing p(n) uses memoization and the sets: p(n,m), the number of ways of partitioning n into m sets. This is equivalent to finding “the number of ways to sum $n using m coins”.

I solved problem 78 before 76. Having created the function to calculate p(n) for problem 78, I just used it again for 76. The only twist is that for problem 76 the answer is p(n)-1 because only solutions which are a sum of 2 or more digits are counted.