/* Project Euler #60 in Picat. http://projecteuler.net/problem=60 """ Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => euler60. % % 0.88s % euler60 ?=> Primes = primes(700), member(A,Primes), member(B,[P : P in Primes, P > A]), A < B, member(C,[P : P in Primes, P > B]), B < C, member(D,[P : P in Primes, P > C]), C < D, L1 = [A,B,C,D], L = [A.number_chars(),B.number_chars(), C.number_chars(),D.number_chars()], OK = true, foreach(X in L, Y in L, X != Y, OK = true) if not prime((X ++ Y).to_integer()) then OK := false end end, if OK then Sum = sum(L1), println([L1,Sum]) else fail % backtrack end, nl. euler60 => true. % % Much slower: 45s % euler60b ?=> Primes = primes(700), MinSum = 1000000, MinSums = [], foreach(A in Primes, B in Primes, A < B, C in Primes, B < C, D in Primes, C < D % , E in [P: P in Primes, P > D], % D < E ) AS = A.to_string(), BS = B.to_string(), CS = C.to_string(), DS = D.to_string(), % ES = D.to_string(), L1 = [A,B,C,D], L = [AS,BS,CS,DS], % L1 = [A,B,C,D,E], % L = [AS,BS,CS,DS,ES], % println(L), OK = true, foreach(X in L, Y in L, X != Y, OK = true) if not prime((X ++ Y).to_integer()) then OK := false end end, if OK then Sum = sum(L1), println([L1,Sum]), if Sum < MinSum then MinSum := Sum, MinSums := L end end end, println(result=[MinSum,MinSums]), nl. euler60b => true. table prime_c(N) => prime(N).