diff options
Diffstat (limited to 'challenge-125')
88 files changed, 5746 insertions, 95 deletions
diff --git a/challenge-125/abigail/README.md b/challenge-125/abigail/README.md index c116d92523..677f80fccf 100644 --- a/challenge-125/abigail/README.md +++ b/challenge-125/abigail/README.md @@ -1,95 +1,93 @@ # Solutions by Abigail -## [Happy Women Day][task1] +## [Pythagorean Triples][task1] -> Write a script to print the Venus Symbol, international gender symbol -> for women. Please feel free to use any character. +> You are given a positive integer `$N`. +> +> Write a script to print all Pythagorean Triples containing $N as +> a member. Print `-1` if it can't be a member of any. +> +> Triples with the same set of elements are considered the same, +> i.e. if your script has already printed `(3, 4, 5)`, `(4, 3, 5)` should +> not be printed. +> +> > The famous Pythagorean theorem states that in a right angle +> > triangle, the length of the two shorter sides and the length of the +> > longest side are related by `a^2+b^2 = c^2`. +### Example ~~~~ - ^^^^^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^^^^^ - ^ - ^ - ^ - ^^^^^ - ^ - ^ +Input: $N = 5 +Output: (3, 4, 5) + (5, 12, 13) + +Input: $N = 13 +Output: (5, 12, 13) + (13, 84, 85) + +Input: $N = 1 +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 124: Happy Women Day][blog1] +[Perl Weekly Challenge 125: Pythagorean Triples][blog1] + +## [Binary Tree Diameter][task2] -## [Tug of War][task2] +> You are given binary tree as below: -> You are given a set of $n integers `(n1, n2, n3, ...)`. +~~~~ + 1 + / \ + 2 5 + / \ / \ +3 4 6 7 + / \ + 8 10 + / + 9 +~~~~ + +> Write a script to find the diameter of the given binary tree. > -> Write a script to divide the set in two subsets of `n/2` sizes each -> so that the difference of the sum of two subsets is the least. If -> `$n` is even then each subset must be of size `$n/2` each. In case $n -> is odd then one subset must be `($n-1)/2` and other must be `($n+1)/2`. +> > 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. + +For the above given binary tree, possible diameters (7) are: -### Examples ~~~~ -Input: Set = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100) -Output: Subset 1 = (30, 40, 60, 70, 80) - Subset 2 = (10, 20, 50, 90, 100) +3, 2, 1, 5, 7, 8, 9 ~~~~ +or + ~~~~ -Input: Set = (10, -15, 20, 30, -25, 0, 5, 40, -5) - Subset 1 = (30, 0, 5, -5) - Subset 2 = (10, -15, 20, -25, 40) +4, 2, 1, 5, 7, 8, 9 ~~~~ ### Solutions * [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) ### Blog -[Perl Weekly Challenge 124: Tug of War][blog2] +[Perl Weekly Challenge 125: Binary Tree Diameter][blog2] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-124/#TASK1 -[task2]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-124/#TASK2 -[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-124-1.html -[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-124-2.html +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-125/#TASK1 +[task2]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-125/#TASK2 +[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-1.html +[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-2.html diff --git a/challenge-125/abigail/awk/ch-1.awk b/challenge-125/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..1758b7d64d --- /dev/null +++ b/challenge-125/abigail/awk/ch-1.awk @@ -0,0 +1,44 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +function introot (square) { + return (int (.4 + sqrt (square))) +} + + +$1 <= 2 { + print (-1) + next +} + + +{ + n = $1 + 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 n, b, c + } + c_sq += 2 * c ++ + 1 + } + + max_a = int (n / sqrt (2)) + for (a = 3; a <= max_a; a ++) { + b_sq = n_sq - a * a + b = introot(b_sq) + if (b_sq == b * b) { + print a, b, n + } + } +} diff --git a/challenge-125/abigail/blog.txt b/challenge-125/abigail/blog.txt new file mode 100644 index 0000000000..79e86ddc80 --- /dev/null +++ b/challenge-125/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-1.html diff --git a/challenge-125/abigail/blog1.txt b/challenge-125/abigail/blog1.txt new file mode 100644 index 0000000000..ce0adcab58 --- /dev/null +++ b/challenge-125/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-2.html 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/go/ch-1.go b/challenge-125/abigail/go/ch-1.go new file mode 100644 index 0000000000..f69846b7ed --- /dev/null +++ b/challenge-125/abigail/go/ch-1.go @@ -0,0 +1,61 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "math" +) + +func introot (square int) int { + return (int (math . Round (math . Sqrt (float64 (square))))); +} + +func main () { + for { + var n, n_sq, a, max_a, b, b_sq, c, c_sq int; + var count, err = fmt . Scanf ("%d", &n); + if (err != nil || count != 1) { + break; + } + + if (n <= 2) { + fmt . Println ("-1"); + continue; + } + + n_sq = n * n; + c = n + 1; + c_sq = n_sq + 2 * n + 1; + + for 2 * c - 1 <= n_sq { + b_sq = c_sq - n_sq; + b = introot (b_sq); + + if (b_sq == b * b) { + fmt . Printf ("%d %d %d\n", n, b, c); + } + + c_sq += 2 * c + 1; + c += 1; + } + + max_a = int (float64 (n) / math . Sqrt (float64 (2))); + + a = 3; + for a <= max_a { + b_sq = n_sq - a * a + b = introot (b_sq) + if (b_sq == b * b) { + fmt . Printf ("%d %d %d\n", a, b, n); + } + a += 1; + } + } +} diff --git a/challenge-125/abigail/java/ch-1.java b/challenge-125/abigail/java/ch-1.java new file mode 100644 index 0000000000..722b6d0f81 --- /dev/null +++ b/challenge-125/abigail/java/ch-1.java @@ -0,0 +1,52 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static long introot (long square) { + return ((long) Math . floor (Math . sqrt (square))); + } + + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + long n = scanner . nextInt (); + if (n <= 2) { + System . out . println ("-1"); + continue; + } + + long n_sq = n * n; + long c = n + 1; + long c_sq = n_sq + 2 * n + 1; + + while (2 * c - 1 <= n_sq) { + long b_sq = c_sq - n_sq; + long b = introot (b_sq); + + if (b_sq == b * b) { + System . out . printf ("%d %d %d\n", n, b, c); + } + + c_sq += 2 * c + 1; + c += 1; + } + + long max_a = (long) Math . floor (n / Math . sqrt (2)); + long a = 3; + for (a = 3; a <= max_a; a ++) { + long b_sq = n_sq - a * a; + long b = introot (b_sq); + if (b_sq == b * b) { + System . out . printf ("%d %d %d\n", a, b, n); + } + } + } + } +} 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< |
