From eb91d4aec41da945d8b9d48942a8d66aa9a26ebd Mon Sep 17 00:00:00 2001 From: Jan Krňávek Date: Sat, 31 Jul 2021 14:24:53 +0200 Subject: solutions week 123 --- challenge-123/wambash/raku/ch-1.raku | 31 +++++++++++++++++++++++++++++++ challenge-123/wambash/raku/ch-2.raku | 26 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-123/wambash/raku/ch-1.raku create mode 100644 challenge-123/wambash/raku/ch-2.raku diff --git a/challenge-123/wambash/raku/ch-1.raku b/challenge-123/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..f45ee3fbfd --- /dev/null +++ b/challenge-123/wambash/raku/ch-1.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku + +multi ugly-numbers ($n, :$ubounds!) { + my @twos = 1,2,4 ... $ubounds; + my @threes = 1,3,9 ... $ubounds; + my @fives = 1,5,25 ... $ubounds; + + @twos X* @threes X* @fives + andthen .sort + andthen .[$n-1] +} + +multi ugly-numbers ($n) { + my $guess = ugly-numbers $n, ubounds => 5**($n**(1/3)); + + ugly-numbers $n, ubounds => $guess; +} + +multi MAIN ( $n ) { + say ugly-numbers $n; +} + +multi MAIN (Bool :test($)!) { + use Test; + is ugly-numbers( 7), 8; + is ugly-numbers(10), 12; + is ugly-numbers(150, ubounds=> 5**(150**(1/3))), 5832; + is ugly-numbers(150), 5832; + is ugly-numbers(2¹⁶), 2³³ * 3⁴⁹; + done-testing; +} diff --git a/challenge-123/wambash/raku/ch-2.raku b/challenge-123/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..2a813e131d --- /dev/null +++ b/challenge-123/wambash/raku/ch-2.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku +class Point { + has $.x; + has $.y; + + method COERCE ( @list ) { + Point.new: x => @list.[0], y => @list.[1]; + } +} + +sub square-points ( Point() $a, Point() $b, Point() $c, Point() $d ) { + ($a.x - $c.x)*($b.x - $d.x) + ($a.y - $c.y)*($b.y-$d.y) == 0 + and + ($a.x - $c.x)² + ($a.y - $c.y)² == ($b.x - $d.x)² + ($b.y - $d.y)² +} + +multi MAIN (*@points) { + say square-points |@points.batch(2) +} + +multi MAIN (Bool :test($)!) { + use Test; + ok square-points((10,20),(20,20),(20,10),(10,10)); + nok square-points((12,24),(16,10),(20,12),(18,16)); + done-testing; +} -- cgit