diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-21 14:08:51 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-21 14:08:51 +0000 |
| commit | 0ee472201f6c2f4768c8a3e2f44ea01082a02db1 (patch) | |
| tree | 3239aa24957c81231f8daa6b7256fd4a537846d8 /challenge-148/james-smith | |
| parent | 3653c728d921f8e1b3bd713921bc92213d638a2d (diff) | |
| parent | 68ab7d171c7ba4e28ff1a30430265967718ac840 (diff) | |
| download | perlweeklychallenge-club-0ee472201f6c2f4768c8a3e2f44ea01082a02db1.tar.gz perlweeklychallenge-club-0ee472201f6c2f4768c8a3e2f44ea01082a02db1.tar.bz2 perlweeklychallenge-club-0ee472201f6c2f4768c8a3e2f44ea01082a02db1.zip | |
Merge branch 'master' of github.com:drbaggy/perlweeklychallenge-club
Diffstat (limited to 'challenge-148/james-smith')
| -rw-r--r-- | challenge-148/james-smith/README.md | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/challenge-148/james-smith/README.md b/challenge-148/james-smith/README.md index 9e9c847415..bd31e18284 100644 --- a/challenge-148/james-smith/README.md +++ b/challenge-148/james-smith/README.md @@ -45,5 +45,23 @@ Which can be further parametrized as: Where *a=3.k-1* *k* starts at 1. So the first entry *k=1*, *b^2.c=5* - so is solved by *a=2*, *b=1*, *c=5*. + +So the code to find all cardano triplets with *a<10,000* is: + +```perl +for my $k (1..3333) { + for( my ($b, $n) = (1, $k*$k*(8*$k-3) ); $n > $b*$b; $b++ ) { + say join "\t", 3*$k-1, $b, $n/$b/$b unless $n%($b*$b); + } +} + +We loop through each value of `$k` up to 3,333, this gives the maximum value of `$a` 9,998. Largest less than or equal to 10,000. +We then loop `$b` from 1 up to the value where `$c < 1`. Rather than computing `$c` at this stage (there could be rounding errors). +We just compare the numerator (*k^2 . (8.k-3)*) with the denominator (*b^2*). We then check to see `$c` is an integer - we again +do this without computing `$c` to avoid rounding errors - to compute the results and display them. + +Time taken to caluclate these **32,235** cardano triplets is **78.5sec**. + +``` ### The result |
