File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Euler31 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Euler31" Watch "Euler31" New topic
Author

Euler31

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).

It is possible to make £2 in the following way:

1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p

How many different ways can £2 be made using any number of coins?



i cant see why i am getting the wrong answer

i get 178 for the answer. i wouldn't be surprised if it was off by 1


SCJP
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
ohhh...i think i see the problem. it is not as simple as i first thought. any hints?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2407
The code I wrote for this a few years back found 73682 different ways. I've no idea if it's correct, though :-)
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
well, i thought i was on to it. my new answer is 462
but it is still incorrect
am i at least on the right track?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
i think i have figured it out. just had to think about it longer
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2407
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 }
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
thanks Tim,
my approach doesn't seem to be working

gives answer 4509 which is incorrect
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3860
    
    1

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3860
    
    1

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
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
a very good suggestion Matthew! i have done that to help solve other problems before
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    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

i get answer = 64184
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10040
    
    6

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 }

I also used eight nested loops, but had eight int variables instead of an array of eight ints.


Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3860
    
    1

Campbell Ritchie wrote:If you count your coins in size order, that should obviate that problem.
Yes, that's how I did it.
Bill Krieg
Greenhorn

Joined: Jul 31, 2011
Posts: 9
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.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Euler31
 
Similar Threads
Dual Program (Applet & Application) : Need to know how to improve in Applet ways only!
LinkedHashMap - trying to use the method removeEldestEntry
Wordwrap in GridLayout
Project Euler #17
Converting Number to Word