/*
Euler #49 in Picat.
"""
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms
increases by 3330, is unusual in two ways: (i) each of the three terms are
prime, and, (ii) each of the 4-digit numbers are permutations of one another.
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes,
exhibiting this property, but there is one other 4-digit increasing sequence.
What 12-digit number do you form by concatenating the three terms
in this sequence?
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import util.
main => go.
go => time(euler49).
euler49 =>
Diff = 3330,
Res := 0,
foreach(N in 1001..2..9999,
N != 1487,
prime(N),
C = check_perms(N, Diff),
C != [])
Res := C
end,
Result = "",
foreach(I in Res) Result := Result ++ I.to_string() end,
println(Result).
euler49b =>
Diff = 3330,
Res := 0,
foreach(N in 1001..2..9999, prime(N))
if prime(N) then
C = check_perms(N, Diff),
if C != [] then
if N != 1487 then
Res := C
end
end
end
end,
writeln(res=Res),
Result = "",
foreach(I in Res) Result := Result ++ I.to_string() end,
println(result=Result).
check_perms(N, Diff) = LL =>
LL := [],
AllPerms := permutations([I.to_integer() : I in N.to_string()]),
if AllPerms.length > 0 then
P1 = 0,
P2 = 0,
P1 := get_element(N, AllPerms, Diff),
if P1 > 0 then
P2 := get_element(P1, AllPerms, Diff)
end,
if P2 > 0 then
LL := [N, P1, P2]
end
end.
get_element(N, LL, Diff) = Res =>
Res := 0,
foreach(P in LL)
PP = [I.to_string() : I in P].flatten().to_integer(),
if PP > N, PP-N == Diff then
Res := PP
end
end.