aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-01-24 15:27:43 +0000
committerGitHub <noreply@github.com>2022-01-24 15:27:43 +0000
commit779989d8a5014e672ed8f74c1e7e4d2fb059c133 (patch)
tree9d6651bc1eb6afd8e8b14c91093ec9943685804c
parent004ffc8ebf81ea5b8525e88c1e0a81a007537579 (diff)
downloadperlweeklychallenge-club-779989d8a5014e672ed8f74c1e7e4d2fb059c133.tar.gz
perlweeklychallenge-club-779989d8a5014e672ed8f74c1e7e4d2fb059c133.tar.bz2
perlweeklychallenge-club-779989d8a5014e672ed8f74c1e7e4d2fb059c133.zip
Update README.md
-rw-r--r--challenge-149/james-smith/README.md21
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-149/james-smith/README.md b/challenge-149/james-smith/README.md
index a755db373e..9ebbcb0a39 100644
--- a/challenge-149/james-smith/README.md
+++ b/challenge-149/james-smith/README.md
@@ -20,6 +20,27 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-149/ja
## The solution
+```perl
+for( my($n,$ds,$i,$fa,$fb,%fib)=(@ARGV?$ARGV[0]:20,0,0,1,1,0,1,1,1);
+ $n; $i++,$ds=0 ) { ## 1
+ $ds+=$_ foreach split //,$i; ## 2
+ ($fib{$fa+$fb},$fa,$fb)=(1,$fb,$fa+$fb) if $ds > $fb; ## 3
+ (say $i)**$n-- if exists $fib{$ds}; ## 4
+}
+```
+
+**Notes:**
+
+ * Line 1 - We initialise everything inside the for loop
+ * `$n` is the number to print (and is based on what is passed at the command line)
+ * `$ds` is the digit sum (Note we reset it everytime through the loop in the incremement part of the loop
+ * `$i` current value being considered
+ * `$fa` & `$fb` - the highest two fibonacci numbers
+ * `%fib` hash whose keys are fibonacci numbers
+ * Line 2 - Computes the digit sum by splitting number on `//` I split into 1 character blocks
+ * Line 3 - Expand the fibonacci hash by 1 if the digit sum is greater than the highest fibonnaci number {we don't need to loop this as the digit sum of `$n+1` can only be at most 1 higher than that for `$n`. Note we just update $fb and $fa in this line
+ * Line 4 - Check to see if the digit sum exists, print and decrement counter - and return to the start of the loop.
+
# Challenge 2 - Largest Square
***Given a number base, derive the largest perfect square with no repeated digits and return it as a string. (For base>10, use ā€˜A’..ā€˜Z’.)***