diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-08-10 20:42:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-10 20:42:43 +0100 |
| commit | c8c3adda25c62f98be7c0455f46f83614bb2a510 (patch) | |
| tree | c96a18b60cb163dbcf4decdfc020c06e6ca322c6 | |
| parent | af413380a76c56592a4352ea083f93367a9d3bb7 (diff) | |
| parent | 02524164e2ab8edb1cc5df7564037369e2aa9ad7 (diff) | |
| download | perlweeklychallenge-club-c8c3adda25c62f98be7c0455f46f83614bb2a510.tar.gz perlweeklychallenge-club-c8c3adda25c62f98be7c0455f46f83614bb2a510.tar.bz2 perlweeklychallenge-club-c8c3adda25c62f98be7c0455f46f83614bb2a510.zip | |
Merge pull request #494 from noudald/challenge-020-noud
Solutions to challenge 20 problem 1 and 2 in Perl 6 by Noud
| -rw-r--r-- | challenge-020/noud/perl6/ch-1.p6 | 13 | ||||
| -rw-r--r-- | challenge-020/noud/perl6/ch-2.p6 | 22 |
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-020/noud/perl6/ch-1.p6 b/challenge-020/noud/perl6/ch-1.p6 new file mode 100644 index 0000000000..bf8b4259f0 --- /dev/null +++ b/challenge-020/noud/perl6/ch-1.p6 @@ -0,0 +1,13 @@ +# Write a script to accept a string from command line and split it on change of +# character. For example, if the string is “ABBCDEEF”, then it should split +# like “A”, “BB”, “C”, “D”, “EE”, “F”. + +sub MAIN(Str $str="ABBCDEEF") { + map({$_.Str.say}, $str ~~ m:g/(.)$0*/); +} + +# Regular expression explained: +# +# m:g --> Match global adverb, i.e. match repeatedly. +# (.) --> Match one character (with variable name $0). +# $0* --> Match zero or more times character $0. diff --git a/challenge-020/noud/perl6/ch-2.p6 b/challenge-020/noud/perl6/ch-2.p6 new file mode 100644 index 0000000000..43e846f267 --- /dev/null +++ b/challenge-020/noud/perl6/ch-2.p6 @@ -0,0 +1,22 @@ +# Write a script to print the smallest pair of Amicable Numbers. For more +# information, please checkout wikipedia page. + + +sub prop_div(Int $i) { + return [+] (1..^$i).grep($i %% *); +} + +# Anonymous cache handler. Trick copied from fjwhittle (ch-017, ch-1.p6). +&prop_div.wrap: -> $i { .[$i] //= callsame } given Array.new; + + +# Create a lazy list of all Amicable Numbers. +my @amicables = lazy gather for 1..^Inf -> $m { + for 1..^$m -> $n { + take ($n, $m) if prop_div($m) == $n and prop_div($n) == $m; + } +} + + +# Print the first three Amicable Numbers. +$_.say for @amicables[^3]; |
