# Euler31

posted 4 years ago

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

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

posted 4 years ago

ohhh...i think i see the problem. it is not as simple as i first thought. any hints?

SCJP

Visit my download page

Tim Moores

Bartender

Posts: 2794

38

posted 4 years ago

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?

but it is still incorrect

am i at least on the right track?

SCJP

Visit my download page

posted 4 years ago

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

SCJP

Visit my download page

Tim Moores

Bartender

Posts: 2794

38

posted 4 years ago

thanks Tim,

my approach doesn't seem to be working

gives answer 4509 which is incorrect

my approach doesn't seem to be working

gives answer 4509 which is incorrect

SCJP

Visit my download page

Matthew Brown

Bartender

Posts: 4567

8

posted 4 years ago

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.

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

Posts: 4567

8

posted 4 years ago

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 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

posted 4 years ago

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

SCJP

Visit my download page

posted 4 years ago

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

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

SCJP

Visit my download page

posted 4 years ago

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

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

Posts: 48940

60

posted 4 years ago

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 wrote:. . . You also need to make sure you don't double-count any permutations.

Matthew Brown

Bartender

Posts: 4567

8

Bill Krieg

Greenhorn

Posts: 9

posted 4 years ago

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.

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.

posted 4 years ago

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.

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.