diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-30 18:51:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-30 18:51:55 +0100 |
| commit | 39b5c5084290ee5dd9541d89b3994baa58463299 (patch) | |
| tree | bb0b1e76a1c3ad4fb289e80635c2323a0a70a21b | |
| parent | d526c68e31c401f1f62e95e1c15d349eb913c9c0 (diff) | |
| parent | fdd7f1418de55c6cfc18551eaa1f50f907b6dd41 (diff) | |
| download | perlweeklychallenge-club-39b5c5084290ee5dd9541d89b3994baa58463299.tar.gz perlweeklychallenge-club-39b5c5084290ee5dd9541d89b3994baa58463299.tar.bz2 perlweeklychallenge-club-39b5c5084290ee5dd9541d89b3994baa58463299.zip | |
Merge pull request #1488 from fluca1978/pwc54
Pwc54
| -rw-r--r-- | challenge-054/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-054/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-054/luca-ferrari/raku/ch-1.p6 | 42 | ||||
| -rw-r--r-- | challenge-054/luca-ferrari/raku/ch-2.p6 | 68 |
4 files changed, 112 insertions, 0 deletions
diff --git a/challenge-054/luca-ferrari/blog-1.txt b/challenge-054/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..cc4e0d5957 --- /dev/null +++ b/challenge-054/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/03/29/PerlWeeklyChallenge54.html#task1 diff --git a/challenge-054/luca-ferrari/blog-2.txt b/challenge-054/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..59fd472d84 --- /dev/null +++ b/challenge-054/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/03/29/PerlWeeklyChallenge54.html#task2 diff --git a/challenge-054/luca-ferrari/raku/ch-1.p6 b/challenge-054/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..cb8e83b68c --- /dev/null +++ b/challenge-054/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,42 @@ +#!env rau + +# Perl Weekly Challenge 54 +# see <https://perlweeklychallenge.org/blog/perl-weekly-challenge-054/> +# +# Task 1 +# +# kth Permutation Sequence +# +# Write a script to accept two integers n (>=1) +# and k (>=1). It should print the kth permutation of n integers. +# For more information, please follow the wiki page. +# +# For example, n=3 and k=4, the possible permutation sequences are listed below: +# +# 123 +# 132 +# 213 +# 231 +# 312 +# 321 +# +# The script should print the 4th permutation sequence 231. + + +sub MAIN( Int:D :$n where { $n >= 1 }, + Int:D :$k where { $k >= 1 } ) { + + "Computing the {$k}-th permutation of $n".say; + + # get all the single digits + # that can be permutated + my @digits; + for 1 .. $n { + @digits.push: $_; + } + + + my @permutations = @digits.permutations.sort; + say "does not exist" if ( $k >= @permutations.elems ); + say @permutations[ $k - 1 ]; +} diff --git a/challenge-054/luca-ferrari/raku/ch-2.p6 b/challenge-054/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..373767a6e5 --- /dev/null +++ b/challenge-054/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,68 @@ +#!env rau + +# Perl Weekly Challenge 54 +# see <https://perlweeklychallenge.org/blog/perl-weekly-challenge-054/> +# +# Task 2 +# +# Collatz Conjecture +# Contributed by Ryan Thompson +# +# It is thought that the following sequence will always reach 1: +# +# $n = $n / 2 when $n is even +# $n = 3*$n + 1 when $n is odd +# +# For example, if we start at 23, we get the following sequence: +# +# 23 → 70 → 35 → 106 → 53 → 160 → 80 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 +# +# Write a function that finds the Collatz sequence for any positive integer. +# Notice how the sequence itself may go far above the original starting number. +# +# +# Extra Credit +# Have your script calculate the sequence length for all starting +# numbers up to 1000000 (1e6), and output the starting number +# and sequence length for the longest 20 sequences. + + + +sub collatz( Int:D $m ) { + my @sequence; + my $n = $m; + while ( $n > 1 ) { + if ( $n %% 2 ) { + $n /= 2; + } + else { + $n = 3 * $n + 1; + } + + @sequence.push: $n; + } + + @sequence; +} + + +sub MAIN( Int:D $m where { $m > 0 } ) { + + my @sequence = collatz( $m ); + # print the results + @sequence.join( " → " ).say; + + + # extra credit + my %extra; + for 1 .. 100000 { + %extra{ $_ } = collatz( $_ ).elems; + } + + # sort by the length + # prints 20 most length sequences data + for %extra.sort( { $^b.value <=> $^a.value } )[0..20] -> $p { + "Number {$p.key} produces a Collatz sequence of {$p.value} numbers length".say; + } + +} |
