aboutsummaryrefslogtreecommitdiff
path: root/challenge-169
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-06-16 14:24:26 +0100
committerGitHub <noreply@github.com>2022-06-16 14:24:26 +0100
commitaa64948dbbcb3a643b4f23c939f0b8f7167558c3 (patch)
tree6bb6555ed29c17e6d1201181647b2a9a0f40ae74 /challenge-169
parentd71fa6f60a0797100b3e151ea7f93d8f0b428423 (diff)
parentb7a9905aa33838d19b74ca3160194982ef88a165 (diff)
downloadperlweeklychallenge-club-aa64948dbbcb3a643b4f23c939f0b8f7167558c3.tar.gz
perlweeklychallenge-club-aa64948dbbcb3a643b4f23c939f0b8f7167558c3.tar.bz2
perlweeklychallenge-club-aa64948dbbcb3a643b4f23c939f0b8f7167558c3.zip
Merge pull request #6273 from drbaggy/master
Tidying up and making more compact...
Diffstat (limited to 'challenge-169')
-rw-r--r--challenge-169/james-smith/README.md27
-rw-r--r--challenge-169/james-smith/perl/ch-1-npp.pl18
-rw-r--r--challenge-169/james-smith/perl/ch-1.pl6
-rw-r--r--challenge-169/james-smith/perl/ch-2-npp.pl18
-rw-r--r--challenge-169/james-smith/perl/ch-2.pl6
5 files changed, 58 insertions, 17 deletions
diff --git a/challenge-169/james-smith/README.md b/challenge-169/james-smith/README.md
index 3e9971e507..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; $n++ ) {
- 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, $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( $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, $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( $c, $n ) = 100; $c; ) {
+ $c--, say $n if 1 == gcd map{ $_->[1] < 2 ? next : $_->[1] } factor_exp ++$n;
}
```
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-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-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";
+
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";