// // 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 // // What are the values of a, b, c, and d? // import org.chocosolver.solver.Model; import org.chocosolver.solver.Solver; import org.chocosolver.solver.variables.IntVar; import org.chocosolver.solver.constraints.IIntConstraintFactory.*; import org.chocosolver.solver.search.strategy.Search; import org.chocosolver.solver.exception.ContradictionException; public class MathChallenge { public static void main(String[] args) throws ContradictionException { Model model = new Model("maths challenge"); Solver solver = model.getSolver(); IntVar a = model.intVar("a",0,9); IntVar b = model.intVar("b",0,9); IntVar c = model.intVar("c",0,9); IntVar d = model.intVar("d",0,9); model.allDifferent(new IntVar[]{a,b,c,d}).post(); IntVar ab = model.intVar("ab",0,99); IntVar bc = model.intVar("bc",0,99); IntVar cd = model.intVar("cd",0,99); IntVar da = model.intVar("da",0,99); // ab = 10.a + b IntVar a10 = model.intVar("10*a",new int[]{0,10,20,30,40,50,60,70,80,90}); model.arithm(a10,"=",a,"*",10); model.arithm(ab,"=",a10,"+",b).post(); // bc = 10.b + c IntVar b10 = model.intVar("10*b",new int[]{0,10,20,30,40,50,60,70,80,90}); model.arithm(b10,"=",b,"*",10); model.arithm(bc,"=",b10,"+",c).post(); // cd = 10.c + d IntVar c10 = model.intVar("10*c",new int[]{0,10,20,30,40,50,60,70,80,90}); model.arithm(c10,"=",c,"*",10); model.arithm(cd,"=",c10,"+",b).post(); // da = 10.d + a IntVar d10 = model.intVar("10*d",new int[]{0,10,20,30,40,50,60,70,80,90}); model.arithm(d10,"=",d,"*",10); model.arithm(da,"=",d10,"+",a).post(); // cd = 3.ab model.arithm(cd,"=",ab,"*",3).post(); // cd = 3*ab // da = 2.cd model.arithm(da,"=",bc,"*",2).post(); // da = 2*bc //solver.propagate(); // does this solve the problem System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(ab); System.out.println(bc); System.out.println(cd); System.out.println(da); System.out.println("there is a solution: "+ solver.solve()); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } } // // (unique) solution should be that abcd = 2163 // // (1) How do I know it is a unique solution? // (2) How much search effort was involved? // (3) Have we yet looked at propagate? i.e. is propagate enough? // (4) Can we make this all a bit simpler? Or is this just java? // - see two other versions produced by Charles //