From 3441fdde915ce5e0cc46b5b197facce50acdb275 Mon Sep 17 00:00:00 2001 From: ohmycloud Date: Tue, 9 Apr 2019 12:34:26 +0800 Subject: Change-003 solution --- challenge-003/ohmycloud/perl6/ch-1.p6 | 9 +++++++++ challenge-003/ohmycloud/perl6/ch-2.p6 | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 challenge-003/ohmycloud/perl6/ch-1.p6 create mode 100644 challenge-003/ohmycloud/perl6/ch-2.p6 diff --git a/challenge-003/ohmycloud/perl6/ch-1.p6 b/challenge-003/ohmycloud/perl6/ch-1.p6 new file mode 100644 index 0000000000..a29127d30c --- /dev/null +++ b/challenge-003/ohmycloud/perl6/ch-1.p6 @@ -0,0 +1,9 @@ +multi sub pascal (1) { $[1] } +multi sub pascal (Int $n where 2..*) { + my @rows = pascal $n - 1; + |@rows, [0, |@rows[*-1] Z+ |@rows[*-1], 0 ]; +} + +sub MAIN(Int $row) { + .say for pascal $row; +} \ No newline at end of file diff --git a/challenge-003/ohmycloud/perl6/ch-2.p6 b/challenge-003/ohmycloud/perl6/ch-2.p6 new file mode 100644 index 0000000000..73fa14704f --- /dev/null +++ b/challenge-003/ohmycloud/perl6/ch-2.p6 @@ -0,0 +1,24 @@ + +sub ugly-number(Int $index) { + return 0 if $index == 0; + my @baselist = [1]; + my ($min2, $min3, $min5) = 0,0,0; + my $curnum = 1; + + while $curnum < $index { + my $minnum = (@baselist[$min2] * 2, @baselist[$min3] * 3, @baselist[$min5] * 5).min; + @baselist.append($minnum); + + while @baselist[$min2] * 2 <= $minnum { $min2 += 1 } + while @baselist[$min3] * 3 <= $minnum { $min3 += 1 } + while @baselist[$min5] * 5 <= $minnum { $min5 += 1 } + + $curnum +=1 + } + + return @baselist[*-1] +} + +sub MAIN(Int $count) { + ugly-number($_).say for 1..$count; +} \ No newline at end of file -- cgit