/* Sum and product of digits in age puzzle in Picat. From MindYourDecisions """ Lila’s age is the sum of the digits of her math teacher’s age. (Both have already had their birthdays this year.) In 5 years, Lila’s age will be the product of the digits of her math teacher’s age at that time. How old is Lila now? The teacher’s age is a two-digit number. """ Solution: [lila = 13,lila5 = 18,t1 = 5,t2 = 8,t15 = 6,t25 = 3] lila_age = 13 teacher_age = 58 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 ?=> % We don't know if Lila is <10 or >=10 years old member(Len,1..2), L = new_list(Len), L :: 0..9, % Lila's age now to_num(L,10,Lila), Lila :: 1..100, % Lila's age in 5 years Lila5 #= Lila + 5, % Teacher's age now (two digit number) [T1,T2] :: 0..9, T1*10 + T2 #>= 10, % Teacher's age in 5 years [T15,T25] :: 0..9, T15*10 + T25 #= (T1*10 + T2)+5, Lila #= T1+T2, Lila5 #= T15*T25, Vars = [lila=Lila,lila5=Lila5,t1=T1,t2=T2,t15=T15,t25=T25], solve(Vars), println(Vars), println(lila_age=Lila), println(teacher_age=(T1*10+T2)), fail, nl. go => true. /* Brute force: [13,58] */ go2 ?=> % Teachers's age is a two digit number foreach(T1 in 10..99) L1 = T1.to_string.map(to_int).sum, if L1 + 5 == (T1+5).to_string.map(to_int).prod then println([lila=L1,teacher=T1]) end end, nl. to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).