algol<
begin
comment
https://projecteuler.net/problem=61
Answer: 28684
Time: 8030.38s = 2h 13m 50.38s
Time: Classic: 7893.65s
Time: Turbo: 6974.42s
No buffer, no index check:
Time classic: 7938.42
Time turbo: 7036.65 11.4pct
Buffer, no index check:
Time classic: 6655.42
Time turbo: 6091.44 8.5pct
No buffer, index check:
Time classic: 7893.65
Time turbo: 7045.71 10.7pct
Buffer, index check:
Time classic: 8032.21
Time turbo: 7153.58 10.9pct
PERM code taken from APL/360 ADVANCEDEX PERM function.
;
real clock;
real procedure clock count;
code clock count;
1, 37;
zl , grf p−1 ; RF ≔ clock count; stack[p−1] ≔ RF;
e;
integer procedure triangle(n);
value n;
integer n;
begin
integer n2;
n2 ≔ −0.5+sqrt(0.25+2×n);
triangle ≔ if (n2×(n2+1)÷2)=n then n2 else 0
end;
integer procedure square(n);
value n;
integer n;
begin
integer n2;
n2 ≔ sqrt(n);
square ≔ if n2×n2=n then n2 else 0
end square;
integer procedure pentagonal(n);
value n;
integer n;
begin
integer n2;
n2 ≔ (0.5+sqrt(0.25+6×n))/3;
pentagonal ≔ if (n2×(3×n2−1)÷2)=n then n2 else 0
end;
integer procedure hexagonal(n);
value n;
integer n;
begin
integer n2;
n2 ≔ (1+sqrt(1+8×n))/4;
hexagonal ≔ if n2×(2×n2−1)=n then n2 else 0
end hexagonal;
integer procedure heptagonal(n);
value n;
integer n;
begin
integer n2;
n2 ≔ (1.5+sqrt(2.25+10×n))/5;
heptagonal ≔ if n2×(5×n2−3)÷2=n then n2 else 0
end heptagonal;
integer procedure octagonal(n);
value n;
integer n;
begin
integer n2;
n2 ≔ (2+sqrt(4+12×n))/6;
octagonal ≔ if n2×(3×n2−2)=n then n2 else 0
end octagonal;
procedure PERM(Z,a,b);
value a,b;
integer a,b;
integer array Z;
begin
integer i,j,rem;
rem ≔ b−1;
for i ≔ 1 step 1 until a do
begin
Z[a−i+1] ≔ 1+rem mod i;
rem ≔ rem÷i
end;
for i ≔ a−1 step −1 until 1 do
for j ≔ i+1 step 1 until a do
if Z[i]⩽Z[j] then Z[j] ≔ Z[j]+1
end PERM;
integer array list,Z[1:6];
integer i,j;
integer aa,bb,cc,dd,ee,ff,
aabb,bbcc,ccdd,ddee,eeff,ffaa;
clock count;
for aa ≔ 10 step 1 until 99 do
begin
for bb ≔ 10 step 1 until 99 do
begin
if aa=bb then goto notbb;
list[1] ≔ aabb ≔ aa×100+bb;
if octagonal(aabb)=0 then goto notbb;
for cc ≔ 10 step 1 until 99 do
begin
if aa=cc then goto notcc;
if bb=cc then goto notcc;
list[2] ≔ bbcc ≔ bb×100+cc;
if heptagonal(bbcc)=0 ∧
hexagonal(bbcc)=0 ∧
pentagonal(bbcc)=0 ∧
square(bbcc)=0 ∧
triangle(bbcc)=0 then goto notcc;
for dd ≔ 10 step 1 until 99 do
begin
if aa=dd then goto notdd;
if bb=dd then goto notdd;
if cc=dd then goto notdd;
list[3] ≔ ccdd ≔ cc×100+dd;
if heptagonal(ccdd)=0 ∧
hexagonal(ccdd)=0 ∧
pentagonal(ccdd)=0 ∧
square(ccdd)=0 ∧
triangle(ccdd)=0 then goto notdd;
for ee ≔ 10 step 1 until 99 do
begin
if aa=ee then goto notee;
if bb=ee then goto notee;
if cc=ee then goto notee;
if dd=ee then goto notee;
list[4] ≔ ddee ≔ dd×100+ee;
if heptagonal(ddee)=0 ∧
hexagonal(ddee)=0 ∧
pentagonal(ddee)=0 ∧
square(ddee)=0 ∧
triangle(ddee)=0 then goto notee;
for ff ≔ 10 step 1 until 99 do
begin
if aa=ff then goto notff;
if bb=ff then goto notff;
if cc=ff then goto notff;
if dd=ff then goto notff;
if ee=ff then goto notff;
list[5] ≔ eeff ≔ ee×100+ff;
if heptagonal(eeff)=0 ∧
hexagonal(eeff)=0 ∧
pentagonal(eeff)=0 ∧
square(eeff)=0 ∧
triangle(eeff)=0 then goto notff;
list[6] ≔ ffaa ≔ ff×100+aa;
if heptagonal(ffaa)=0 ∧
hexagonal(ffaa)=0 ∧
pentagonal(ffaa)=0 ∧
square(ffaa)=0 ∧
triangle(ffaa)=0 then goto notff;
for i ≔ 1 step 1 until 720 do
begin
PERM(Z,6,i);
if triangle(list[Z[1]])=0 then goto notperm;
if square(list[Z[2]])=0 then goto notperm;
if pentagonal(list[Z[3]])=0 then goto notperm;
if hexagonal(list[Z[4]])=0 then goto notperm;
if heptagonal(list[Z[5]])=0 then goto notperm;
if octagonal(list[Z[6]])=0 then goto notperm;
writecr;
write(«dddddd»,aabb,bbcc,ccdd,ddee,eeff,ffaa,
aabb+bbcc+ccdd+ddee+eeff+ffaa);
notperm: end for i;
notff: end ff;
notee: end ee;
notdd: end dd;
notcc: end cc;
notbb: end bb;
end aa;
clock ≔ clock count;
writecr;
write(«dddddddd.dd»,clock)
end;
t<