diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-13 00:03:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-13 00:03:50 +0100 |
| commit | 6f46915900b1d91c55d7402ce6591c2f76e9f201 (patch) | |
| tree | f937e65fc25882f04167a0ec37ba164efc21ac9d | |
| parent | fae7785937591d9358556ef15cedd3092c80e59e (diff) | |
| parent | 80ddae74f22f8fc3184e5aa1d5c2947311092c80 (diff) | |
| download | perlweeklychallenge-club-6f46915900b1d91c55d7402ce6591c2f76e9f201.tar.gz perlweeklychallenge-club-6f46915900b1d91c55d7402ce6591c2f76e9f201.tar.bz2 perlweeklychallenge-club-6f46915900b1d91c55d7402ce6591c2f76e9f201.zip | |
Merge pull request #4704 from drbaggy/master
Updated readme..
| -rw-r--r-- | challenge-125/james-smith/README.md | 23 | ||||
| -rw-r--r-- | challenge-125/james-smith/blog.txt | 1 |
2 files changed, 24 insertions, 0 deletions
diff --git a/challenge-125/james-smith/README.md b/challenge-125/james-smith/README.md index b363e138a8..97ebd934cd 100644 --- a/challenge-125/james-smith/README.md +++ b/challenge-125/james-smith/README.md @@ -18,6 +18,29 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-125/ja ## The solution +There are two cases to consider - whether `$N` is the hypotenuse or one of the shorter sides. + + * `$N` is the hypotenuse - we just need to work out whether `$N**2 - $a**2` is a square for `$a` between `3` and `$N/sqrt 2` + + * `$N` is not the hypotenuse - we need to loop through `$a` from `$N+1` such that `$a**2-$N**2` is a square. + +This gives: +```perl +sub get_triples { + my $n = shift; + return $n < 3 ? -1 : join '; ', map { sprintf '(%s)', join ', ', @{$_} } + ( + grep { $_->[1] == int $_->[1] } ## Check if all int + map { [ $_, sqrt($n**2-$_**2), $n ] } ## Generate triple + 3 .. sqrt($n**2/2) ## Shortest side ($n is hypotenuse) + ),( + map { $_->[0]>$_->[1] ? [@{$_}[1,0,2]] : $_ } ## put in numerical order + grep { $_->[1] == int $_->[1] } ## Check all int + map { [ $n, sqrt($_**2-$n**2), $_ ] } ## Generate triple + ($n+1) .. ($n**2/2+1) ## Hypotenuse ($n is one of other two sides) + ); +} +``` # Task 2 - Binary Tree Diameter *** Write a script to find the diameter of the given binary tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. It doesn’t have to pass through the root.*** diff --git a/challenge-125/james-smith/blog.txt b/challenge-125/james-smith/blog.txt new file mode 100644 index 0000000000..6f8a2266f4 --- /dev/null +++ b/challenge-125/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/blob/master/challenge-125/james-smith/ |
