From 2a1d18c8d972a88a9f21c5ee538ef906e4a2ee7a Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Mar 2020 08:25:29 +0200 Subject: Task1 done. --- challenge-054/luca-ferrari/raku/ch-1.p6 | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 challenge-054/luca-ferrari/raku/ch-1.p6 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 +# +# 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 ]; +} -- cgit From 5184107180552bc066f260d0169426d281f3e38b Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Mar 2020 08:49:36 +0200 Subject: Task 2 done. --- challenge-054/luca-ferrari/raku/ch-2.p6 | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 challenge-054/luca-ferrari/raku/ch-2.p6 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 +# +# 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; + } + +} -- cgit From fdd7f1418de55c6cfc18551eaa1f50f907b6dd41 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Mar 2020 08:58:24 +0200 Subject: Blog files. --- challenge-054/luca-ferrari/blog-1.txt | 1 + challenge-054/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-054/luca-ferrari/blog-1.txt create mode 100644 challenge-054/luca-ferrari/blog-2.txt 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 -- cgit