Skip to content

Instantly share code, notes, and snippets.

@Tarliton
Created August 25, 2020 18:20
Show Gist options
  • Save Tarliton/c3aaa77645b14f458f403e9c031160a0 to your computer and use it in GitHub Desktop.
Save Tarliton/c3aaa77645b14f458f403e9c031160a0 to your computer and use it in GitHub Desktop.
Solution to MindYourDecisions video https://www.youtube.com/watch?v=9dyK_op-Ocw with constraint programming
from constraint import *
def fact(n):
f = 1
for i in range(1,n+1):
f = f * i
return f
problem = Problem()
# you should choose these wisely
problem.addVariable('a', list(range(0, 10)))
problem.addVariable('b', list(range(0, 10)))
problem.addVariable('c', list(range(0, 10)))
def func(a, b, c):
fact_a = fact(a)
fact_b = fact(b)
return fact_a*fact_b == fact_a + fact_b + fact(c)
problem.addConstraint(func)
print(problem.getSolutions())
# prints [{'a': 3, 'b': 3, 'c': 4}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment