#!/usr/bin/perl
use 5.016;
use warnings;
no warnings
qw(recursion
);
sub new {
}
sub value { $_[0]->[0] }
sub next {
ref $_[0]->[1] eq 'CODE' ? $_[0]->[1] = $_[0]->[1]->() :
$_[0]->[1]
}
use List
::Util qw(reduce
);
sub _maker {
my ($i) = @_;
return _Stream
->new(_sequencer
($i, 1), sub{ _maker
($i + 1) }); }
sub _sequencer {
my ($i, $j) = @_;
[$i, $j, $i ** 3 + $j ** 3],
($j < ($i - 1) ? sub{ _sequencer($i, $j + 1) } : sub{})
);
}
sub _sorted {
my ($pool, $maker) = @_;
my @p = (@{$pool}, $maker->value);
my $n = reduce{
$p[$a]->value->[2] <= $p[$b]->value->[2] ? $a : $b
} (0 .. $#p);
$p[$n]->value,
sub{
_sorted(
[
@p[0 .. $n - 1],
($p[$n]->next // ()),
@p[$n + 1 .. $#p - ($n == $#p ? 0 : 1)]
],
($n == $#p ? $maker->next : $maker)
)
}
);
}
sub _f {
my ($old, $pool) = @_;
if ($old->[2] != $pool->value->[2]){
return _f
($pool->value, $pool->next); }
[@{$old}[0, 1], @{$pool->value}],
sub{ _f($pool->value, $pool->next) }
);
}
sub f{ _f([0, 0, 0], _sorted([], _maker(2))) }
my $f = f();
foreach(1 .. 30){
printf("%d^3 + %d^3 = %d^3 + %d^3 = %d\n", @{$f->value}); $f = $f->next;
}
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwpubyB3YXJuaW5ncyBxdyhyZWN1cnNpb24pOwoKcGFja2FnZSBfU3RyZWFtOwoKc3ViIG5ldyB7CglteSAkY2xhc3MgPSBzaGlmdDsKCXJldHVybiBibGVzcyBbQF9dLCAkY2xhc3M7Cn0KCnN1YiB2YWx1ZSB7ICRfWzBdLT5bMF0gfQoKc3ViIG5leHQgewoJcmVmICRfWzBdLT5bMV0gZXEgJ0NPREUnID8KCQkkX1swXS0+WzFdID0gJF9bMF0tPlsxXS0+KCkgOgoJCSRfWzBdLT5bMV0K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