diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-06-14 21:37:48 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-06-14 21:37:48 +0100 |
| commit | 4e67de07fef7fabe2a2ebd5d371d8644c83de1b9 (patch) | |
| tree | ebe1bee2a0844a7e14732d1063de4b6b1e8d3df6 /challenge-169/james-smith/perl | |
| parent | 57aa4c32f6b056ec9b5f5530f364c1f814ca5f7a (diff) | |
| download | perlweeklychallenge-club-4e67de07fef7fabe2a2ebd5d371d8644c83de1b9.tar.gz perlweeklychallenge-club-4e67de07fef7fabe2a2ebd5d371d8644c83de1b9.tar.bz2 perlweeklychallenge-club-4e67de07fef7fabe2a2ebd5d371d8644c83de1b9.zip | |
updatead notes
Diffstat (limited to 'challenge-169/james-smith/perl')
| -rw-r--r-- | challenge-169/james-smith/perl/ch-2.pl | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-169/james-smith/perl/ch-2.pl b/challenge-169/james-smith/perl/ch-2.pl index d385b4239a..6588e57cce 100644 --- a/challenge-169/james-smith/perl/ch-2.pl +++ b/challenge-169/james-smith/perl/ch-2.pl @@ -11,8 +11,29 @@ my $time = time; open my $fh, '>', 'achilles.txt'; for( my( $n, $c, $MAX ) = ( 2, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) { + + ## Factorise $n into prime factors, and count for each factor... + ## + ## e.g. 10800 has factors ( 2, 2, 2, 2, 3, 3, 3, 5, 5 ) or 2^4 3^3 5^2 + ## + ## So %factors = ( 2 => 4, 3 => 3, 5 => 2 ); + ## + my %factors; $factors{$_}++ for factor $n; + + ## If any of the values in this hash are 1 then we skip to the next number + ## as it isn't brilliant.. + ## + ## To rule out perfect numbers we find the gcd of the values and if it is + ## greater than 1 we have a perfect number and skip to the next number + ## + ## We have a brilliant imperfect (or archilles) number.... so display it.. + ## + ## To pretty print the archilles numbers - we have a counter so that we + ## know which achilles number it is, we display the number AND the + ## factorisation. + say {$fh} sprintf '%6d: %15d = %s', ++$c, $n, join ' . ', map { "$_^$factors{$_}" } @@ -20,6 +41,9 @@ for( my( $n, $c, $MAX ) = ( 2, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) { keys %factors if 1 == gcd( grep { $_ < 2 ? next : 1 } values %factors ) ## Any of the factors is not squared we try the next number in the loop! + + ## The `next` out of grep jumps to the next loop of the foor loop... + } close $fh; warn 'Time taken: ', time-$time, "\n"; |
