From 6f9f9e196da10865b1698b8045720b81e7442c3b Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 16 Jun 2022 06:55:55 +0100 Subject: Update README.md --- challenge-169/james-smith/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-169/james-smith/README.md b/challenge-169/james-smith/README.md index 3e9971e507..ecf04f777f 100644 --- a/challenge-169/james-smith/README.md +++ b/challenge-169/james-smith/README.md @@ -29,8 +29,8 @@ We loop through all numbers, checking to see if (a) the number has exactly 2 pri For flexibility we define the max count `$MAX` as the command-line argument if one is supplied (or 100 if not). ```perl -for( my( $n, $c, $MAX, @f ) = ( 0, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) { - say sprintf '%7d: %10d = %5d x %d', ++$c, $n, @f if 2 == ( @f = factor $n ) && length $f[0] == length $f[1]; +for( my( $n, $c, $MAX, @f ) = ( 0, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; ) { + say sprintf '%7d: %10d = %5d x %d', ++$c, $n, @f if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; } ``` @@ -102,8 +102,8 @@ The output in each row is the brilliant number and the two primes which are it's If we remove the pretty print this reduces to: ```perl -for( my( $n, $c, $MAX, @f ) = ( 0, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) { - $c++, say $n if 2 == ( @f = factor $n ) && length $f[0] == length $f[1]; +for( my($n,$c,@f) = (0,100); $c; ) { + $c--, say $n if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; } ``` @@ -190,8 +190,8 @@ The following are the first 50 achilles numbers. If we remove the pretty print this reduces to: ```perl -for( my( $n, $c, $MAX, @f ) = ( 2, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; $n++ ) { - $c++, say $n if 1 == gcd map { $_->[1] < 2 ? next : $_->[1] } @f = factor_exp $n; +for( my( $n, $c ) = ( 2, 100 ); $c; $n++ ) { + $c--, say $n if 1 == gcd map { $_->[1] < 2 ? next : $_->[1] } factor_exp $n; } ``` -- cgit From c1ebaf2f59df349c8e82cda7f43ce40a5a431525 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 16 Jun 2022 10:00:10 +0100 Subject: updates to make code more compact --- challenge-169/james-smith/perl/ch-1-npp.pl | 18 ++++++++++++++++++ challenge-169/james-smith/perl/ch-2-npp.pl | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge-169/james-smith/perl/ch-1-npp.pl create mode 100644 challenge-169/james-smith/perl/ch-2-npp.pl diff --git a/challenge-169/james-smith/perl/ch-1-npp.pl b/challenge-169/james-smith/perl/ch-1-npp.pl new file mode 100644 index 0000000000..ce0b88c777 --- /dev/null +++ b/challenge-169/james-smith/perl/ch-1-npp.pl @@ -0,0 +1,18 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Math::Prime::Util qw(factor); +use Time::HiRes qw(time); + +my $time = time; + +#------------------------------------------------------------------------------- +for( my( $c, $n, @f ) = 100; $c; ) { + $c--, say $n if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; +} + +warn 'Time taken: ', time-$time, "\n"; + diff --git a/challenge-169/james-smith/perl/ch-2-npp.pl b/challenge-169/james-smith/perl/ch-2-npp.pl new file mode 100644 index 0000000000..a3d686dc25 --- /dev/null +++ b/challenge-169/james-smith/perl/ch-2-npp.pl @@ -0,0 +1,18 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Math::Prime::Util qw(factor_exp gcd); +use Time::HiRes qw(time); + +my $time = time; + +#------------------------------------------------------------------------------- +for( my( $c, $n ) = ( 100 ); $c; ) { + $c--, say $n if 1 == gcd map{ $_->[1] < 2 ? next : $_->[1] } factor_exp ++$n; +} + +warn 'Time taken: ', time-$time, "\n"; + -- cgit From 68f76f4aab3253674ac22ee3a0dd7cab3f41f139 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 16 Jun 2022 10:02:30 +0100 Subject: tidied up --- challenge-169/james-smith/perl/ch-1.pl | 6 +++--- challenge-169/james-smith/perl/ch-2.pl | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-169/james-smith/perl/ch-1.pl b/challenge-169/james-smith/perl/ch-1.pl index 029bc36c6c..775040f182 100644 --- a/challenge-169/james-smith/perl/ch-1.pl +++ b/challenge-169/james-smith/perl/ch-1.pl @@ -13,9 +13,9 @@ my $time = time; # 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 sprintf '%8d: %10d = %5d x %d', ++$c, $n, @f - if 2 == ( @f = factor $n ) && length $f[0] == length $f[1]; +for( my( $MAX, $c, $n, @f ) = ($ARGV[0] // 1e2,0); $c < $MAX; ) { + printf "%8d: %10d = %5d x %d\n", ++$c, $n, @f + if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; } 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 62a7e91631..c55acdc58d 100644 --- a/challenge-169/james-smith/perl/ch-2.pl +++ b/challenge-169/james-smith/perl/ch-2.pl @@ -26,9 +26,9 @@ my $time = time; # To pretty print the archilles numbers - we use our counter, and display # it alongside the number and the factorisation. -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; +for( my( $MAX, $c, $n, @f ) = ($ARGV[0] // 1e2,0); $c<$MAX; ) { + say sprintf '%6d: %15d = %s', ++$c, $n, join ' . ', map { join '^', @$_ } @f + if 1 == gcd map { $_->[1] < 2 ? next : $_->[1] } @f = factor_exp ++$n; } warn 'Time taken: ', time-$time, "\n"; -- cgit From b7a9905aa33838d19b74ca3160194982ef88a165 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 16 Jun 2022 10:02:46 +0100 Subject: Update README.md --- challenge-169/james-smith/README.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/challenge-169/james-smith/README.md b/challenge-169/james-smith/README.md index ecf04f777f..ba9c64a2b4 100644 --- a/challenge-169/james-smith/README.md +++ b/challenge-169/james-smith/README.md @@ -29,12 +29,12 @@ We loop through all numbers, checking to see if (a) the number has exactly 2 pri For flexibility we define the max count `$MAX` as the command-line argument if one is supplied (or 100 if not). ```perl -for( my( $n, $c, $MAX, @f ) = ( 0, 0, @ARGV ? $ARGV[0] : 1e2 ); $c<$MAX; ) { - say sprintf '%7d: %10d = %5d x %d', ++$c, $n, @f if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; +for( my( $MAX, $c, $n, @f ) = ($ARGV[0] // 1e2, 0); $c<$MAX; ) { + printf "%8d: %10d = %5d x %d\n", ++$c, $n, @f if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; } ``` -This logic is wrapped up int the single `if`. As we check the number of factors, we store these in an array, so that we can check the 2nd condition. +This logic is wrapped up in the single `if`. As we check the number of factors, we store these in an array, so that we can check the 2nd condition. The output in each row is the brilliant number and the two primes which are it's factors. @@ -42,7 +42,7 @@ The output in each row is the brilliant number and the two primes which are it's **Note:** to make the code easier to read we use a *Yoda* condition, where we reverse the value and the code evaluation - so instead if say `$a == 2` we say `2 == $a`. -**Moan:** Why is there no `sayf` function similar to `printf` - using `say sprintf` seems a bit "messy" each time... +**Moan:** Why is there no `sayf` function similar to `printf` - using `say sprintf` is just "messy" each time... ``` 1: 4 = 2 x 2 @@ -102,11 +102,16 @@ The output in each row is the brilliant number and the two primes which are it's If we remove the pretty print this reduces to: ```perl -for( my($n,$c,@f) = (0,100); $c; ) { +for( my( $c, $n, @f ) = 100; $c; ) { $c--, say $n if 2 == ( @f = factor ++$n ) && length $f[0] == length $f[1]; } ``` +**Notes:** As well as moving the pretty print (and the $MAX for simplicity) we do three things: + * We start $c at the number of entries we want, and decrement the counter each time - the end condition is then simple `$c == 0` or as we write a "continue" condition that is just `$c`. + * As above we remove the increment from the `for()` to the entry - in this case ++$n. As we don't initialize `$n`, we have to "pre-increment" so that `$n` is defined when factor is called. + * Finally to display the brilliant number AND decrement `$c`, we separate the two commands with a `,` as `$c--, say $n`. + # Challenge 2 - Achilles Number ***Write a script to generate first 20 Achilles Numbers. An Achilles number is a number that is powerful but imperfect (not a perfect power). Named after Achilles, a hero of the Trojan war, who was also powerful but imperfect.*** @@ -126,9 +131,9 @@ We then check to see if any of the factors does not have its square as a factor. We then compute the `gcd` of these powers - if it is 1 then we display the result - our output is index, value and the prime factorisation, most of the loop is for the pretty print. ```perl -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; +for( my( $MAX, $c, $n, @f ) = ($ARGV[0] // 1e2, 0); $c<$MAX; ) { + printf "%6d: %15d = %s\n", ++$c, $n, join ' . ', map { join '^', @$_ } @f + if 1 == gcd map { $_->[1] < 2 ? next : $_->[1] } @f = factor_exp ++$n; } ``` @@ -190,8 +195,8 @@ The following are the first 50 achilles numbers. If we remove the pretty print this reduces to: ```perl -for( my( $n, $c ) = ( 2, 100 ); $c; $n++ ) { - $c--, say $n if 1 == gcd map { $_->[1] < 2 ? next : $_->[1] } factor_exp $n; +for( my( $c, $n ) = 100; $c; ) { + $c--, say $n if 1 == gcd map{ $_->[1] < 2 ? next : $_->[1] } factor_exp ++$n; } ``` -- cgit