Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add scripts, rename scripts, fix glitches in scripts.

  • Loading branch information...
commit cf0a7e5388c5107000a25f2057a226266cbd15c9 1 parent 3e3fe62
@colomon authored
View
16 bin/collatz-loop.pl
@@ -0,0 +1,16 @@
+sub collatz-length(Int $n is copy) {
+ my $length = 1;
+ while $n != 1 {
+ $n = $n %% 2 ?? $n div 2 !! 3 * $n + 1;
+ $length++;
+ }
+ $length;
+}
+
+sub MAIN(*@numbers) {
+ for @numbers -> $n {
+ say "$n: " ~ collatz-length($n.Int);
+ }
+}
+
+
View
15 bin/collatz-recursive-default.pl
@@ -0,0 +1,15 @@
+sub collatz-length(Int $n) {
+ given $n {
+ when 1 { 1 }
+ when * %% 2 { 1 + collatz-length($_ div 2) }
+ default { 1 + collatz-length(3 * $_ + 1) }
+ }
+}
+
+sub MAIN(*@numbers) {
+ for @numbers -> $n {
+ say "$n: " ~ collatz-length($n.Int);
+ }
+}
+
+
View
4 bin/collatz-recursive2.pl → bin/collatz-recursive-ternary.pl
@@ -1,6 +1,6 @@
sub collatz-length(Int $n) {
- return 1 if $n eq 1;
- 1 + ($n %% 2 ?? collatz-length($_ div 2) !! collatz-length(3 * $_ + 1));
+ return 1 if $n == 1;
+ 1 + ($n %% 2 ?? collatz-length($n div 2) !! collatz-length(3 * $n + 1));
}
sub MAIN(*@numbers) {
View
6 bin/collatz-recursive.pl
@@ -1,8 +1,8 @@
sub collatz-length(Int $n) {
given $n {
- when 1 { 1 }
- when * %% 2 { 1 + collatz-length($_ div 2) }
- default { 1 + collatz-length(3 * $_ + 1) }
+ when 1 { 1 }
+ when * %% 2 { 1 + collatz-length($_ div 2) }
+ when * !%% 2 { 1 + collatz-length(3 * $_ + 1) }
}
}
View
2  bin/collatz-recursive4.pl
@@ -14,7 +14,7 @@
sub collatz-length(Int $n) is cached {
return 1 if $n eq 1;
- 1 + ($n %% 2 ?? collatz-length($_ div 2) !! collatz-length(3 * $_ + 1));
+ 1 + ($n %% 2 ?? collatz-length($n div 2) !! collatz-length(3 * $n + 1));
}
sub MAIN(*@numbers) {
View
0  bin/collatz-sequence2.pl → bin/collatz-sequence-div.pl
File renamed without changes
View
11 bin/collatz-sequence-ternary.pl
@@ -0,0 +1,11 @@
+sub collatz-length(Int $start) {
+ +($start, { $_ %% 2 ?? $_ div 2 !! 3 * $_ + 1 } ... 1);
+}
+
+sub MAIN(*@numbers) {
+ for @numbers -> $n {
+ say "$n: " ~ collatz-length($n.Int);
+ }
+}
+
+
View
2  bin/collatz-sequence.pl
@@ -7,5 +7,3 @@ (*@numbers)
say "$n: " ~ collatz-length($n.Int);
}
}
-
-
Please sign in to comment.
Something went wrong with that request. Please try again.