aboutsummaryrefslogtreecommitdiff
path: root/challenge-169/james-smith/perl/ch-2.pl
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-06-15 07:45:03 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-06-15 07:45:03 +0100
commitd24994b1b8ce7ab171910e3d7bccc4541b8d9875 (patch)
tree08782f9f3067c33946c37b2da8aab89580197d6d /challenge-169/james-smith/perl/ch-2.pl
parent367abf27a6d236b8d8dedfa17a6c7d9ad9e44566 (diff)
downloadperlweeklychallenge-club-d24994b1b8ce7ab171910e3d7bccc4541b8d9875.tar.gz
perlweeklychallenge-club-d24994b1b8ce7ab171910e3d7bccc4541b8d9875.tar.bz2
perlweeklychallenge-club-d24994b1b8ce7ab171910e3d7bccc4541b8d9875.zip
tidy up removing brackets
Diffstat (limited to 'challenge-169/james-smith/perl/ch-2.pl')
-rw-r--r--challenge-169/james-smith/perl/ch-2.pl47
1 files changed, 19 insertions, 28 deletions
diff --git a/challenge-169/james-smith/perl/ch-2.pl b/challenge-169/james-smith/perl/ch-2.pl
index a694061493..62a7e91631 100644
--- a/challenge-169/james-smith/perl/ch-2.pl
+++ b/challenge-169/james-smith/perl/ch-2.pl
@@ -8,37 +8,28 @@ use Math::Prime::Util qw(factor_exp gcd);
use Time::HiRes qw(time);
my $time = time;
-open my $fh, '>', 'achilles.txt';
-for( my( $n, $c, $MAX, @f ) = ( 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
+#
+# factor_exp returns a list of tuples - [ prime, exponent ]
+
+# If any of the 2nd entries in each tuple are 1 then we skip to the next number
+# as it isn't brilliant..
+#
+# To rule out perfect numbers we find the gcd of these values and if it is
+# greater than one we have a perfect number and skip to the next
+#
+# We have a brilliant imperfect (or archilles) number.... so display it..
+#
+# To pretty print the archilles numbers - we use our counter, and display
+# it alongside the number and the factorisation.
- ## 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
- ##
- ## factor_exp returns a list of tuples - [ prime, exponent ]
-
- ## 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 { "$_->[0]^$_->[1]" } @f
+for( my( $n, $c, $MAX, @f ) = ( 2, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) {
+ say sprintf '%6d: %15d = %s', ++$c, $n, join ' . ', map { "$_->[0]^$_->[1]" } @f
if 1 == gcd map { $_->[1] < 2 ? next : $_->[1] } @f = factor_exp $n;
-
- ## 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...
- ## Then we check to see if the number is imperfect by looking for a
- ## GCD of 1 between the indecies.
}
-close $fh;
+
warn 'Time taken: ', time-$time, "\n";