/* Determine Five numbers from Averages and Mode in Picat. From Thanushan Sivapatham "Math Challenge: How to Determine Five Numbers from Averages and Mode" https://medium.com/think-art/math-challenge-how-to-determine-five-numbers-from-averages-and-mode-894d523068d2 There are two problems stated in this post. The one in the text and than a picture which is another. The problem that seems to be the intended one (also in an image): """ puzzle If three numbers less than 11 have an average of 7, and two more numbers are added, the average of all five numbers becomes 5. Additionally, the model of these five numbers is 3. Find the five numbers (all are positive integers). """ And the problem first at the post: """ Find the (A,B,C,D,E) A+B+C = 21 Ayg(sic!) = 5 Mode = 3 """ [I assume that this image was generated by an chat-bot.] This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. /* First puzzle """ puzzle If three numbers less than 11 have an average of 7, and two more numbers are added, the average of all five numbers becomes 5. Additionally, the model of these five numbers is 3. Find the five numbers (all are positive integers). ... Therefore, after analyzing the conditions on the sums, averages, and the mode, we conclude that the five numbers satisfying all these criteria are:1,3,3,8,10 - The three original numbers are 3,8,10 - The two added numbers are 1,3 - The average of the three numbers is 7 - The average of all five numbers is 5 - The mode of the five numbers is 3 """ This model finds the solution (with symmetry breaking of L[1..3] and L[4..5]) [l = [3,8,10,1,3],sum = 25,mean3 = 7,mean = 5,mode = 3] */ go ?=> L=[A,B,C,D,E], L::0..120, L[1..3] :: 1..10, A+B+C#=21, mean(L[1..3],7), mean(L,5), mode(L,3), increasing(L[1..3]), % symmetry breaking increasing(L[4..5]), % symmetry breaking solve(L), mean(L[1..3],Mean3), mean(L,Mean), mode(L,Mode), println([l=L,sum=sum(L),mean3=Mean3,mean=Mean,mode=Mode]), fail, nl. go => true. /* The problem in the (chatbot generated) image: """ Find the (A,B,C,D,E) A+B+C = 21 Ayg(sic!) = 5 Mode = 3 """ Here are the solutions for this variant (with the same symmetry breaking as above): [l = [3,3,15,1,3],sum = 25,mean = 5,mode = 3] [l = [3,4,14,1,3],sum = 25,mean = 5,mode = 3] [l = [3,5,13,1,3],sum = 25,mean = 5,mode = 3] [l = [3,6,12,1,3],sum = 25,mean = 5,mode = 3] [l = [3,7,11,1,3],sum = 25,mean = 5,mode = 3] [l = [3,8,10,1,3],sum = 25,mean = 5,mode = 3] (These were also mentioned in a comment by Cemal Dincer.) */ go2 ?=> L=[A,B,C,D,E], L::0..120, A+B+C#=21, mean(L,5), mode(L,3), increasing(L[1..3]), % symmetry breaking increasing(L[4..5]), % symmetry breaking solve(L), mean(L,Mean), mode(L,Mode), println([l=L,sum=sum(L),mean=Mean,mode=Mode]), fail, nl. go2 => true. % % We assume integer values. % mean(X,Mean) => Mean*X.len #= sum(X). median(X,Median) => Median #= X[ceiling(X.len / 2)]. mode(X,Mode) => element(_,X,Mode), % Mode must be in the list % Count the number of occurrences of each number MaxVal = max([fd_max(X[I]) : I in 1..X.len]), GCC = new_list(MaxVal+1), GCC :: 0..X.len, global_cardinality(X,$[I-GCC[I] : I in 1..MaxVal]), % the max value is unique element(Mode,GCC,ModeNum), ModeNum #> 1, sum([V #= ModeNum : V in GCC ]) #= 1.