/* Increasing by percentage problem (CP approach) in Picat. Mind your decisions: "Seemingly Impossible 7th Grade Math Problem" https://mindyourdecisions.com/blog/2018/05/21/seemingly-impossible-7th-grade-math-riddle-the-coded-table/ https://www.youtube.com/watch?v=PAslxl1nIT4&feature=youtu.be """ Seemingly Impossible 7th Grade Math Problem All numbers in the first column have been incresed by the same percentage, P, to give the result in the second column. The result numbers are coded; each digit is replaced by a letter. A given letter stands for the same digit every time it appears in that column. What is the value of P, the percentage increase? 20 GC 15 IJ 45 AC 35 CG 70 JC 80 HF 25 EB 60 DG """ Note: This problem don't need solve/1 since Picat is able to find out the solution before. Here's how Picat calculates the values. After four examples the problem is solved. [ff = 20,pct = DV_01918_100..200,aa = DV_015b0_2..4,bb = DV_01600_0..9] [ff = 15,pct = DV_01918_100..200,aa = DV_01650_1..3,bb = DV_016a0_0..9] [ff = 45,pct = DV_01918_100..200,aa = DV_016f0_4..9,bb = DV_01600_0..9] [ff = 35,pct = DV_01918_120..180,aa = DV_01600_4..6,bb = DV_015b0_2..3] [ff = 70,pct = 120,aa = 8,bb = 4] [ff = 80,pct = 120,aa = 9,bb = 6] [ff = 25,pct = 120,aa = 3,bb = 0] [ff = 60,pct = 120,aa = 7,bb = 2] Full solution: pct = 20 20*1.20 = 24 15*1.20 = 18 45*1.20 = 54 35*1.20 = 42 70*1.20 = 84 80*1.20 = 96 25*1.20 = 30 60*1.20 = 72 Compare with the MIP approach: http://hakank.org/picat/increasing_by_percentage_mip.pi This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> % The two columns First = [20,15,45,35,70,80,25,60], Second = [[G,C],[I,J],[A,C],[C,G],[J,C],[H,F],[E,B],[D,G]], Vars = Second.flatten().remove_dups(), Vars :: 0..9, Pct :: 100..200, % Here we see how Picat progress to find out the % possible values in each loop (see the output above). foreach({FF,[AA,BB]} in zip(First,Second)) FF*Pct #= 100*(AA*10+BB), println([ff=FF,pct=Pct,aa=AA,bb=BB]) end, % We don't need solve/1 % solve(Vars), println("\nFull solution:"), println(pct=(Pct-100)), foreach({FF,[AA,BB]} in zip(First,Second)) printf("%d*%0.2f = %d%d\n", FF,Pct/100,AA,BB) end, fail, nl. go => true.