Skip to content

Instantly share code, notes, and snippets.

@nudomarinero
Last active August 29, 2015 14:21
Show Gist options
  • Save nudomarinero/8928056304d72ef5d278 to your computer and use it in GitHub Desktop.
Save nudomarinero/8928056304d72ef5d278 to your computer and use it in GitHub Desktop.
#!/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