/* Sultan's children in Picat. From "Puzzle-based learning", page 80 """ Many years ago, a powerful sultan had the largest harem int the world. He had many children with his many wifes, and toward the end of his life the total number of his children was estimated to be between 100 and 500. However, the sultan kept the exact number of his children a secret: no one could provide a better estimation of this number. One day a foreign diplomat overheard a conversation between the sultan and his vizier. The sultan said: "If you select any two of my children at random, the probability that you selected two boys would be exactly 50 percent. This piece of information was sufficient for the diplomat to calculate the exact number of the sultan's children. How many children did the sultan have? """ b = 85 g = 35 total = 120 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 => B :: 1..500, % boys G :: 1..500, % girls % The equation at page 81 % (floats not supported by Picat's CP/SAT solvers) % (B*(B-1)) / ( (B+G)*(B+G-1) ) #= 0.5 % Variant % 2*(B*(B-1)) / ( (B+G)*(B+G-1) ) #= 1 % No division: 2*(B*(B-1)) #= ( (B+G)*(B+G-1)), Total #= B+G, Total #>= 100, Total #<= 500, solve([B,G,Total]), println(b=B), println(g=G), println(total=Total), nl, fail, nl. /* Alternative: The range of possible children i 1..5000: g = 1 total = 4 b = 15 g = 6 total = 21 b = 85 g = 35 total = 120 b = 493 g = 204 total = 697 b = 2871 g = 1189 total = 4060 */ go2 => B :: 1..5000, % boys G :: 1..5000, % girls 2*(B*(B-1)) #= ( (B+G)*(B+G-1)), Total #= B+G, solve($[ff,split],[B,G,Total]), println(b=B), println(g=G), println(total=Total), nl, fail, nl.