diff options
| author | Abigail <abigail@abigail.be> | 2021-08-11 00:33:29 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-08-11 00:33:29 +0200 |
| commit | 9fc9a8a98be3336fb1bd9fb909c68af45186744e (patch) | |
| tree | 7d641a5ae4dd9a9f4a41202bd43b33e606e82892 | |
| parent | 3b86d11379632b17ca5328d8b92756e4950f0bfc (diff) | |
| download | perlweeklychallenge-club-9fc9a8a98be3336fb1bd9fb909c68af45186744e.tar.gz perlweeklychallenge-club-9fc9a8a98be3336fb1bd9fb909c68af45186744e.tar.bz2 perlweeklychallenge-club-9fc9a8a98be3336fb1bd9fb909c68af45186744e.zip | |
Solutions in 6 languages for week 125, part 1
| -rw-r--r-- | challenge-125/abigail/README.md | 23 | ||||
| -rw-r--r-- | challenge-125/abigail/c/ch-1.c | 52 | ||||
| -rw-r--r-- | challenge-125/abigail/lua/ch-1.lua | 48 | ||||
| -rw-r--r-- | challenge-125/abigail/node/ch-1.js | 47 | ||||
| -rw-r--r-- | challenge-125/abigail/perl/ch-1.pl | 69 | ||||
| -rw-r--r-- | challenge-125/abigail/python/ch-1.py | 41 | ||||
| -rw-r--r-- | challenge-125/abigail/ruby/ch-1.rb | 45 |
7 files changed, 302 insertions, 23 deletions
diff --git a/challenge-125/abigail/README.md b/challenge-125/abigail/README.md index 155bfc8302..f8c6f2595b 100644 --- a/challenge-125/abigail/README.md +++ b/challenge-125/abigail/README.md @@ -29,35 +29,12 @@ Output: -1 ~~~~ ### Solutions -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [Basic](basic/ch-1.bas) -* [bc](bc/ch-1.bc) -* [Befunge-93](befunge-93/ch-1.bf93) * [C](c/ch-1.c) -* [Cobol](cobol/ch-1.cb) -* [Csh](csh/ch-1.csh) -* [Erlang](erlang/ch-1.erl) -* [Forth](forth/ch-1.fs) -* [Fortran](fortran/ch-1.f90) -* [Go](go/ch-1.go) -* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) -* [m4](m4/ch-1.m4) -* [MMIX](mmix/ch-1.mms) * [Node.js](node/ch-1.js) -* [Ocaml](ocaml/ch-1.ml) -* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) -* [Php](php/ch-1.php) -* [Postscript](postscript/ch-1.ps) * [Python](python/ch-1.py) -* [R](r/ch-1.r) -* [Rexx](rexx/ch-1.rexx) * [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) -* [SQL](sql/ch-1.sql) -* [Tcl](tcl/ch-1.tcl) ### Blog [Perl Weekly Challenge 125: Pythagorean Triples][blog1] diff --git a/challenge-125/abigail/c/ch-1.c b/challenge-125/abigail/c/ch-1.c new file mode 100644 index 0000000000..63b3a67db1 --- /dev/null +++ b/challenge-125/abigail/c/ch-1.c @@ -0,0 +1,52 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <math.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +typedef long long number; + +number introot (number square) { + return ((number) floorl (.4 + sqrt (square))); +} + +int main (void) { + number n; + + while (scanf ("%lld", &n) == 1) { + if (n <= 2) { + printf ("-1\n"); + continue; + } + + number n_sq = n * n; + number c = n + 1; + number c_sq = n_sq + 2 * n + 1; + while (2 * c - 1 <= n_sq) { + number b_sq = c_sq - n_sq; + number b = introot (b_sq); + if (b_sq == b * b) { + printf ("%lld %lld %lld\n", n, b, c); + } + c_sq += 2 * c ++ + 1; + } + + number max_a = (number) floorl (n / sqrt (2)); + for (number a = 3; a <= max_a; a ++) { + number b_sq = n_sq - a * a; + number b = introot (b_sq); + if (b_sq == b * b) { + printf ("%lld %lld %lld\n", a, b, n); + } + } + } + + return (0); +} diff --git a/challenge-125/abigail/lua/ch-1.lua b/challenge-125/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..1a0efb40c8 --- /dev/null +++ b/challenge-125/abigail/lua/ch-1.lua @@ -0,0 +1,48 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +function introot (square) + return (math . floor (.4 + math . sqrt (square))) +end + + +for line in io . lines () do + local n = tonumber (line) + if n <= 2 then + print (-1) + goto end_loop + end + + local n_sq = n * n + local c = n + 1 + local c_sq = n_sq + 2 * n + 1 + while 2 * c - 1 <= n_sq do + local b_sq = c_sq - n_sq + local b = introot (b_sq) + + if b_sq == b * b then + print (n .. " " .. b .. " " .. c) + end + + c_sq = c_sq + 2 * c + 1 + c = c + 1 + end + + local max_a = math . floor (n / math . sqrt (2)) + for a = 3, max_a do + local b_sq = n_sq - a * a + local b = introot (b_sq) + if b_sq == b * b then + print (a .. " " .. b .. " " .. n) + end + end + + ::end_loop:: +end diff --git a/challenge-125/abigail/node/ch-1.js b/challenge-125/abigail/node/ch-1.js new file mode 100644 index 0000000000..a8c8c9c2c4 --- /dev/null +++ b/challenge-125/abigail/node/ch-1.js @@ -0,0 +1,47 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => main (+ line)) + +function introot (square) { + return (Math . floor (.4 + Math . sqrt (square))) +} + +function main (n) { + if (n <= 2) { + console . log (-1) + return + } + + let n_sq = n * n + let c = n + 1 + let c_sq = n_sq + 2 * n + 1 + while (2 * c - 1 <= n_sq) { + let b_sq = c_sq - n_sq + let b = introot (b_sq) + + if (b_sq == b * b) { + console . log (n + " " + b + " " + c) + } + + c_sq += 2 * c ++ + 1 + } + + let max_a = Math . floor (n / Math . sqrt (2)) + for (let a = 3; a <= max_a; a ++) { + let b_sq = n_sq - a * a + let b = introot (b_sq) + if (b_sq == b * b) { + console . log (a + " " + b + " " + n) + } + } +} diff --git a/challenge-125/abigail/perl/ch-1.pl b/challenge-125/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..c62b9b92d3 --- /dev/null +++ b/challenge-125/abigail/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +sub introot ($square) { + # + # Return the largest integer less than, or equal to the + # square root of $square. + # + int (.4 + sqrt ($square)); +} + + + +while (my $n = <>) { + chomp ($n); + say (-1), next if $n <= 2; + # + # First case, $n is not the hypothenuse; wlog, assume n = a. + # + # Then, we start searching from c = n + 1 until + # c^2 - (c - 1)^2 > n^2. Note that c^2 - (c - 1)^2 = 2c - 1 + # In each iteration, we calculate b^2 = c^2 - n^2. If b^2 is + # a proper square, we have a Pythagorian triple. + # + my $n_sq = $n * $n; + + my $c = $n + 1; + my $c_sq = $n_sq + 2 * $n + 1; + while (2 * $c - 1 <= $n_sq) { + # + # We now have a^2 (n_sq) and c^2. We can calculate b^2 (b_sq) + # and check whether this is a proper square. + # + my $b_sq = $c_sq - $n_sq; + my $b = introot ($b_sq); + + say "$n $b $c" if $b_sq == $b * $b; + $c_sq += 2 * $c ++ + 1; # (c + 1)^2 == c^2 + 2 * c + 1 + } + + # + # Handle the case $n is the hypothenuse, so $n == c. + # + # We now need to search for a, b such that a^2 + b^2 = c^2 ($n_sq). + # Wlog, assume a < b (a == b cannot happen). Then a < c / sqrt (2). + # + my $max_a = int ($n / sqrt (2)); + for (my $a = 3; $a <= $max_a; $a ++) { + my $b_sq = $n_sq - $a * $a; + my $b = introot ($b_sq); + say "$a $b $n" if $b_sq == $b * $b; + } +} diff --git a/challenge-125/abigail/python/ch-1.py b/challenge-125/abigail/python/ch-1.py new file mode 100644 index 0000000000..f8c07799b3 --- /dev/null +++ b/challenge-125/abigail/python/ch-1.py @@ -0,0 +1,41 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput +import math + +def introot (square): + return math . floor (.4 + math . sqrt (square)) + +for line in fileinput . input (): + n = int (line) + if n <= 2: + print (-1) + continue + + n_sq = n * n + c = n + 1 + c_sq = n_sq + 2 * n + 1 + while 2 * c - 1 <= n_sq: + b_sq = c_sq - n_sq + b = introot (b_sq) + + if b_sq == b * b: + print (str (n) + " " + str (b) + " " + str (c)) + + c_sq = c_sq + 2 * c + 1 + c = c + 1 + + max_a = math . floor (n / math . sqrt (2)) + for a in range (3, max_a + 1): + b_sq = n_sq - a * a + b = introot (b_sq) + if b_sq == b * b: + print (str (a) + " " + str (b) + " " + str (n)) diff --git a/challenge-125/abigail/ruby/ch-1.rb b/challenge-125/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..91518fc95e --- /dev/null +++ b/challenge-125/abigail/ruby/ch-1.rb @@ -0,0 +1,45 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +def introot (square) + return Math . sqrt(square) . floor() +end + +ARGF . each_line do + |n| + n = n . to_i + if (n <= 2) then + puts (-1) + end + + n_sq = n * n + c = n + 1 + c_sq = n_sq + 2 * n + 1 + while (2 * c - 1 <= n_sq) do + b_sq = c_sq - n_sq + b = introot (b_sq) + + if (b_sq == b * b) then + puts (n . to_s + " " + b . to_s + " " + c . to_s) + end + + c_sq += 2 * c + 1 + c += 1 + end + + max_a = (n / Math . sqrt(2)) . floor() + for a in 3 .. max_a do + b_sq = n_sq - a * a + b = introot (b_sq) + if (b_sq == b * b) then + puts (a . to_s + " " + b . to_s + " " + n . to_s) + end + end +end |
