diff options
| author | James Smith <baggy@baggy.me.uk> | 2021-06-07 21:30:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-07 21:30:57 +0100 |
| commit | c99eaa2c9ee884b120e13ccb8fca0c622fd30bed (patch) | |
| tree | 5cad73d6dbaf0790dd13e5de80dc978ffd172cde /challenge-116/james-smith | |
| parent | 10f48d8d374e9d8734a59c3218993fda5c7a9ecb (diff) | |
| download | perlweeklychallenge-club-c99eaa2c9ee884b120e13ccb8fca0c622fd30bed.tar.gz perlweeklychallenge-club-c99eaa2c9ee884b120e13ccb8fca0c622fd30bed.tar.bz2 perlweeklychallenge-club-c99eaa2c9ee884b120e13ccb8fca0c622fd30bed.zip | |
Update README.md
Diffstat (limited to 'challenge-116/james-smith')
| -rw-r--r-- | challenge-116/james-smith/README.md | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/challenge-116/james-smith/README.md b/challenge-116/james-smith/README.md index e65394fd3e..0f0f90a9c8 100644 --- a/challenge-116/james-smith/README.md +++ b/challenge-116/james-smith/README.md @@ -18,15 +18,13 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-116/ja ## The solution -We first need to get the possible leading numbers.. to do this we split the -string into individual digits. We then concatenate each digit on to the end +We first need to get the possible leading numbers.. to do this we split the string into individual digits. We then concatenate each digit on to the end of the sequence (`$start.=$_`).... -Within each loop we just stitch together the string by incrementing the -number each time through the loop.. +Within each loop we just stitch together the string by incrementing the number each time through the loop.. + * We use string (in)equalities/incremements so this will work with arbitrarily large numbers (see examples in script) * We reduce the maximum calculations by a factor of 2 by spliting just the first half of the string - * We use string (in)equalities so this will work with arbitrary numbers. * We have to check length as well as the strings in the while condition (because we are using string comparison) ```perl @@ -40,7 +38,6 @@ sub splitnum { } return [$in]; } - ``` # Challenge 2 - Sum of Squares @@ -55,9 +52,9 @@ Just split the input number and sum the square of it's digits... Then just retur ```perl sub sum_square { - my $sum = 0; ## Initialize sum - $sum += $_*$_ foreach split //, shift; ## Sum digits.. - return $sum == (int sqrt $sum)**2 || 0; ## Check is squared + my $sum = 0; ## Initialize sum + $sum += $_*$_ for split //, shift; ## Sum digits.. + $sum == (int sqrt $sum)**2 || 0; ## Check is squared } ``` |
