/* Which hand puzzle in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" See https://www.researchgate.net/publication/374588335_Measuring_reasoning_capabilities_of_ChatGPT """ Puzzle 136. Which hand? Give a friend an "even" coin (say, a dime—ten is an even number) and an "odd" coin (say, a nickel). Ask him to hold one coin in his right hand and the other in his left. Tell him to triple the value of the coin in his right hand and double the value of the coin in his left, then add the two. If the sum is even, the dime is in his right hand; if odd, in his left. Explain, and think up some variations. (puzzle 44 from Kordemsky (1992)). """ Let's skip + 2 since it does not change the parity of the total: Right Left 3*Right 2*Left Total Total mod 2 Where is Even? ------------------------------------------------------------------------ 1 (odd) 2 (even) 3 (odd) 4 7 1 (odd) Even in Left 2 (even) 1 (odd) 6 (even) 2 8 0 (even) Even in Right The only way the Total is an odd number (mod 2 == 1) is when Right is odd and thus 3*Right is odd (since odd * odd = odd), and then Even is in Left. And the same way, if Right is even, then 3*Right is even (odd*even=even) * go/0: Simple example * go2/0: Slightly different formula and some examples. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => Dime = 10, % "Dime" all even numbers Left = Dime, Right = 5, println([left=Left,right=Right]), X = 3*Right + 2*Left + 2, % We don't need to add 2, adding 0 works just fine println(x=X=[3*Right,+,2*Left,+,2]), if X mod 2 == 0 then println(dime_in_right=Right) else println(dime_in_left=Left) end, fail, nl. /* Here's a variant: - pick two numbers, one even and one odd and assign one of them to Left and the other to Right - multiply the odd number with the right number and multiply the even number with the Left - sum these two numbers - if the sum is even then the even number is assigned to Right, other wise it's assigned to Left Some samples: [evenR = 104,oddR = 97,left = 97,right = 104] x = 20176 = [10088,+,10088] correct = right even_in_right = 104 ok [evenR = 124,oddR = 199,left = 199,right = 124] x = 49352 = [24676,+,24676] correct = right even_in_right = 124 ok [evenR = 44,oddR = 119,left = 119,right = 44] x = 10472 = [5236,+,5236] correct = right even_in_right = 44 ok [evenR = 146,oddR = 69,left = 69,right = 146] x = 20148 = [10074,+,10074] correct = right even_in_right = 146 ok [evenR = 90,oddR = 103,left = 90,right = 103] x = 18709 = [10609,+,8100] correct = left even_in_left = 90 ok [evenR = 104,oddR = 7,left = 104,right = 7] x = 10865 = [49,+,10816] correct = left even_in_left = 104 ok [evenR = 22,oddR = 79,left = 79,right = 22] x = 3476 = [1738,+,1738] correct = right even_in_right = 22 ok [evenR = 42,oddR = 7,left = 7,right = 42] x = 588 = [294,+,294] correct = right even_in_right = 42 ok [evenR = 74,oddR = 187,left = 187,right = 74] x = 27676 = [13838,+,13838] correct = right even_in_right = 74 ok [evenR = 74,oddR = 87,left = 74,right = 87] x = 13045 = [7569,+,5476] correct = left even_in_left = 74 ok */ go2 => _ = random2(), N = 100, member(_,1..10), % Generate 10 examples EvenR = random(1,N)*2, OddR = random(1,N)*2-1, if EvenR mod 2 == OddR mod 2 then println(strange=[evenR=EvenR,oddR=OddR]), halt end, if random() mod 2 == 0 then Left = EvenR, Right = OddR, Correct = left else Left = OddR, Right = EvenR, Correct = right end, println([evenR=EvenR,oddR=OddR,left=Left,right=Right]), % We can have any odd number in right and any even number in right. % Let's make it dynamic X = OddR*Right + EvenR*Left, % And we don't need to add 2, adding 0 works just fine println(x=X=[OddR*Right,+,EvenR*Left]), println(correct=Correct), if X mod 2 == 0 then println(even_in_right=Right) else println(even_in_left=Left) end, if X mod 2 == Right mod 2 then println(ok) else println(not_ok) end, nl, fail, nl.