// // Maths challenge 2004-2005 J5 // // // My bank card has a 4 digit pin, abcd. I use the following facts to help me // remember it: // // - no two digits are the same // - the 2-digit number cd is 3 times the 2-digit number ab // - the 2-digit number da is 2 times the 2-digit number bc import static choco.Choco.*; import choco.cp.model.CPModel; import choco.cp.solver.CPSolver; import choco.kernel.model.Model; import choco.kernel.solver.Solver; import choco.kernel.model.variables.integer.IntegerExpressionVariable; import choco.kernel.model.variables.integer.IntegerVariable; public class MathChallenge2 { public static void main(String[] args) { Model m = new CPModel(); IntegerVariable a = makeIntVar("a",0,9); IntegerVariable b = makeIntVar("b",0,9); IntegerVariable c = makeIntVar("c",0,9); IntegerVariable d = makeIntVar("d",0,9); m.addConstraint(allDifferent(new IntegerVariable[]{a,b,c,d})); IntegerVariable ab = makeIntVar("ab",0,99); IntegerVariable bc = makeIntVar("bc",0,99); IntegerVariable cd = makeIntVar("cd",0,99); IntegerVariable da = makeIntVar("da",0,99); m.addConstraint(eq(ab,plus(mult(10,a),b))); m.addConstraint(eq(bc,plus(mult(10,b),c))); m.addConstraint(eq(cd,plus(mult(10,c),d))); m.addConstraint(eq(da,plus(mult(10,d),a))); m.addConstraint(eq(mult(3,ab),cd)); // 3.ab = cd m.addConstraint(eq(mult(2,bc),da)); // 2.bc = da Solver s = new CPSolver(); s.read(m); s.solve(); System.out.println(a.pretty()); System.out.println(b.pretty()); System.out.println(c.pretty()); System.out.println(d.pretty()); System.out.println(s.getVar(a).pretty()); System.out.println(s.getVar(b).pretty()); System.out.println(s.getVar(c).pretty()); System.out.println(s.getVar(d).pretty()); } } // // (unique) solution should be that abcd = 2163 //