Write a number cruncher that works as follows:
Pick six (6) random numbers from this list:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100
Numbers may be picked multiple times.
Pick one (1) random number (i) in the range:
1 . . . 1000
Tell how, by combining the first 6 numbers or or subset with the operators +,-,* and /, you can make i;
An example. We have picked the numbers: 1, 6, 7, 8, 8 and 75. And i is 977. This can be done in many different ways, two ways are:
((((1 * 6) * 8) + 75) * 8) - 7 = 977
(8*(75+(8*6)))-(7/1) = 977
Another:
(((6*8)+75)*8)-7 = 977
When using only a subset. This one is without the number 1.
Credit for this particular exercise goes to JC van Winkel.
Tags: go
6 comments
That is a nice exercise. You can do this via 22 ways:
(((((1 * 6) * 8) + 75) * 8) - 7) = 977
(((((1 * 8) * 6) + 75) * 8) - 7) = 977
(((((6 * 1) * 8) + 75) * 8) - 7) = 977
(((((6 / 1) * 8) + 75) * 8) - 7) = 977
(((((6 * 8) * 1) + 75) * 8) - 7) = 977
(((((6 * 8) / 1) + 75) * 8) - 7) = 977
(((((6 * 8) + 75) * 1) * 8) - 7) = 977
(((((6 * 8) + 75) / 1) * 8) - 7) = 977
(((((6 * 8) + 75) * 8) * 1) - 7) = 977
(((((6 * 8) + 75) * 8) / 1) - 7) = 977
(((((6 * 8) + 75) * 8) - 7) * 1) = 977
(((((6 * 8) + 75) * 8) - 7) / 1) = 977
(((((8 * 1) * 6) + 75) * 8) - 7) = 977
(((((8 / 1) * 6) + 75) * 8) - 7) = 977
(((((8 * 6) * 1) + 75) * 8) - 7) = 977
(((((8 * 6) / 1) + 75) * 8) - 7) = 977
(((((8 * 6) + 75) * 1) * 8) - 7) = 977
(((((8 * 6) + 75) / 1) * 8) - 7) = 977
(((((8 * 6) + 75) * 8) * 1) - 7) = 977
(((((8 * 6) + 75) * 8) / 1) - 7) = 977
(((((8 * 6) + 75) * 8) - 7) * 1) = 977
(((((8 * 6) + 75) * 8) - 7) / 1) = 977
Hmm, according to JC there should 544 solutions... Either I'm doing something wrong or ...
Stupid - and / and commutative property. Now plan B.
What I find interesting is that there must be combinations of randomly picked numbers that cannot be solved to result in i. Suppose your random numbers are 1, 1, 1, 2, 3, and 4 and i is 999, for instance. So apart from the list of correct solutions, there is also the solution: impossible.
So exercise B (for bonus credits ;) could be:
Given the whole range of input numbers above in *all* random combinations, and the range 1 ... 1000 for i, list the combinations that *cannot* be solved.
Not that I would try to progam this in Go myself, by the way :D
So exercise B (for bonus credits ;) could be:
Given the whole range of input numbers above in *all* random combinations, and the range 1 ... 1000 for i, list the combinations that *cannot* be solved.
Not that I would try to progam this in Go myself, by the way :D
There are 10 billion "solutions", most of which has an illegal syntax. Clearly generating them all is going to take a long time. So plan B now is: a recursive back tracking algorithm.
There a 544 solution to this particular exercise. Pff

