fork download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4. no warnings qw(recursion);
  5.  
  6. package _Stream;
  7.  
  8. sub new {
  9. my $class = shift;
  10. return bless [@_], $class;
  11. }
  12.  
  13. sub value { $_[0]->[0] }
  14.  
  15. sub next {
  16. ref $_[0]->[1] eq 'CODE' ?
  17. $_[0]->[1] = $_[0]->[1]->() :
  18. $_[0]->[1]
  19. }
  20.  
  21. package main;
  22. use List::Util qw(reduce);
  23.  
  24. sub _maker {
  25. my ($i) = @_;
  26.  
  27. return _Stream->new(_sequencer($i, 1), sub{ _maker($i + 1) });
  28. }
  29.  
  30. sub _sequencer {
  31. my ($i, $j) = @_;
  32.  
  33. return _Stream->new(
  34. [$i, $j, $i ** 3 + $j ** 3],
  35. ($j < ($i - 1) ? sub{ _sequencer($i, $j + 1) } : sub{})
  36. );
  37. }
  38.  
  39. sub _sorted {
  40. my ($pool, $maker) = @_;
  41.  
  42. my @p = (@{$pool}, $maker->value);
  43.  
  44. my $n = reduce{
  45. $p[$a]->value->[2] <= $p[$b]->value->[2] ? $a : $b
  46. } (0 .. $#p);
  47.  
  48. return _Stream->new(
  49. $p[$n]->value,
  50. sub{
  51. _sorted(
  52. [
  53. @p[0 .. $n - 1],
  54. ($p[$n]->next // ()),
  55. @p[$n + 1 .. $#p - ($n == $#p ? 0 : 1)]
  56. ],
  57. ($n == $#p ? $maker->next : $maker)
  58. )
  59. }
  60. );
  61. }
  62.  
  63. sub _f {
  64. my ($old, $pool) = @_;
  65.  
  66. if ($old->[2] != $pool->value->[2]){
  67. return _f($pool->value, $pool->next);
  68. }
  69.  
  70. return _Stream->new(
  71. [@{$old}[0, 1], @{$pool->value}],
  72. sub{ _f($pool->value, $pool->next) }
  73. );
  74. }
  75.  
  76. sub f{ _f([0, 0, 0], _sorted([], _maker(2))) }
  77.  
  78. my $f = f();
  79. foreach(1 .. 30){
  80. printf("%d^3 + %d^3 = %d^3 + %d^3 = %d\n", @{$f->value});
  81. $f = $f->next;
  82. }
  83.  
Success #stdin #stdout 0.13s 7112KB
stdin
Standard input is empty
stdout
10^3 + 9^3 = 12^3 + 1^3 = 1729
15^3 + 9^3 = 16^3 + 2^3 = 4104
20^3 + 18^3 = 24^3 + 2^3 = 13832
24^3 + 19^3 = 27^3 + 10^3 = 20683
30^3 + 18^3 = 32^3 + 4^3 = 32832
33^3 + 15^3 = 34^3 + 2^3 = 39312
33^3 + 16^3 = 34^3 + 9^3 = 40033
30^3 + 27^3 = 36^3 + 3^3 = 46683
36^3 + 26^3 = 39^3 + 17^3 = 64232
33^3 + 31^3 = 40^3 + 12^3 = 65728
40^3 + 36^3 = 48^3 + 4^3 = 110656
45^3 + 27^3 = 48^3 + 6^3 = 110808
43^3 + 38^3 = 51^3 + 12^3 = 134379
50^3 + 29^3 = 53^3 + 8^3 = 149389
48^3 + 38^3 = 54^3 + 20^3 = 165464
54^3 + 24^3 = 55^3 + 17^3 = 171288
57^3 + 22^3 = 58^3 + 9^3 = 195841
59^3 + 22^3 = 60^3 + 3^3 = 216027
50^3 + 45^3 = 60^3 + 5^3 = 216125
60^3 + 36^3 = 64^3 + 8^3 = 262656
66^3 + 30^3 = 68^3 + 4^3 = 314496
66^3 + 32^3 = 68^3 + 18^3 = 320264
58^3 + 51^3 = 67^3 + 30^3 = 327763
60^3 + 54^3 = 72^3 + 6^3 = 373464
61^3 + 56^3 = 69^3 + 42^3 = 402597
69^3 + 48^3 = 76^3 + 5^3 = 439101
73^3 + 38^3 = 76^3 + 17^3 = 443889
75^3 + 45^3 = 80^3 + 10^3 = 513000
72^3 + 52^3 = 78^3 + 34^3 = 513856
71^3 + 54^3 = 80^3 + 15^3 = 515375