aboutsummaryrefslogtreecommitdiff
path: root/challenge-125
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-08-16 13:03:46 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-08-16 13:03:46 +0100
commit53202dee121a79b59f76cc7eb434672d543146a8 (patch)
treea903f3ebf60a4a18db625323c132911b866f0a79 /challenge-125
parent6efb25dc769e9d974cc90950bb7b75213dab3124 (diff)
downloadperlweeklychallenge-club-53202dee121a79b59f76cc7eb434672d543146a8.tar.gz
perlweeklychallenge-club-53202dee121a79b59f76cc7eb434672d543146a8.tar.bz2
perlweeklychallenge-club-53202dee121a79b59f76cc7eb434672d543146a8.zip
- Added solution by Peter Campbell Smith.
Diffstat (limited to 'challenge-125')
-rw-r--r--challenge-125/peter-campbell-smith/perl/ch-1.pl55
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-125/peter-campbell-smith/perl/ch-1.pl b/challenge-125/peter-campbell-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..6411b1c54d
--- /dev/null
+++ b/challenge-125/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-08-08
+# PWC 125 task 1
+
+use strict;
+use warnings;
+
+# A Pythagorean triple consist of 3 positive integers such that
+# a**2 + b**2 = c**2. Note that a cannot equal b as sqrt(2a**2) =
+# a * sqrt(2) which cannot be integer.
+
+# We are asked to find all the triples where one of a, b, c equals
+# a given number n.
+
+# Clearly a < c and b < c, and we can require a < b to avoid
+# duplicates such a 3, 4, 5 and 4, 3, 5.
+
+my ($a, $b, $c, $n, $s, @solutions);
+
+# input n
+print qq[n: ];
+$n = <>;
+chomp($n);
+
+# If c == n:
+# a < b so a**2 < n**2 / 2, so we only need to test values of a < sqrt(n**2 / 2)
+# and solutions are where n**2 - a**2 is an integer square (ie b**2)
+
+for $a (1 .. int(sqrt($n**2 / 2))) {
+ $solutions[$s++] = qq[ ($a, $b, $n)\n] if $b = is_a_square($n**2 - $a**2);
+}
+
+# If a == n:
+# We are looking for b and c such that c**2 - b**2 == n**2.
+# if t == s + 1 then t**2 - s**2 is 2s + 1, so we only
+# need to test values of b where 2b + 1 < n**2
+# and solutions are where n**2 - b**2 is an integer square (ie a**2)
+
+for $b (1 .. 2**32) {
+ last if 2 * $b + 1 > $n**2;
+ $solutions[$s++] = qq[ ($n, $b, $c)\n] if $c = is_a_square($n**2 + $b**2);
+}
+
+# solutions
+print qq[Input: \$N = $n\nOutput:\n];
+for $s (@solutions) { print $s };
+print qq[-1\n] unless (scalar @solutions);
+
+#---
+
+sub is_a_square {
+ my $test = sqrt($_[0]);
+ return $test == int($test) ? $test : 0;
+}