#!/usr/bin/env setl
--
-- Project Euler problem 44 in SETL
--
-- """
-- Pentagonal numbers are generated by the formula, P(n)=n(3nā1)/2.
-- The first ten pentagonal numbers are:
-- 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
-- It can be seen that P(4) + P(7) = 22 + 70 = 92 = P(8). However,
-- their difference, 70 ā 22 = 48, is not pentagonal.
-- Find the pair of pentagonal numbers, P(j) and P(k), for which their sum
-- and difference is pentagonal and D = |P(k) ā P(j)| is minimised; what
-- is the value of D?
--
-- """
--
-- This SETL program was created by Hakan Kjellerstrand (hakank@bonetmail.com)
-- Also see my SETL page: http://www.hakank.org/setl/
--
problem44();
proc pent(n);
return n*(3*n-1)/2;
end;
proc problem44;
T := {};
s := [pent(n) : n in [1..2500]];
for n in s loop
T(n) := 1;
end loop;
d := 100000000;
for j in s loop
--print("j:", j);
for k in s | j < k loop
--print("\tk:", k);
if j > k then
print("j>k");
continue;
end;
a := j+k;
if T(a) = om then
continue;
end;
b := abs(j-k);
if T(b) = om then
continue;
end;
if b < d then
d := b;
end;
end;
end;
print(d);
end;