diff options
| author | Scimon Proctor <simon.proctor@gmail.com> | 2021-08-09 14:12:10 +0100 |
|---|---|---|
| committer | Scimon Proctor <simon.proctor@gmail.com> | 2021-08-09 14:12:10 +0100 |
| commit | 2fd3284b4fe1fff4704852af151b0efcecad2d2c (patch) | |
| tree | df4a169210bd85341a33eb00b54bffcb59aed462 | |
| parent | dae33f10b00beaf02883597401ede84ddcc3b77f (diff) | |
| download | perlweeklychallenge-club-2fd3284b4fe1fff4704852af151b0efcecad2d2c.tar.gz perlweeklychallenge-club-2fd3284b4fe1fff4704852af151b0efcecad2d2c.tar.bz2 perlweeklychallenge-club-2fd3284b4fe1fff4704852af151b0efcecad2d2c.zip | |
Simon reads wikipedia time is over
| -rw-r--r-- | challenge-125/simon-proctor/raku/ch-1.raku | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-125/simon-proctor/raku/ch-1.raku b/challenge-125/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..bf36282fe4 --- /dev/null +++ b/challenge-125/simon-proctor/raku/ch-1.raku @@ -0,0 +1,39 @@ +#!/usr/bin/env raku + +my @odds = (1,*+2...*).cache; + +multi sub MAIN('test') is hidden-from-USAGE { + use Test; + + is py-triples(3), ( (3,4,5) ); + is py-triples(4), ( (3,4,5) ); + is py-triples(5), ( (3,4,5), (5,12,13) ); + is py-triples(6), (); + is py-triples(13), ( (5,12,13), (13, 84, 85) ); + + done-testing; +} + +#| Given a positive Integer $N print the pythagorean triples it's found in or -1 if there are none +multi sub MAIN(UInt $N where * > 0) { + my @trips = py-triples( $N ); + say -1 unless +@trips; + .join(",").say for @trips; +} + +sub py-triples(UInt $N) { + my @out; + + my @odds = (1,*+2...* >= $N).cache; + + my @pairs = (@odds X, @odds).grep( -> ($m, $n) { $m > $n } ); + + for @pairs -> ( $m, $n ) { + if ( $N ~~ $m * $n || $N ~~ ($m²-$n²)/2 || $N ~~ ($m² + $n²)/2 ) { + push @out, $ = ( ($m * $n), ( ($m²-$n²)/2), ( ($m² + $n²)/2 ) ); + } + } + + return @out; +} + |
