aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-06-28 16:46:53 +0100
committerGitHub <noreply@github.com>2022-06-28 16:46:53 +0100
commit58f5e220ae958fe48453e45fedc079df6b466d54 (patch)
treee4eda5220ea247e37fc7cbc99ee0d2267ff1a502
parent88ea7f7dfd55ef1cd580e3b66649c9e10f29717f (diff)
downloadperlweeklychallenge-club-58f5e220ae958fe48453e45fedc079df6b466d54.tar.gz
perlweeklychallenge-club-58f5e220ae958fe48453e45fedc079df6b466d54.tar.bz2
perlweeklychallenge-club-58f5e220ae958fe48453e45fedc079df6b466d54.zip
Update README.md
-rw-r--r--challenge-171/james-smith/README.md19
1 files changed, 10 insertions, 9 deletions
diff --git a/challenge-171/james-smith/README.md b/challenge-171/james-smith/README.md
index 6fba608adf..610c168984 100644
--- a/challenge-171/james-smith/README.md
+++ b/challenge-171/james-smith/README.md
@@ -1,7 +1,7 @@
[< Previous 170](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-170/james-smith) |
[Next 172 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-172/james-smith)
-# The Weekly Challenge 171
+# The Weekly Challenge 171 - The last shall be first, and the first last.
You can find more information about this weeks, and previous weeks challenges at:
@@ -15,12 +15,13 @@ You can find the solutions here on github at:
https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-171/james-smith
-# The last shall be first, and the first last.
+# A topsy-turvey week.
-This week we will do two things which are contra to the norm - we will use `pop` to get parameters of a subroutine **AND** we write up task 2 before task 1 - you will understand when we get to challenge 1..
+This week we do two things which are contra to the norm - we use `pop` to get parameters of a subroutine **AND** we write up task 2 before task 1 - you will understand when we get to the end of task 1..
-# Challenge 2 - First-class Function
-***Create `sub compose($f, $g)` which takes in two parameters `$f` and `$g` as subroutine refs and returns subroutine ref i.e. `compose($f, $g)->($x) = $f->($g->($x))`***
+# Task 2 - First-class Function
+
+***Create `sub compose($f, $g)` which takes in two parameters `$f` and `$g` as subroutine refs and returns a subroutine ref i.e. `compose($f, $g)->($x) = $f->($g->($x))`***
The simple solution is:
@@ -39,7 +40,7 @@ Now it would be good if we could do this for multiple functions functions rather
my $f = compose( sub {}, compose( sub {}, compose( sub {}, sub {} ) ) );
```
-We can re-write `compose` to be a recursive function which composits the last two functions in the supplied list (and adds that composite to the end of the list), and repeats until the list has just a single function in it.
+We can re-write `compose` to be a recursive function which composits the last two functions in the supplied list (and adds that composite back to the end of the list), and repeats until the list has just a single function in it.
This leads to our first instance of *back to front* - we are so used to always using `shift` to remove parameters within a function, we rarely use `pop`. But in this case `pop` is the right method:
@@ -52,7 +53,7 @@ sub compose {
We grab the inner (first to be executed) function (and if it is the only function on the stack we return it) We the grab the next function and composit it with the first one, and push the resultant function back on the end of the list - and call `compose` again! Simples! As I said this is a case first shall be last, and last shall be first, as the first function that we will execute is the last one in the supplied list. Hence why `pop` is appropriate.
-# Challenge 1 - Abundant Number
+# Task 1 - Abundant Number
***Write a script to generate first 20 Abundant Odd Numbers. A number `n` for which the sum of divisors `σ(n) > 2n`, or, equivalently, the sum of proper divisors (or aliquot sum) `s(n) > n`.***
@@ -67,7 +68,7 @@ There are methods available to get all factors of a number (Math::Prime::Util ha
Firstly looping between `1` and `n` is a long loop especially is `n` is large. But we know that all factors (well nearly all) come in pairs if `f` is a factor of `n` then `f/n` is also a factor. This allows us to short cut the factor finding by only going as far as `sqrt(n)` to find the factors and include `f` and `n/f`. This is where we get to the little gotcha if `f` is the `sqrt(n)` we have to avoid including it twice! See the second line of the equation...
-Now note we usually sum from `0` - but we do something slightly different here (1) we start from `1` not zero as we don't include `1` in our search for facotrs. Additionally we subtract `n` as that leads a simpler to read comparision of `$s>0`.
+Now note we usually sum from `0` - but we do something slightly different here (1) we start from `1` not zero as we don't include `1` in our search for facotrs. Additionally we subtract `n` as that leads a simpler to read comparision of `$s>0`. This is something we will use in the alternative solution below.
In this example we merge the factor/sum stages together into a single loop.
@@ -88,7 +89,7 @@ We use the `redo` trick to return 20 results - `redo` calls the method in the lo
### Why we wrote about 2 before 1...
-I need a function to test task 2 out - so thought this would be a good problem.
+I needed a function to test task 2 out - so thought this would be a good problem.
We have the three stages -> factor -> sum -> test which we can male into 3 subs which we can compose together. Note we still do the `-n` trick to avoid passing `n` through from method to method.