/*
Euler problem 44
"""
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 Pop-11 program was created by Hakan Kjellerstrand (hakank@bonetmail.com).
See also my Pop-11 / Poplog page: http://www.hakank.org/poplog/
*/
compile('/home/hakank/Poplib/init.p');
define pent(n);
n*(3*n-1)/2;
enddefine;
define problem44;
lvars T = newmapping([], 10000, 0, true);
lvars j,k,a,b;
lvars n;
lvars s = [% for n to 2500 do pent(n) endfor %];
for n in s do
1->T(n);
endfor;
lvars d = 100000000;
lvars jc = 1;
for j in s do
for k in s do
if j > k then
nextloop;
endif;
lvars a = j+k;
if T(a) = 0 then
nextloop;
endif;
lvars b = abs(j-k);
if T(b) = 0 then
nextloop;
endif;
if b < d then
b->d;
endif;
endfor;
endfor;
d=>
enddefine;
'problem44()'=>
problem44();