diff options
| -rw-r--r-- | challenge-169/james-smith/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-169/james-smith/perl/ch-2.pl | 47 |
2 files changed, 25 insertions, 35 deletions
diff --git a/challenge-169/james-smith/perl/ch-1.pl b/challenge-169/james-smith/perl/ch-1.pl index 30f49fd117..029bc36c6c 100644 --- a/challenge-169/james-smith/perl/ch-1.pl +++ b/challenge-169/james-smith/perl/ch-1.pl @@ -8,16 +8,15 @@ use Math::Prime::Util qw(factor); use Time::HiRes qw(time); my $time = time; -open my $fh, '>', 'factor.txt'; -## Within each loop we get a factorisation -## must have preciesely 2 prime factors -## THEN each factor must be the same length; +# Within each loop we get a factorisation +# must have preciesely 2 prime factors +# THEN each factor must be the same length; + for( my( $n, $c, $MAX, @f ) = ( 0, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) { - say {$fh} sprintf '%8d: %10d = %5d x %d', ++$c, $n, @f - if 2 == ( @f=factor $n ) && length( $f[0] ) == length $f[1]; + say sprintf '%8d: %10d = %5d x %d', ++$c, $n, @f + if 2 == ( @f = factor $n ) && length $f[0] == length $f[1]; } -close $fh; warn 'Time taken: ', time-$time, "\n"; 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"; |
