My feed
Premium

Please
or
to access all these features

Geeky stuff

Coding maths problems?

4 replies

MrsWombat · 10/06/2016 21:42

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!

OP posts:
Report
LemonEmmaP · 10/06/2016 21:57

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!

Report
BelfastSmile · 10/06/2016 22:03

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.

Report
Parietal · 10/06/2016 22:04

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) + 5comb(i,3) + 7comb(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!
Report
MrsWombat · 11/06/2016 07:37

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

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

OP posts:
Report
Please create an account

To comment on this thread you need to create a Mumsnet account.