# Talk

## Coding maths problems?

(5 Posts)
MrsWombat Fri 10-Jun-16 21:42:01

nrich.maths.org/223 My 8 year old came home with this maths puzzle this afternoon. Specifically the
Henry chose 8 sweets. What could he have bought? question. He had completed it all in class but thought he could entertain his poor parents with it, as he found it really hard, and was the only one on his table who got it. We got it right in the end.

Is there anyway I could create a program/code that would solve this puzzle? I've been doing some basic coding lessons on Khan Academy (drawing and SQL ones) and wondered if and when I get around to doing the JavaScript stuff, or a different language (?) would I be able to do something like this? Thank you!

Fri 10-Jun-16 21:57:50

The question looks a bit like the Knapsack problem, which I vaguely recall from university days. To solve this problem, I'd probably create an algorithm which started by fitting in 8 of the lowest price sweets, and then worked by progressively switching out one or more of those sweets and replacing it with another at a higher value until you got a solution that met all criteria. I'm not sure how I would go about testing to see whether there were other solutions, which I suppose a good algorithm ought to do!

BelfastSmile Fri 10-Jun-16 22:03:34

You could, Let's simplify it by numbering the sweet types (1 is chews, 2 is mini eggs etc). So you have 4 types.
Then the total amount of money: 20.

Next, you need to know the combinations of sweets he can buy. So he could buy 8 chews, or 7 chews + 1 egg, or 6 chews, 1 egg and 1 lollipop and so on. You need to get it to loop over all the possibilities (I'll come to that in a second) and for each one, figure out the cost. If the cost is 20, then you have found a valid option, so print it out.

So, looping over the options (hopefully you understand for loops)

For(sweet1 type = 1 up to 4) {
For(sweet2 type = 1 up to 4) {
.... you'll have 8 for loops in total, for sweet 1 up to sweet 8...
For(sweet8 type = 1 up to 4) {

Calculate the total cost of sweet1 + sweet2 + ... + sweet8
If the cost is exactly 20, print out this result
}
}
}

This would print you out all the valid combinations, in this format: 1, 3, 3, 1, 2, 2, 2, 2. You could make it prettier by getting it to print out the names of the sweets instead of "1","2" etc. It will also print out duplicates, and take a long time to run - it can be made more efficient though (I'm too tired to work it out now).

And you could go really fancy and combine the types so it says "2 chews, 3 lollipops and 3 mini eggs", for example.

Parietal Fri 10-Jun-16 22:04:20

ok, I'll try to code it:

the problem is:
A chews: 2p
B eggs: 3p
C choco: 5p
D lolly: 7p

Henry spends 20p on 8 sweets

So I guess the answer is that he has 4 chews + 4 eggs.

but I'm not sure you could code that easily, because by the time you've coded it, you've worked out the answer.

if you want a general piece of code which can work out how much he spends on any 8 sweets, then I would do:

## first , set up a matrix of all the possible combinations of sweets
## first column is number of chews, second column is number of eggs etc.
comb = [8 0 0 0;
7 1 0 0;
6 2 0 0;
etc ];

## then work out the cost of each combination
for i=1 -to- length(comb)
cost(i) = 2* comb(i,1) + 3* comb(i,2) + 5*comb(i,3) + 7*comb(i,4)

if( cost(i)==20 )
print( 'it costs 20p to buy: comb(i,1) chews and comb(i,2) eggs ...)
end
end
##### note this code is kindof like Matlab (my best language) but not
## quite because I'm too lazy to include all the details

hope that makes sense!

MrsWombat Sat 11-Jun-16 07:37:02

There are at least 2 possible answers, as he got one and his dad and I independently got another combination. I didn't necessarily wanted to solve it only using a computer, but thought it might be a good example for him of what can be done with coding.

Thank you everyone! I will take a look when I get more confident with it all.

BelfastSmile I think your one makes the most sense to me, at the moment!

Join the discussion

Registering is free, easy, and means you can join in the discussion, get discounts, win prizes and lots more.

Register now