aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-125/mimosinnet/raku/ch-1.raku59
-rw-r--r--challenge-125/mimosinnet/raku/ch-2.raku68
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-125/mimosinnet/raku/ch-1.raku b/challenge-125/mimosinnet/raku/ch-1.raku
new file mode 100644
index 0000000000..63fe530f02
--- /dev/null
+++ b/challenge-125/mimosinnet/raku/ch-1.raku
@@ -0,0 +1,59 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-125/
+
+# Euclid's method for finding Pythagorean Triples
+# a = m2 – n2, b = 2mn, c = m2 + n2 (assuming m > n)
+# https://schoolbag.info/mathematics/numbers/79.html
+
+# The Euclid's approach does not seem to work.
+# It solves the tests of the challenge, but it does not find "all" of the
+# Pythagorean Triples. See Mark Anderson's solution.
+
+sub challenge( UInt $num ) {
+ return -1 if $num ∈ (1, 2);
+ gather {
+ # Pythagorean Triples of $num are bigger than $m²
+ for 1..$num -> $m {
+ for 1..^$num -> $n {
+ next if $n >= $m;
+ my @pt = ($m² - $n², 2 * $m * $n, $m² + $n²);
+ take @pt.sort if $num ∈ @pt;
+ }
+ }
+ };
+}
+
+# Odd:
+# Works: $ raku ch1.raku challenge
+# Does not Work: $ raku ch1.raku 13
+multi sub MAIN( UInt $N ) {
+ say "\nInput: ",'$N = ',$N;
+ say 'Output: ';
+ for challenge($N) -> $output {
+ say "\t ",$output.gist;
+ }
+}
+
+multi sub MAIN( 'challenge' ) {
+ my @challenge = (5, 13, 1);
+
+ for @challenge -> $a {
+ MAIN($a);
+ }
+}
+
+multi sub MAIN( 'test' ) is hidden-from-USAGE {
+ use Test;
+
+ my @test = (
+ 5, ( 3, 4, 5; 5, 12, 13 ),
+ 13, ( 5, 12, 13; 13, 84, 85 ),
+ 1, -1;
+ );
+
+ for @test -> $a, $b {
+ is challenge($a), $b;
+ }
+
+ done-testing;
+
+}
diff --git a/challenge-125/mimosinnet/raku/ch-2.raku b/challenge-125/mimosinnet/raku/ch-2.raku
new file mode 100644
index 0000000000..f48a72e21d
--- /dev/null
+++ b/challenge-125/mimosinnet/raku/ch-2.raku
@@ -0,0 +1,68 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-125
+
+# I have been following this:
+# https://www.geeksforgeeks.org/diameter-of-a-binary-tree/
+# and I believe it is only partially implemented. It does not cover
+# the situation where the left side or the right side are longer
+# without going to through the root node.
+
+class Node {
+ has Int $.value;
+ has Node $.left;
+ has Node $.right;
+}
+
+class Tree {
+ has Node $.tree;
+
+ my $lhight = 0;
+ my $rhight = 0;
+
+ sub lhight ($node) { return left-hight( $node ) }
+ sub rhight ($node) { return right-hight( $node ) }
+ sub tree-hight ($node) { return lhight($node) + rhight($node) + 1 }
+
+ sub left-hight( $node ) {
+ return $lhight unless $node.left.defined;
+ $lhight += 1;
+ left-hight( $node.left);
+ }
+
+ sub right-hight( $node ) {
+ return $rhight unless $node.right.defined;
+ $rhight += 1;
+ right-hight( $node.right);
+ }
+
+ method diameter { return tree-hight( self.tree) }
+}
+
+my $nodes = Node.new(
+ value => 1,
+ left => Node.new( value => 2,
+ left => Node.new(value => 3),
+ right => Node.new(value => 4)
+ ),
+ right => Node.new( value => 5,
+ left => Node.new( value => 6),
+ right => Node.new( value => 7,
+ left => Node.new( value => 8,
+ left => Node.new( value => 9)
+ ),
+ right => Node.new(value => 10)
+ )
+ )
+);
+
+multi sub MAIN( ) {
+ my $tree = Tree.new( tree => $nodes);
+ say "The diameter of the binary tree is ", $tree.diameter;
+}
+
+multi sub MAIN( 'test' ) is hidden-from-USAGE {
+ use Test;
+
+ is Tree.new( tree => $nodes).diameter, 6;
+
+ done-testing;
+}