5 ms·
The obligatory Python says that there are 120 solutions, or %0.03 of the possible permutations. It seems almost cruel to subject 8 year-olds to a problem like t
by msvan 11y ago
The obligatory Python says that there are 120 solutions, or %0.03 of the possible permutations. It seems almost cruel to subject 8 year-olds to a problem like this, even if they have enough intuition to reduce the problem space a bit. Maybe it says more about the teacher than the students.
from itertools import permutations
def f(a, b, c, d, e, f, g, h, i):
return a + 13 * b / c + d + 12 * e - f - 11 + g * h / i - 1
solutions = [p for p in permutations(range(1, 10)) if f(*p) == 66]
print(len(solutions))
- tempestn 11y agoAgreed. I kept holding out hope that there was some clever solution I just couldn't see, but no, the only sane solution is to do exactly what you did and write a script. What's the point in making students slog through endlessly plugging numbers into a weirdly formatted equation?
- facepalm 11y agoRaise them to be diligent workers who never complain about boring tasks?
- IanCal 11y agoYou need to swap the 1 at the end for an 11. Also, this code for me returns 2672 solutions although I don't believe all are valid due to integer division. Perhaps a python 2 -> python 3 difference? (py2 here)
- acqq 11y agoAt the end should be 10. And with checks to have only divisions without the remainder I get 20 solutions, the first one being (3, 2, 1, 5, 4, 7, 8, 9, 6)?
- IanCal 11y agoYes, I get the same. Edit - Ah yes, 10, sorry.
- ust 11y agoI have the same number, do you think there is a reason why they are not all valid? EDIT: actually the number I calculate is 2796. Also using Python 2..
- IanCal 11y agoPartly it might be due to needing "-10" at the end rather than -1 (I think I may have corrected it to -11 by mistake). However, larger is the difference in how division works. Python2: 3 / 2 => 1 Python3: 3 / 2 => 1.5
- ust 11y agoWould forcing the integer result fix this problem? Something like: def f(a, b, c, d, e, f, g, h, i): return a + int((13 * b * i + g * h * c) / (i * c)) + d + 12 * e - f - 11 - 10 If I try it like this, I still have the same number of solutions... Maybe I'm missing something.. EDIT: code formatting...
- IanCal 11y agoNo, the problem is the other way around. Keeping them as integers and just using '/' is what's causnig the problem. You can either force them to be floats (not the result, the parameters in the calculation), or you can explicitly look for divisions that have remainders.
- Spidler 11y ago128 actually, you missed the last char of your snake. ( -1 should be -10 )
- tzs 11y agoThat script (with the -1 at the end replaced with the correct -10) does not work, at least on Python2. It misses the solution (1, 8, 3, 7, 4, 5, 2, 6, 9). Here's a fixed script: from itertools import permutations def f(a, b, c, d, e, f, g, h, i): return a + (13 * b * i + g * h * c) / (i * c) + d + 12 * e - f - 11 - 10 solutions = [p for p in permutations(range(1, 10)) if f(*p) == 66] print(len(solutions))
- deleted 11y ago[deleted]