Skip to content

Instantly share code, notes, and snippets.

@shayelkin
Last active August 29, 2015 14:01
Show Gist options
  • Save shayelkin/2397850f9b2c3df7e3aa to your computer and use it in GitHub Desktop.
Save shayelkin/2397850f9b2c3df7e3aa to your computer and use it in GitHub Desktop.
What is the smallest natural number that can not be generated using the numbers 1,2,3,4 (exactly once each) and four basic arithmetic operations?
import operator
from sys import maxint
from itertools import permutations, product
ops = [operator.add, operator.sub, operator.mul, operator.div]
possibilities = set(reduce(lambda v, t: t[0](v,t[1]), zip(o, n[1:]), n[0]) for n in permutations(range(1,5)) for o in product(ops, repeat=3))
# Without repeating operators:
# possibilities = set(reduce(lambda v, t: t[0](v,t[1]), zip(o, n[1:]), n[0]) for n in permutations(range(1,5)) for o in permutations(ops, repeat=3))
next(x for x in xrange(maxint) if x not in possibilities)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment