aspose file tools*
The moose likes Java in General and the fly likes Euler31 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Euler31" Watch "Euler31" New topic
Author

Euler31

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2


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
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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: 2408
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: 4347
    
    2

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: 4347
    
    2

i think i have figured it out. just had to think about it longer
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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: 4347
    
    2

thanks Tim,
my approach doesn't seem to be working

gives answer 4509 which is incorrect
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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: 4422
    
    8

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: 4347
    
    2

a very good suggestion Matthew! i have done that to help solve other problems before
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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: 11420
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
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: 4422
    
    8

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