$tot = 0; %terms = (); for ($j = 1; $j < 1000000; $j++) { my %found = (); $found{$j} = 1; my $r = expandFact($j); @k = keys %found; while (! $found{$r} && ($#k < 62)) { $found{$r} = 1; $r = expandFact($r); @k = keys %found; } if ($r != $j) { if (($#k + 1) == 60) { $tot++; } } } print "Total with 60 repeating terms: " . $tot . "\n"; exit; sub expandFact { my $x = shift; return 1 if ($x == 1); my $tot = 0; for (my $q = 0; $q < length($x); $q++) { $z = substr($x,$q,1); #generate factorial my $n = 1; $n *= $_ foreach (2 .. $z); $tot += $n; } return $tot; }