This week's giveaway is in the Groovy forum. We're giving away four copies of Groovy Fundamentals video training course and have Ken Kousen on-line! See this thread for details.

Tim Moores wrote:My solution ended up having 8 nested loops. It also had a variable like this: int numCoins[] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }

My solution used a recursive approach. Let's say the first coin you choose is a £1 coin. Then you have to work out how many ways you can split the remaining £1. You also need to make sure you don't double-count any permutations.

Randall Twede wrote:am i at least on the right track?

I'd suggest forgetting the £2 problem to begin with, and use the program to solve one simple enough to also do by hand. That way you can get it to write out the permutations, and you should be able to see which ones it's missing.

E.g. 6p can be split up in the following ways:
5, 1
2, 2, 2
2, 2, 1, 1
2, 1, 1, 1, 1
1, 1, 1, 1, 1, 1

i have been struggling with this one for several days now and i still get the wrong answer
can anyone see anything wrong with this code?
it seems way too long for one thing, most of these problems only take about 30 lines or so

Matthew Brown wrote:. . . You also need to make sure you don't double-count any permutations.

If you count your coins in size order, that should obviate that problem. If, for example, the first coin you try is a 50p, you are allowed to use 50p later, or go down to 20p, but you are not allowed to go up to £1. You can of course do the same in reverse order.

Tim Moores wrote:The code I wrote for this a few years back found 73682 different ways. I've no idea if it's correct, though :-)

I also get 73682. I wrote a program to solve the general case (variable coins, different total) which I believe is virtually impossible without recursion. Otherwise you're looking at eight nested loops.

I have it with 2 loops. One main loop that goes through { 1, 2, 5, 10, 20, 50, 100, 200 } and each time calls a recursive function. The function itself also contains a loop, which is only executed when the current sum is less that the value that needs to be calculated (200p in this case). However I get 73681 solutions.

Edit: I get the correct result now of 73682 solutions. And about 40 lines of code. I use a LinkedList to keep track of all unique combinations.