aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-15 17:39:14 +0100
committerGitHub <noreply@github.com>2021-08-15 17:39:14 +0100
commitd7c6e37182c3951b1b2ddae393b53f41fa189554 (patch)
tree5f7343cc8d3824f40416068d3df045ba01fd3fbf
parent7b6f3ea1e95dbba79a492ed384efe6c81468edde (diff)
parent51521874346ebd683e5027c9495d31b2385f7032 (diff)
downloadperlweeklychallenge-club-d7c6e37182c3951b1b2ddae393b53f41fa189554.tar.gz
perlweeklychallenge-club-d7c6e37182c3951b1b2ddae393b53f41fa189554.tar.bz2
perlweeklychallenge-club-d7c6e37182c3951b1b2ddae393b53f41fa189554.zip
Merge pull request #4714 from kaicb97/kai_burgdorf_solution_week_125
solution 125 perl ch-1
-rwxr-xr-xchallenge-125/kai-burgdorf/perl/ch-1.pl50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-125/kai-burgdorf/perl/ch-1.pl b/challenge-125/kai-burgdorf/perl/ch-1.pl
new file mode 100755
index 0000000000..bafc62c9a9
--- /dev/null
+++ b/challenge-125/kai-burgdorf/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use utf8;
+use strict;
+use warnings;
+
+my $N = 5;
+
+if($N == 0) {
+ print "(0, 0, 0)\n";
+ exit(0);
+}
+
+print "Input: \$N = $N\nOutput:\n";
+print "\t-1\n" if(!(n_as_hypotenuse($N) and n_as_cathete($N)));
+
+sub n_as_cathete {
+ my ($a) = @_;
+
+ my $c = 0;
+
+ while ($c**2 - ($c-1)**2 <= $a**2) {
+ for(1..($c-1)) {
+ if($c*$c - $_*$_ == $a*$a) {
+ print "\t($a, $_, $c)\n";
+ return 1;
+ }
+ }
+ $c++;
+ }
+
+ return 0;
+}
+
+
+sub n_as_hypotenuse {
+ my ($c) = @_;
+
+ for(1..($c-1)) {
+ my $a = $_;
+ for($a..($c-1)) {
+ if($a**2 + $_**2 == $c**2) {
+ print "\t($a, $_, $c)\n";
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}