aboutsummaryrefslogtreecommitdiff
path: root/challenge-054
diff options
context:
space:
mode:
authorkevincolyer <>2020-04-05 15:05:54 +0100
committerkevincolyer <>2020-04-05 15:05:54 +0100
commit85aa2638f3015185bc529937f075bf5644880feb (patch)
tree5c1871b97ab847bbc442a6cf4746e0a9ea54e891 /challenge-054
parent1d7018eb917998edb6e405392ffc11a3c0bde946 (diff)
downloadperlweeklychallenge-club-85aa2638f3015185bc529937f075bf5644880feb.tar.gz
perlweeklychallenge-club-85aa2638f3015185bc529937f075bf5644880feb.tar.bz2
perlweeklychallenge-club-85aa2638f3015185bc529937f075bf5644880feb.zip
completed challege
Diffstat (limited to 'challenge-054')
-rw-r--r--challenge-054/kevin-colyer/raku/ch-1.p622
-rw-r--r--challenge-054/kevin-colyer/raku/ch-2.p683
l---------challenge-054/kevincolyer1
3 files changed, 73 insertions, 33 deletions
diff --git a/challenge-054/kevin-colyer/raku/ch-1.p6 b/challenge-054/kevin-colyer/raku/ch-1.p6
new file mode 100644
index 0000000000..bc56d2fefc
--- /dev/null
+++ b/challenge-054/kevin-colyer/raku/ch-1.p6
@@ -0,0 +1,22 @@
+#!perl6
+# Task 1 Challenge 054 Solution by kevincolyer
+# 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.
+
+subset PosInt of Int where * > 0;
+
+#| Permutation sequence 1..n kth item
+sub MAIN(PosInt :$n=3, PosInt :$k=4) {
+ # make a list with a range of numbers from 1 to $n and take the permutations
+ my @p=(1..$n).permutations;
+ die "k is too high ($k) - only {@p.elems} in sequence" if $k > @p.elems;
+
+ say "n=$n, k=$k => " ~ @p[$k-1].join;
+}
diff --git a/challenge-054/kevin-colyer/raku/ch-2.p6 b/challenge-054/kevin-colyer/raku/ch-2.p6
index d7ac6e5a67..58c743c2b9 100644
--- a/challenge-054/kevin-colyer/raku/ch-2.p6
+++ b/challenge-054/kevin-colyer/raku/ch-2.p6
@@ -14,13 +14,12 @@
# all starting numbers up to 1000000 (1e6), and output the starting
# number and sequence length for the longest 20 sequences.
-my Int @cache;
-my Int @length;
+
sub collatzSeqChain(Int $n is copy) {
my Str $seq = "$n";
while $n > 1 {
if $n +& 1 == 0 {
- $n=Int($n / 2);
+ $n= $n +> 1;
} else {
$n = $n * 3 + 1;
}
@@ -29,14 +28,19 @@ sub collatzSeqChain(Int $n is copy) {
return $seq;
}
-sub collatzSeqLen(Int $number) {
+
+sub collatzSeqLen(Int $number) returns Int {
+ state Int @length;
my Int $n=$number;
my Int $len=1;
my Str $seq = "$n";
while $n > 1 {
- if @length[$n]:exists { $len += @length[$n]; last }
+ if @length[$n]:exists {
+ $len += @length[$n];
+ last;
+ }
if $n +& 1 == 0 {
- $n=Int($n / 2);
+ $n = $n +> 1;
} else {
$n = $n * 3 + 1;
}
@@ -46,32 +50,47 @@ sub collatzSeqLen(Int $number) {
return $len;
}
-say collatzSeqChain(23);
-say collatzSeqLen(2000);
+multi MAIN('test') {
+ say collatzSeqChain(23);
+}
+
+multi MAIN('sequence', Int :$number=23 ) {
+ die "number must be a positive integer > 1 [$number]" if $number < 1;
+ say collatzSeqChain($number);
+}
-my Int @chain=0;
-my $want=20;
-my $topMin=1;
-my @top;
-my @topN;
-my %ltoi;
-my $t =now.Int;
-for 1..1_000_00 -> $i {
- my $l=collatzSeqLen($i);
-
- next if $l < $topMin;
- next if $l == any @top;
-
- %ltoi{$l}=$i;
+multi MAIN ('top', Int :$number=10_000, Int :$want=20) {
+
+ die "Longest chainst WANTed [$want] must be > number in sequence [$number]" if $want >= $number;
- @top.push: $l;
- @top.=sort;
- @top.shift if @top.elems > $want;
- $topMin=@top[0];
+ my Int @chain = 0;
+ my Int $topMin = 1;
+ my Int @top;
+ my Int @topN;
+ my %ltoi;
+ my $t = now.Int;
+ my Int $x = $number;
+ my Int $l;
+
+ for 1..$x -> Int $i {
+ $l = collatzSeqLen($i);
+
+ next if $l < $topMin;
+ next if $l == any @top;
+
+ %ltoi{$l} = $i;
+
+ @top.push: $l;
+ @top.=sort;
+ @top.shift if @top.elems > $want;
+ $topMin = @top[0];
+ }
+
+ @top.map({ "\n{%ltoi{$_}} length $_ = \n"~collatzSeqChain( %ltoi{$_} ) })>>.say;
+ say "\n$x sequences searched in {Rat(now -$t)} seconds";
}
-say now -$t;
-say @top.map: %ltoi{*};
-#
-# for @chain.reverse[1..20] -> $i {
-# my $n=@chain
-# };
+
+# 1000000 sequences searched in 17.394643 seconds
+# 1000000 sequences searched in 10.144286 seconds
+
+
diff --git a/challenge-054/kevincolyer b/challenge-054/kevincolyer
deleted file mode 120000
index 8fc47c15c2..0000000000
--- a/challenge-054/kevincolyer
+++ /dev/null
@@ -1 +0,0 @@
-kevin-colyer \ No newline at end of file