diff options
| author | Roger Bell_West <roger@firedrake.org> | 2019-08-06 16:46:15 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2019-08-06 16:46:15 +0100 |
| commit | f2f8c5a62728a03a1346f02707e4ccb228f797e5 (patch) | |
| tree | b5bf83f22dbdcc526685b4c6814b1a72c77ea227 | |
| parent | 4b8b136f68f8ee1881b1b7e88bdb6c554eda7b59 (diff) | |
| download | perlweeklychallenge-club-f2f8c5a62728a03a1346f02707e4ccb228f797e5.tar.gz perlweeklychallenge-club-f2f8c5a62728a03a1346f02707e4ccb228f797e5.tar.bz2 perlweeklychallenge-club-f2f8c5a62728a03a1346f02707e4ccb228f797e5.zip | |
Better divisors calculation, rather faster
| -rwxr-xr-x | challenge-020/roger-bell-west/perl6/ch-2.p6 | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/challenge-020/roger-bell-west/perl6/ch-2.p6 b/challenge-020/roger-bell-west/perl6/ch-2.p6 index 9f095df46b..519f2c8866 100755 --- a/challenge-020/roger-bell-west/perl6/ch-2.p6 +++ b/challenge-020/roger-bell-west/perl6/ch-2.p6 @@ -4,17 +4,11 @@ my $a=1; while (1) { $a++; my @a=divisors_unself($a); - unless (@a) { - next; - } my $b=@a.sum; if ($b <= $a) { next; } my @b=divisors_unself($b); - unless (@b) { - next; - } my $aa=@b.sum; if ($aa == $a) { print "$a, $b\n"; @@ -23,10 +17,14 @@ while (1) { sub divisors_unself ($k) { my @d=(1); - for 2..$k/2.Int -> $d { + my @e; + for 2..$k.sqrt.Int -> $d { if ($k % $d == 0) { - push @d,$d; + push @d,$d,$k/$d; + if @d[*-1] == $d { + pop @d; + } } } return @d; -}
\ No newline at end of file +} |
