aboutsummaryrefslogtreecommitdiff
path: root/challenge-149/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-01-24 15:28:05 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-01-24 15:28:05 +0000
commita233878d10412fbc5b8a87ffb2d6af2e991e3693 (patch)
treea34bcd7a31460f2c4c52b9afd19ea484dd72cc8e /challenge-149/james-smith
parentd14e65562eb2ae5f5d0a1d26b3643d285ffbd93a (diff)
parent779989d8a5014e672ed8f74c1e7e4d2fb059c133 (diff)
downloadperlweeklychallenge-club-a233878d10412fbc5b8a87ffb2d6af2e991e3693.tar.gz
perlweeklychallenge-club-a233878d10412fbc5b8a87ffb2d6af2e991e3693.tar.bz2
perlweeklychallenge-club-a233878d10412fbc5b8a87ffb2d6af2e991e3693.zip
Merge branch 'master' of github.com:drbaggy/perlweeklychallenge-club
Diffstat (limited to 'challenge-149/james-smith')
-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’.)***