use strict; my %numhash; for (my $n = 1; ; ++$n) { my $cube = $n ** 3; my $fixnum = join('', sort { $a <=> $b } split(//, $cube)); if (exists($numhash{$fixnum})) { push(@{$numhash{$fixnum}}, $cube); if (scalar(@{$numhash{$fixnum}}) == 5) { print (sort { $a <=> $b } (@{$numhash{$fixnum}})[0]),"\n"; exit(0); } } else { $numhash{$fixnum} = []; push(@{$numhash{$fixnum}}, $cube); } }