#!/usr/bin/perl
use 5.016;
use warnings;
sub f {
my ($n) = @_;
my %memo;
my @cube;
my $i = 1;
while(1){
$cube[$i] = $i ** 3;
foreach my $j (1 .. $i - 1){
my $t = $cube[$i] + $cube[$j];
--$n or return ($i, $j, @{$memo{$t}}, $t); }
$memo{$t} = [$i, $j];
}
$i++;
}
}
printf("%d^3 + %d^3 = %d^3 + %d^3 = %d\n", f
(2));
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwoKc3ViIGYgewoJbXkgKCRuKSA9IEBfOwoJbXkgJW1lbW87CglteSBAY3ViZTsKCglteSAkaSA9IDE7Cgl3aGlsZSgxKXsKCQkkY3ViZVskaV0gPSAkaSAqKiAzOwoJCWZvcmVhY2ggbXkgJGogKDEgLi4gJGkgLSAxKXsKCQkJbXkgJHQgPSAkY3ViZVskaV0gKyAkY3ViZVskal07CgkJCWlmIChleGlzdHMgJG1lbW97JHR9KXsKCQkJCS0tJG4gb3IgcmV0dXJuICgkaSwgJGosIEB7JG1lbW97JHR9fSwgJHQpOwoJCQl9CgkJCSRtZW1veyR0fSA9IFskaSwgJGpdOwoJCX0KCQkkaSsrOwoJfQp9CgpwcmludGYoIiVkXjMgKyAlZF4zID0gJWReMyArICVkXjMgPSAlZFxuIiwgZigyKSk7Cg==