Last active
August 29, 2015 14:21
-
-
Save nudomarinero/8928056304d72ef5d278 to your computer and use it in GitHub Desktop.
Solutions to the problem proposed in http://www.theguardian.com/science/alexs-adventures-in-numberland/2015/may/20/can-you-do-the-maths-puzzle-for-vietnamese-eight-year-olds-that-has-stumped-parents-and-teachers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Solutions to the problem proposed in http://gu.com/p/493zk/stw | |
""" | |
from itertools import permutations | |
from decimal import Decimal | |
def p(x): | |
""" | |
Define the problem. Follow the canonical order of operations | |
""" | |
return x[0]+13*x[1]/x[2]+x[3]+12*x[4]-x[5]-11+x[6]*x[7]/x[8]-10 | |
# List of possible values. We will use exact arithmetic to avoid numerical problems. | |
values = [Decimal(b) for b in range(1,10)] | |
# The possible solutions are found | |
sol = [a for a in permutations(values) if (p(a) == Decimal(66))] | |
print len(sol) # There are 136 possible solutions | |
for solution in sol: | |
print [int(b) for b in solution] # Print the values as integers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment