From edad8316de0be8b8342907bfce39169c7f1344b9 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 31 Jan 2022 08:32:36 +0000 Subject: Challenge 150 Solutions (Raku) --- challenge-150/mark-anderson/raku/ch-1.raku | 13 +++++++++++++ challenge-150/mark-anderson/raku/ch-2.raku | 5 +++++ 2 files changed, 18 insertions(+) create mode 100644 challenge-150/mark-anderson/raku/ch-1.raku create mode 100644 challenge-150/mark-anderson/raku/ch-2.raku diff --git a/challenge-150/mark-anderson/raku/ch-1.raku b/challenge-150/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..876bf8620a --- /dev/null +++ b/challenge-150/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + +use Test; + +is fib-words(1234, 5678), 7; +is fib-words(123, 45678), 4; +is fib-words('abcd', 'efgh'), 'g'; +is fib-words('abc', 'defgh'), 'd'; + +sub fib-words(\a, \b) +{ + (a, b, * ~ * ... *.chars > 50).tail.substr(50, 1); +} diff --git a/challenge-150/mark-anderson/raku/ch-2.raku b/challenge-150/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..3f65bb03c8 --- /dev/null +++ b/challenge-150/mark-anderson/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/usr/bin/env raku + +use Prime::Factor; + +.say for (1..500).grep({ not prime-factors($_).repeated }); -- cgit From d52aa060e5c7663353b4d1a0186b608ef520dc9e Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 31 Jan 2022 11:09:54 +0100 Subject: Week 150: Remove left over file from week 149. No idea why this file got copied from the previous week. --- challenge-150/abigail/data/preprocess | 244 ---------------------------------- 1 file changed, 244 deletions(-) delete mode 100755 challenge-150/abigail/data/preprocess diff --git a/challenge-150/abigail/data/preprocess b/challenge-150/abigail/data/preprocess deleted file mode 100755 index 20cdfa2cb0..0000000000 --- a/challenge-150/abigail/data/preprocess +++ /dev/null @@ -1,244 +0,0 @@ -#!/opt/perl/bin/perl - -use 5.032; - -use strict; -use warnings; -no warnings 'syntax'; - -use experimental 'signatures'; -use experimental 'lexical_subs'; - -use LWP::Simple; - -my $lang = lc (shift // "Perl"); - -my $URL = "https://oeis.org/A287298/b287298.txt"; - -my @chars = (0 .. 9, 'A' .. 'Z'); -my @A287298; -my $ARRAY = 'A287298'; -my $file = lc $ARRAY; - -$ENV {BC_LINE_LENGTH} = 1000; - -foreach my $line (split (/\n/ => get $URL), - "22 340653564758245010607213613056", - "23 20837313275713865979999662611449", - "24 1331214423741263089885099589776609", - "25 88663641996555130440258540215016516") { - next unless $line =~ /^([0-9]+)\s+([0-9]+)/; - my ($base, $value) = ($1, $2); - my $value_in_base = `echo "obase=$base; $value" | bc` - =~ s/ ([0-9]{2})/$chars [0 + $1]/egr - =~ s/\n//r; - $A287298 [$base] = [$value_in_base, $value]; -} - -my $size = @A287298; -my $max_base = $size - 1; -my $width = 2 + length $A287298 [$max_base] [0]; - - -open my $awk_h, ">", "$file.awk" or die "open $file.awk: $!"; -open my $bash_h, ">", "$file.sh" or die "open $file.sh: $!"; -open my $basic_h, ">", "$file.bas" or die "open $file.bas: $!"; -open my $bc_h, ">", "$file.bc" or die "open $file.bc: $!"; -open my $c_h, ">", "$file.c" or die "open $file.c: $!"; -open my $go_h, ">", "$file.go" or die "open $file.go: $!"; -open my $java_h, ">", "$file.java" or die "open $file.java: $!"; -open my $lua_h, ">", "$file.lua" or die "open $file.lua: $!"; -open my $node_h, ">", "$file.js" or die "open $file.js: $!"; -open my $pascal_h, ">", "$file.p" or die "open $file.p: $!"; -open my $perl_h, ">", "$file.pl" or die "open $file.pl: $!"; -open my $python_h, ">", "$file.py" or die "open $file.py: $!"; -open my $r_h, ">", "$file.r" or die "open $file.r: $!"; -open my $ruby_h, ">", "$file.rb" or die "open $file.rb: $!"; -open my $scheme_h, ">", "$file.scm" or die "open $file.scm: $!"; -open my $tcl_h, ">", "$file.tcl" or die "open $file.tcl: $!"; - -my $basic_ln = 990; - -say $awk_h "BEGIN {"; - -say $bash_h "declare -a $ARRAY\n"; - -say $basic_h ($basic_ln += 10), " maxbase = $max_base\n"; -say $basic_h ($basic_ln += 10), " INPUT N\n\n"; - -say $bc_h "max_base = $max_base\n"; - -print $c_h <<~ "--"; - int main (void) { - char * $ARRAY [$size]; - size_t max_base = $max_base; - - -- - -print $go_h <<~ "--"; - func main () { - max_base := $max_base - var $ARRAY [$size] string - - -- - -print $java_h <<~ "--"; - public static void main (String [] args) { - int max_base = $max_base; - String [] $ARRAY = new String [$size]; - -- - -say $lua_h "$ARRAY = {}\n"; - -say $node_h "let $ARRAY = []\n"; - -print $pascal_h <<~ "--"; - var - $ARRAY: array [0 .. $max_base] of string; - max_base: integer = $max_base; - - begin - -- - -say $perl_h "my \@$ARRAY;\n"; - -say $python_h "$ARRAY = {}\n"; - -print $r_h <<~ "--"; - $ARRAY <- c () - max_base <- $max_base - - -- - -say $ruby_h "$ARRAY = []\n"; - -print $scheme_h <<~ "--"; - (define $ARRAY '()) - (define max_base $max_base) - - -- - -say $tcl_h "set $ARRAY list\n"; - -while (my ($i, $info) = each @A287298) { - if (!defined $info) { - printf $c_h " $ARRAY [%2d] = %${width}s;\n", $i, "NULL"; - printf $go_h " $ARRAY [%2d] = %${width}s\n", $i, '""'; - printf $java_h " $ARRAY [%2d] = %${width}s;\n", $i, '""'; - printf $pascal_h " $ARRAY [%2d] = %${width}s;\n", $i, "''"; - printf $scheme_h "(set! $ARRAY (append $ARRAY (list %${width}s)))\n", - '""'; - printf $tcl_h "lset $ARRAY %2d %${width}s\n", $i, '""'; - next; - } - my ($value, $decimal_value) = @$info; - my $dqvalue = qq ["$value"]; - my $sqvalue = qq ['$value']; - - # - # AWK - # - printf $awk_h " $ARRAY [%2d] = %${width}s\n", $i, $dqvalue; - - # - # Bash - # - printf $bash_h "${ARRAY}[%d]=%s\n", $i, $value; - - # - # BASIC - # - printf $basic_h "%04d IF N = %2d THEN PRINT %${width}s\n", - ($basic_ln += 10), $i, $dqvalue; - - # - # bc - # - if ($i <= 16) { - printf $bc_h "\l$ARRAY [%2d] = %30s\n", $i, $decimal_value; - } - - # - # C - # - printf $c_h " $ARRAY [%2d] = %${width}s;\n", $i, $dqvalue; - - # - # Go - # - printf $go_h " $ARRAY [%2d] = %${width}s\n", $i, $dqvalue; - - # - # Java - # - printf $java_h " $ARRAY [%2d] = %${width}s;\n", $i, $dqvalue; - - # - # Lua - # - printf $lua_h "$ARRAY [%2d] = %${width}s\n", $i, $dqvalue; - - # - # Node.js - # - printf $node_h "$ARRAY [%2d] = %${width}s\n", $i, $dqvalue; - - # - # Pascal - # - printf $pascal_h " $ARRAY [%2d] := %${width}s;\n", $i, $sqvalue; - - # - # Perl - # - printf $perl_h "\$$ARRAY [%2d] = %${width}s;\n", $i, $dqvalue; - - # - # Python - # - printf $python_h "$ARRAY [%2d] = %${width}s\n", $i, $dqvalue; - - # - # R - # - printf $r_h "$ARRAY [[%2d]] <- %${width}s\n", $i, $dqvalue; - - # - # Ruby - # - printf $ruby_h "${ARRAY}[%2d] = %${width}s\n", $i, $dqvalue; - - # - # Scheme - # - printf $scheme_h "(set! $ARRAY (append $ARRAY (list %${width}s)))\n", - $dqvalue; - - # - # Tcl - # - printf $tcl_h "lset $ARRAY %2d %${width}s\n", $i, $dqvalue; -} -say $awk_h "}"; -say $c_h "}"; -say $go_h "}"; -say $java_h " }"; -say $pascal_h "end."; - - -close $awk_h or die "close $file.awk: $!"; -close $bash_h or die "close $file.sh: $!"; -close $basic_h or die "close $file.bas: $!"; -close $bc_h or die "close $file.bc: $!"; -close $c_h or die "close $file.c: $!"; -close $go_h or die "close $file.go: $!"; -close $java_h or die "close $file.java: $!"; -close $lua_h or die "close $file.lua: $!"; -close $node_h or die "close $file.js: $!"; -close $pascal_h or die "close $file.p: $!"; -close $perl_h or die "close $file.pl: $!"; -close $python_h or die "close $file.py: $!"; -close $r_h or die "close $file.r: $!"; -close $ruby_h or die "close $file.rb: $!"; -close $scheme_h or die "close $file.scm: $!"; -close $tcl_h or die "close $file.tcl: $!"; -- cgit From 21738ed719f6f4f185aef8b5c0e8502ac000a2a7 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 31 Jan 2022 11:23:02 +0000 Subject: Solutions for challenge #150 --- challenge-150/roger-bell-west/javascript/ch-1.js | 23 +++ challenge-150/roger-bell-west/javascript/ch-2.js | 149 +++++++++++++++ challenge-150/roger-bell-west/kotlin/ch-1.kt | 21 +++ challenge-150/roger-bell-west/kotlin/ch-2.kt | 151 +++++++++++++++ challenge-150/roger-bell-west/lua/ch-1.lua | 22 +++ challenge-150/roger-bell-west/lua/ch-2.lua | 179 ++++++++++++++++++ challenge-150/roger-bell-west/perl/ch-1.pl | 22 +++ challenge-150/roger-bell-west/perl/ch-2.pl | 119 ++++++++++++ challenge-150/roger-bell-west/postscript/ch-1.ps | 21 +++ challenge-150/roger-bell-west/postscript/ch-2.ps | 226 +++++++++++++++++++++++ challenge-150/roger-bell-west/python/ch-1.py | 21 +++ challenge-150/roger-bell-west/python/ch-2.py | 138 ++++++++++++++ challenge-150/roger-bell-west/raku/ch-1.p6 | 20 ++ challenge-150/roger-bell-west/raku/ch-2.p6 | 116 ++++++++++++ challenge-150/roger-bell-west/ruby/ch-1.rb | 25 +++ challenge-150/roger-bell-west/ruby/ch-2.rb | 114 ++++++++++++ challenge-150/roger-bell-west/rust/ch-1.rs | 21 +++ challenge-150/roger-bell-west/rust/ch-2.rs | 124 +++++++++++++ 18 files changed, 1512 insertions(+) create mode 100755 challenge-150/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-150/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-150/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-150/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-150/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-150/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-150/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-150/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-150/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-150/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-150/roger-bell-west/python/ch-1.py create mode 100755 challenge-150/roger-bell-west/python/ch-2.py create mode 100755 challenge-150/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-150/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-150/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-150/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-150/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-150/roger-bell-west/rust/ch-2.rs diff --git a/challenge-150/roger-bell-west/javascript/ch-1.js b/challenge-150/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..51d234f33a --- /dev/null +++ b/challenge-150/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,23 @@ +#! /usr/bin/node + +function fibstr(aa,bb,limit) { + let a=aa; + let b=bb; + while (true) { + let c = a + b; + console.log(c); + if (c.length >= limit) { + return c.slice(limit-1,limit); + } + a = b; + b = c; + } +} + +if (fibstr("1234","5678",51) == "7") { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-150/roger-bell-west/javascript/ch-2.js b/challenge-150/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..b64412bf0c --- /dev/null +++ b/challenge-150/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,149 @@ +#! /usr/bin/node + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function genprimes(mx) { + let primesh=new Set([2,3]); + for (let i = 6; i <= mx+1; i += 6) { + for (let j = i-1; j <= i+1; j += 2) { + if (j <= mx) { + primesh.add(j); + } + } + } + let q=[2,3,5,7]; + let p=q.shift(); + let mr=Math.floor(Math.sqrt(mx)); + while (p <= mr) { + if (primesh.has(p)) { + let i=p*p + for (let i=p*p; i <= mx; i += p) { + primesh.delete(i); + } + } + if (q.length < 2) { + q.push(q[q.length-1]+4); + q.push(q[q.length-1]+2); + } + p=q.shift(); + } + let primes=[...primesh]; + primes.sort(function(a,b) { + return a-b; + }); + return primes; +} + +function squarefree(max) { + let out=[]; + let primes=genprimes(Math.floor(Math.sqrt(max))); + let plimit=1; + for (let candidate=1; candidate <= max; candidate++) { + let sf=true; + while (plimit * plimit < candidate) { + plimit++; + } + let cc=candidate; + for (let pr of primes) { + if (pr > plimit) { + break; + } + let n=0; + while (cc % pr == 0) { + n++; + if (n > 1) { + sf=false; + break; + } + cc = Math.floor(cc/pr); + } + if (!sf) { + break; + } + } + if (sf) { + out.push(candidate); + } + } + return out; +} + +if (deepEqual(squarefree(30),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, + 30 +])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(squarefree(113),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, + 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 51, 53, 55, + 57, 58, 59, 61, 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, + 82, 83, 85, 86, 87, 89, 91, 93, 94, 95, 97, 101, 102, 103, 105, + 106, 107, 109, 110, 111, 113 +])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(squarefree(500),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, + 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 51, 53, 55, + 57, 58, 59, 61, 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, + 82, 83, 85, 86, 87, 89, 91, 93, 94, 95, 97, 101, 102, 103, 105, + 106, 107, 109, 110, 111, 113, 114, 115, 118, 119, 122, 123, 127, + 129, 130, 131, 133, 134, 137, 138, 139, 141, 142, 143, 145, 146, + 149, 151, 154, 155, 157, 158, 159, 161, 163, 165, 166, 167, 170, + 173, 174, 177, 178, 179, 181, 182, 183, 185, 186, 187, 190, 191, + 193, 194, 195, 197, 199, 201, 202, 203, 205, 206, 209, 210, 211, + 213, 214, 215, 217, 218, 219, 221, 222, 223, 226, 227, 229, 230, + 231, 233, 235, 237, 238, 239, 241, 246, 247, 249, 251, 253, 254, + 255, 257, 258, 259, 262, 263, 265, 266, 267, 269, 271, 273, 274, + 277, 278, 281, 282, 283, 285, 286, 287, 290, 291, 293, 295, 298, + 299, 301, 302, 303, 305, 307, 309, 310, 311, 313, 314, 317, 318, + 319, 321, 322, 323, 326, 327, 329, 330, 331, 334, 335, 337, 339, + 341, 345, 346, 347, 349, 353, 354, 355, 357, 358, 359, 362, 365, + 366, 367, 370, 371, 373, 374, 377, 379, 381, 382, 383, 385, 386, + 389, 390, 391, 393, 394, 395, 397, 398, 399, 401, 402, 403, 406, + 407, 409, 410, 411, 413, 415, 417, 418, 419, 421, 422, 426, 427, + 429, 430, 431, 433, 434, 435, 437, 438, 439, 442, 443, 445, 446, + 447, 449, 451, 453, 454, 455, 457, 458, 461, 462, 463, 465, 466, + 467, 469, 470, 471, 473, 474, 478, 479, 481, 482, 483, 485, 487, + 489, 491, 493, 494, 497, 498, 499 +])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-150/roger-bell-west/kotlin/ch-1.kt b/challenge-150/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..fa3cb3b826 --- /dev/null +++ b/challenge-150/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,21 @@ +fun fibstr(aa: String,bb: String,limit: Int): Char { + var a=aa; + var b=bb; + while (true) { + var c=a+b; + println(c); + if (c.length >= limit) { + return c[limit-1]; + } + a=b; + b=c; + } +} + +fun main() { + if (fibstr("1234","5678",51) == '7') { + println("Pass") + } else { + println("FAIL") + } +} diff --git a/challenge-150/roger-bell-west/kotlin/ch-2.kt b/challenge-150/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..bf2330348b --- /dev/null +++ b/challenge-150/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,151 @@ +import kotlin.math.* + +fun genprimes(mx: Int): ArrayList { + var primesh=mutableSetOf() + for (i in 2..3) { + primesh.add(i) + } + for (i in 6..mx+1 step 6) { + for (j in i-1..i+1 step 2) { + if (j <= mx) { + primesh.add(j) + } + } + } + var q=ArrayDeque(listOf(2,3,5,7)) + var p=q.removeFirst() + val mr=sqrt(mx.toDouble()).toInt() + while (p <= mr) { + if (primesh.contains(p)) { + for (i in p*p..mx step p) { + primesh.remove(i) + } + } + if (q.size < 2) { + q.add(q.last()+4) + q.add(q.last()+2) + } + p=q.removeFirst() + } + var primes=ArrayList(primesh.distinct()) + primes.sort() + return primes +} + +fun squarefree(max: Int): ArrayList { + var out=ArrayList() + val primes=genprimes(Math.sqrt(max.toDouble()).toInt()) + var plimit=1 + for (candidate in 1..max) { + var squarefree=true + while (plimit * plimit < candidate) { + plimit += 1 + } + var cc=candidate + for (pr in primes) { + if (pr > plimit) { + break + } + var n=0 + while (cc % pr == 0) { + n += 1 + if (n > 1) { + squarefree=false + break + } + cc /= pr + } + if (!squarefree) { + break + } + } + if (squarefree) { + out.add(candidate) + } + } + return out +} + +fun main() { + if (squarefree(30) == listOf(1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, + 17, 19, 21, 22, 23, 26, 29, 30)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (squarefree(113) == listOf(1, 2, 3, 5, 6, 7, 10, 11, 13, 14, + 15, 17, 19, 21, 22, 23, 26, 29, 30, + 31, 33, 34, 35, 37, 38, 39, 41, + 42, 43, 46, 47, 51, 53, 55, 57, 58, + 59, 61, 62, 65, 66, 67, 69, 70, + 71, 73, 74, 77, 78, 79, 82, 83, 85, + 86, 87, 89, 91, 93, 94, 95, 97, + 101, 102, 103, 105, 106, 107, 109, + 110, 111, 113)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (squarefree(500) == listOf(1, 2, 3, 5, 6, 7, 10, 11, 13, + 14, 15, 17, 19, 21, 22, 23, + 26, 29, 30, 31, 33, 34, 35, 37, + 38, 39, 41, 42, 43, 46, + 47, 51, 53, 55, 57, 58, 59, + 61, 62, 65, 66, 67, 69, 70, + 71, 73, 74, 77, 78, 79, 82, + 83, 85, 86, 87, 89, 91, 93, + 94, 95, 97, 101, 102, 103, + 105, 106, 107, 109, 110, 111, + 113, 114, 115, 118, 119, 122, + 123, 127, 129, 130, 131, + 133, 134, 137, 138, 139, 141, + 142, 143, 145, 146, 149, + 151, 154, 155, 157, 158, 159, + 161, 163, 165, 166, 167, + 170, 173, 174, 177, 178, 179, + 181, 182, 183, 185, 186, + 187, 190, 191, 193, 194, 195, + 197, 199, 201, 202, 203, + 205, 206, 209, 210, 211, 213, + 214, 215, 217, 218, 219, + 221, 222, 223, 226, 227, 229, + 230, 231, 233, 235, 237, + 238, 239, 241, 246, 247, 249, + 251, 253, 254, 255, 257, + 258, 259, 262, 263, 265, 266, + 267, 269, 271, 273, 274, + 277, 278, 281, 282, 283, 285, + 286, 287, 290, 291, 293, + 295, 298, 299, 301, 302, 303, + 305, 307, 309, 310, 311, + 313, 314, 317, 318, 319, 321, + 322, 323, 326, 327, 329, + 330, 331, 334, 335, 337, 339, + 341, 345, 346, 347, 349, + 353, 354, 355, 357, 358, 359, + 362, 365, 366, 367, 370, + 371, 373, 374, 377, 379, 381, + 382, 383, 385, 386, 389, + 390, 391, 393, 394, 395, 397, + 398, 399, 401, 402, 403, + 406, 407, 409, 410, 411, 413, + 415, 417, 418, 419, 421, + 422, 426, 427, 429, 430, 431, + 433, 434, 435, 437, 438, + 439, 442, 443, 445, 446, 447, + 449, 451, 453, 454, 455, + 457, 458, 461, 462, 463, 465, + 466, 467, 469, 470, 471, + 473, 474, 478, 479, 481, 482, + 483, 485, 487, 489, 491, + 493, 494, 497, 498, 499)) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-150/roger-bell-west/lua/ch-1.lua b/challenge-150/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..6732c948d6 --- /dev/null +++ b/challenge-150/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,22 @@ +#! /usr/bin/lua + +function fibstr(aa,bb,limit) + a=aa + b=bb + while true do + c = a .. b + print(c) + if #c >= limit then + return string.sub(c,limit,limit) + end + a=b + b=c + end +end + +if fibstr("1234","5678",51) == "7" then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-150/roger-bell-west/lua/ch-2.lua b/challenge-150/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..6ecd6e71b4 --- /dev/null +++ b/challenge-150/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,179 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + + -- Check each key-value pair + -- We have to do this both ways in case we miss some. + -- TODO: Could probably be smarter and not check those we've + -- already checked though! + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + + return true +end + +function genprimes(mx) + local primesh = {} + for i = 2, 3 do + primesh[i] = true + end + for i = 6, mx+1, 6 do + for j = i-1, i+1, 2 do + if j <= mx then + primesh[j]=true + end + end + end + local q={2,3,5,7} + local p=table.remove(q,1) + local mr=math.floor(math.sqrt(mx)) + while p <= mr do + if primesh[p] ~= nil then + for i = p*p,mx,p do + primesh[i] = nil + end + end + if #q < 2 then + table.insert(q,q[#q]+4) + table.insert(q,q[#q]+2) + end + p=table.remove(q,1) + end + local primes = {} + for k,v in pairs(primesh) do + table.insert(primes,k) + end + table.sort(primes) + return primes +end + +function squarefree(max) + out={} + primes=genprimes(math.floor(math.sqrt(max))) + plimit=1 + for candidate = 1,max do + sf=true + while plimit * plimit < candidate do + plimit = plimit + 1 + end + cc=candidate + for i,pr in ipairs(primes) do + if pr > plimit then + break + end + n=0 + while cc % pr == 0 do + n = n + 1 + if n > 1 then + sf=false + break + end + cc=math.floor(cc/pr) + end + if not sf then + break + end + end + if sf then + table.insert(out,candidate) + end + end + return out +end + +if recursive_compare(squarefree(30),{1, 2, 3, 5, 6, 7, 10, 11, 13, 14, + 15, 17, 19, 21, 22, 23, 26, 29, + 30}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(squarefree(113),{1, 2, 3, 5, 6, 7, 10, 11, 13, + 14, 15, 17, 19, 21, 22, 23, 26, + 29, 30, 31, 33, 34, 35, 37, 38, + 39, 41, 42, 43, 46, 47, 51, 53, + 55, 57, 58, 59, 61, 62, 65, 66, + 67, 69, 70, 71, 73, 74, 77, 78, + 79, 82, 83, 85, 86, 87, 89, 91, + 93, 94, 95, 97, 101, 102, 103, + 105, 106, 107, 109, 110, 111, + 113}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(squarefree(500),{1, 2, 3, 5, 6, 7, 10, 11, 13, + 14, 15, 17, 19, 21, 22, 23, 26, + 29, 30, 31, 33, 34, 35, 37, 38, + 39, 41, 42, 43, 46, 47, 51, 53, + 55, 57, 58, 59, 61, 62, 65, 66, + 67, 69, 70, 71, 73, 74, 77, 78, + 79, 82, 83, 85, 86, 87, 89, 91, + 93, 94, 95, 97, 101, 102, 103, + 105, 106, 107, 109, 110, 111, + 113, 114, 115, 118, 119, 122, + 123, 127, 129, 130, 131, 133, + 134, 137, 138, 139, 141, 142, + 143, 145, 146, 149, 151, 154, + 155, 157, 158, 159, 161, 163, + 165, 166, 167, 170, 173, 174, + 177, 178, 179, 181, 182, 183, + 185, 186, 187, 190, 191, 193, + 194, 195, 197, 199, 201, 202, + 203, 205, 206, 209, 210, 211, + 213, 214, 215, 217, 218, 219, + 221, 222, 223, 226, 227, 229, + 230, 231, 233, 235, 237, 238, + 239, 241, 246, 247, 249, 251, + 253, 254, 255, 257, 258, 259, + 262, 263, 265, 266, 267, 269, + 271, 273, 274, 277, 278, 281, + 282, 283, 285, 286, 287, 290, + 291, 293, 295, 298, 299, 301, + 302, 303, 305, 307, 309, 310, + 311, 313, 314, 317, 318, 319, + 321, 322, 323, 326, 327, 329, + 330, 331, 334, 335, 337, 339, + 341, 345, 346, 347, 349, 353, + 354, 355, 357, 358, 359, 362, + 365, 366, 367, 370, 371, 373, + 374, 377, 379, 381, 382, 383, + 385, 386, 389, 390, 391, 393, + 394, 395, 397, 398, 399, 401, + 402, 403, 406, 407, 409, 410, + 411, 413, 415, 417, 418, 419, + 421, 422, 426, 427, 429, 430, + 431, 433, 434, 435, 437, 438, + 439, 442, 443, 445, 446, 447, + 449, 451, 453, 454, 455, 457, + 458, 461, 462, 463, 465, 466, + 467, 469, 470, 471, 473, 474, + 478, 479, 481, 482, 483, 485, + 487, 489, 491, 493, 494, 497, + 498, 499}) then + io.write("Pass") +else + io.write("FAIL") +end + +print("") diff --git a/challenge-150/roger-bell-west/perl/ch-1.pl b/challenge-150/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..5e5af64e7d --- /dev/null +++ b/challenge-150/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +is(fibstr("1234","5678",51),'7','example 1'); + +sub fibstr { + my ($aa,$bb,$limit) = @_; + my $a=$aa; + my $b=$bb; + while (1) { + my $c=$a.$b; + print "$c\n"; + if (length($c) >= $limit) { + return substr($c,$limit-1,1); + } + ($a,$b)=($b,$c); + } +} diff --git a/challenge-150/roger-bell-west/perl/ch-2.pl b/challenge-150/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..93b4ca9916 --- /dev/null +++ b/challenge-150/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,119 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +is_deeply(squarefree(30),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30 + ],'example 1'); + +is_deeply(squarefree(113),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, + 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, 69, + 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, 91, + 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, + 111, 113 + ],'example 2'); + +is_deeply(squarefree(500),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, + 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, 69, + 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, 91, + 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, + 111, 113, 114, 115, 118, 119, 122, 123, 127, 129, 130, + 131, 133, 134, 137, 138, 139, 141, 142, 143, 145, 146, + 149, 151, 154, 155, 157, 158, 159, 161, 163, 165, 166, + 167, 170, 173, 174, 177, 178, 179, 181, 182, 183, 185, + 186, 187, 190, 191, 193, 194, 195, 197, 199, 201, 202, + 203, 205, 206, 209, 210, 211, 213, 214, 215, 217, 218, + 219, 221, 222, 223, 226, 227, 229, 230, 231, 233, 235, + 237, 238, 239, 241, 246, 247, 249, 251, 253, 254, 255, + 257, 258, 259, 262, 263, 265, 266, 267, 269, 271, 273, + 274, 277, 278, 281, 282, 283, 285, 286, 287, 290, 291, + 293, 295, 298, 299, 301, 302, 303, 305, 307, 309, 310, + 311, 313, 314, 317, 318, 319, 321, 322, 323, 326, 327, + 329, 330, 331, 334, 335, 337, 339, 341, 345, 346, 347, + 349, 353, 354, 355, 357, 358, 359, 362, 365, 366, 367, + 370, 371, 373, 374, 377, 379, 381, 382, 383, 385, 386, + 389, 390, 391, 393, 394, 395, 397, 398, 399, 401, 402, + 403, 406, 407, 409, 410, 411, 413, 415, 417, 418, 419, + 421, 422, 426, 427, 429, 430, 431, 433, 434, 435, 437, + 438, 439, 442, 443, 445, 446, 447, 449, 451, 453, 454, + 455, 457, 458, 461, 462, 463, 465, 466, 467, 469, 470, + 471, 473, 474, 478, 479, 481, 482, 483, 485, 487, 489, + 491, 493, 494, 497, 498, 499, + ],'example 3'); + +sub genprimes { + my $mx=shift; + my @primes; + { + my %primesh=map {$_ => 1} (2,3); + for (my $i=6;$i <= $mx+1; $i += 6) { + foreach my $j ($i-1,$i+1) { + if ($j <= $mx) { + $primesh{$j}=1; + } + } + } + my @q=(2,3,5,7); + my $p=shift @q; + my $mr=int(sqrt($mx)); + while ($p <= $mr) { + if ($primesh{$p}) { + my $i=$p*$p; + while ($i <= $mx) { + delete $primesh{$i}; + $i += $p; + } + } + if (scalar @q < 2) { + push @q,$q[-1]+4; + push @q,$q[-1]+2; + } + $p=shift @q; + } + @primes=sort {$a <=> $b} keys %primesh; + } + return \@primes; +} + +sub squarefree { + my $max=shift; + my @out; + my @primes=@{genprimes(int(sqrt($max)))}; + my $plimit=1; + foreach my $candidate (1..$max) { + my $squarefree=1; + while ($plimit * $plimit < $candidate) { + $plimit++; + } + my $cc=$candidate; + foreach my $pr (@primes) { + if ($pr > $plimit) { + last; + } + my $n=0; + while ($cc % $pr == 0) { + $n++; + if ($n>1) { + $squarefree=0; + last; + } + $cc=int($cc/$pr); + } + unless ($squarefree) { + last; + } + } + if ($squarefree) { + push @out,$candidate; + } + } + return \@out; +} diff --git a/challenge-150/roger-bell-west/postscript/ch-1.ps b/challenge-150/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..16e003a2e4 --- /dev/null +++ b/challenge-150/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,21 @@ +/fibstr { + 4 dict begin + /limit exch def + /b exch def + /a exch def + { + /c a length b length add string def + a c copy + c a length b putinterval + c = + c length limit ge { + exit + } if + /a b def + /b c def + } loop + c limit 1 sub 1 getinterval + end +} bind def + +(1234) (5678) 51 fibstr (7) eq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-150/roger-bell-west/postscript/ch-2.ps b/challenge-150/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..8b9c905f2d --- /dev/null +++ b/challenge-150/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,226 @@ +%!PS + +/aeq { + 3 dict begin + /a exch def + /b exch def + a length b length eq { + /e true def + 0 1 a length 1 sub { + dup a exch get + exch b exch get ne { + /e false def + exit + } if + } for + e + } { + false + } ifelse + end +} bind def + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + 0 arr length 1 sub quicksort.main + arr + end +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/genprimes { + /mx exch def + /primesh mx dict def + 2 1 3 { + primesh exch true put + } for + 6 6 mx 1 add { + dup 1 sub exch 1 add 2 exch { + dup mx le { + primesh exch true put + } { + pop + } ifelse + } for + } for + /q [ 3 5 7 ] def + /qi 0 def + /p 2 def + /mr mx sqrt cvi def + { + p mr le not { + exit + } if + primesh p known { + p dup mul p mx { + primesh exch undef + } for + } if + q length qi sub 2 le { + /q q q q length 1 sub get 4 add apush def + /q q q q length 1 sub get 2 add apush def + } if + /p q qi get def + /qi qi 1 add def + } loop + /primes 0 array def + primesh { + pop + /primes exch primes exch apush def + } forall + primes quicksort +} bind def + +/squarefree { + 8 dict begin + /max exch def + /out 0 array def + /primes max sqrt cvi genprimes def + /plimit 1 def + 1 1 max { + /candidate exch def + /sf true def + { + plimit plimit mul candidate lt { + /plimit plimit 1 add def + } { + exit + } ifelse + } loop + /cc candidate def + primes { + /pr exch def + pr plimit gt { + exit + } if + /n 0 def + { + cc pr mod 0 eq { + /n n 1 add def + n 1 gt { + /sf false def + exit + } if + /cc cc pr idiv def + } { + exit + } ifelse + } loop + sf not { + exit + } if + } forall + sf { + /out out candidate apush def + } if + } for + out + end +} bind def + +30 squarefree [ 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 ] +aeq { (Pass) } { (FAIL) } ifelse print ( ) print + + +113 squarefree [ 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 + 26 29 30 31 33 34 35 37 38 39 41 42 43 46 + 47 51 53 55 57 58 59 61 62 65 66 67 69 70 + 71 73 74 77 78 79 82 83 85 86 87 89 91 93 + 94 95 97 101 102 103 105 106 107 109 110 111 + 113 ] +aeq { (Pass) } { (FAIL) } ifelse print ( ) print + + +500 squarefree [ 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 + 26 29 30 31 33 34 35 37 38 39 41 42 43 46 + 47 51 53 55 57 58 59 61 62 65 66 67 69 70 + 71 73 74 77 78 79 82 83 85 86 87 89 91 93 + 94 95 97 101 102 103 105 106 107 109 110 111 + 113 114 115 118 119 122 123 127 129 130 131 + 133 134 137 138 139 141 142 143 145 146 149 + 151 154 155 157 158 159 161 163 165 166 167 + 170 173 174 177 178 179 181 182 183 185 186 + 187 190 191 193 194 195 197 199 201 202 203 + 205 206 209 210 211 213 214 215 217 218 219 + 221 222 223 226 227 229 230 231 233 235 237 + 238 239 241 246 247 249 251 253 254 255 257 + 258 259 262 263 265 266 267 269 271 273 274 + 277 278 281 282 283 285 286 287 290 291 293 + 295 298 299 301 302 303 305 307 309 310 311 + 313 314 317 318 319 321 322 323 326 327 329 + 330 331 334 335 337 339 341 345 346 347 349 + 353 354 355 357 358 359 362 365 366 367 370 + 371 373 374 377 379 381 382 383 385 386 389 + 390 391 393 394 395 397 398 399 401 402 403 + 406 407 409 410 411 413 415 417 418 419 421 + 422 426 427 429 430 431 433 434 435 437 438 + 439 442 443 445 446 447 449 451 453 454 455 + 457 458 461 462 463 465 466 467 469 470 471 + 473 474 478 479 481 482 483 485 487 489 491 + 493 494 497 498 499 ] +aeq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-150/roger-bell-west/python/ch-1.py b/challenge-150/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..364860a40b --- /dev/null +++ b/challenge-150/roger-bell-west/python/ch-1.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +import unittest + +def fibstr(aa,bb,limit): + a=aa + b=bb + while True: + c=a+b + print(c) + if len(c) >= limit: + return c[limit-1] + a=b + b=c + +class TestFds(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(fibstr("1234","5678",51),"7",'example 1') + +unittest.main() diff --git a/challenge-150/roger-bell-west/python/ch-2.py b/challenge-150/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4efb99b1c9 --- /dev/null +++ b/challenge-150/roger-bell-west/python/ch-2.py @@ -0,0 +1,138 @@ +#! /usr/bin/python3 + +import unittest + +from math import sqrt,floor +from collections import deque + +def genprimes(mx): + primesh=set(range(2,4)) + for i in range(6,mx+2,6): + for j in range(i-1,i+2,2): + if j <= mx: + primesh.add(j) + q=deque([2,3,5,7]) + p=q.popleft() + mr=floor(sqrt(mx)) + while p <= mr: + if p in primesh: + for i in range(p*p,mx+1,p): + primesh.discard(i) + if len(q) < 2: + q.append(q[-1]+4) + q.append(q[-1]+2) + p=q.popleft() + primes=list(primesh) + primes.sort() + return primes + +def squarefree(max): + out=[] + primes=genprimes(int(sqrt(max))) + plimit=1 + for candidate in range(1,max+1): + squarefree=True + while plimit * plimit < candidate: + plimit += 1 + cc=candidate + for pr in primes: + if pr > plimit: + break + n=0 + while cc % pr == 0: + n += 1 + if n > 1: + squarefree=False + break + cc /= pr + if not squarefree: + break + if squarefree: + out.append(candidate) + return out + +class TestSquarefree(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(squarefree(30),[1, 2, 3, 5, 6, 7, 10, 11, + 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30],'example 1') + + def test_ex2(self): + self.assertEqual(squarefree(113),[1, 2, 3, 5, 6, 7, 10, 11, + 13, 14, 15, 17, 19, 21, + 22, 23, 26, 29, 30, 31, + 33, 34, 35, 37, 38, 39, + 41, 42, 43, 46, 47, 51, + 53, 55, 57, 58, 59, 61, + 62, 65, 66, 67, 69, 70, + 71, 73, 74, 77, 78, 79, + 82, 83, 85, 86, 87, 89, + 91, 93, 94, 95, 97, 101, + 102, 103, 105, 106, 107, + 109, 110, 111, + 113],'example 2') + + def test_ex3(self): + self.assertEqual(squarefree(500),[1, 2, 3, 5, 6, 7, 10, 11, + 13, 14, 15, 17, 19, 21, + 22, 23, 26, 29, 30, 31, + 33, 34, 35, 37, 38, 39, + 41, 42, 43, 46, 47, 51, + 53, 55, 57, 58, 59, 61, + 62, 65, 66, 67, 69, 70, + 71, 73, 74, 77, 78, 79, + 82, 83, 85, 86, 87, 89, + 91, 93, 94, 95, 97, 101, + 102, 103, 105, 106, 107, + 109, 110, 111, 113, 114, + 115, 118, 119, 122, 123, + 127, 129, 130, 131, 133, + 134, 137, 138, 139, 141, + 142, 143, 145, 146, 149, + 151, 154, 155, 157, 158, + 159, 161, 163, 165, 166, + 167, 170, 173, 174, 177, + 178, 179, 181, 182, 183, + 185, 186, 187, 190, 191, + 193, 194, 195, 197, 199, + 201, 202, 203, 205, 206, + 209, 210, 211, 213, 214, + 215, 217, 218, 219, 221, + 222, 223, 226, 227, 229, + 230, 231, 233, 235, 237, + 238, 239, 241, 246, 247, + 249, 251, 253, 254, 255, + 257, 258, 259, 262, 263, + 265, 266, 267, 269, 271, + 273, 274, 277, 278, 281, + 282, 283, 285, 286, 287, + 290, 291, 293, 295, 298, + 299, 301, 302, 303, 305, + 307, 309, 310, 311, 313, + 314, 317, 318, 319, 321, + 322, 323, 326, 327, 329, + 330, 331, 334, 335, 337, + 339, 341, 345, 346, 347, + 349, 353, 354, 355, 357, + 358, 359, 362, 365, 366, + 367, 370, 371, 373, 374, + 377, 379, 381, 382, 383, + 385, 386, 389, 390, 391, + 393, 394, 395, 397, 398, + 399, 401, 402, 403, 406, + 407, 409, 410, 411, 413, + 415, 417, 418, 419, 421, + 422, 426, 427, 429, 430, + 431, 433, 434, 435, 437, + 438, 439, 442, 443, 445, + 446, 447, 449, 451, 453, + 454, 455, 457, 458, 461, + 462, 463, 465, 466, 467, + 469, 470, 471, 473, 474, + 478, 479, 481, 482, 483, + 485, 487, 489, 491, 493, + 494, 497, 498, + 499],'example 3') + +unittest.main() diff --git a/challenge-150/roger-bell-west/raku/ch-1.p6 b/challenge-150/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..05b67e2492 --- /dev/null +++ b/challenge-150/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is(fibstr("1234","5678",51),'7','example 1'); + +sub fibstr($aa,$bb,$limit) { + my $a=$aa; + my $b=$bb; + loop { + my $c=$a ~ $b; + say $c; + if (chars($c) >= $limit) { + return substr($c,$limit-1,1); + } + ($a,$b)=($b,$c); + } +} diff --git a/challenge-150/roger-bell-west/raku/ch-2.p6 b/challenge-150/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..1c5363be3c --- /dev/null +++ b/challenge-150/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,116 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is-deeply(squarefree(30),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30 + ],'example 1'); + +is-deeply(squarefree(113),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, + 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, 69, + 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, 91, + 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, + 111, 113 + ],'example 2'); + +is-deeply(squarefree(500),[ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, + 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, + 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, 69, + 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, 91, + 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, + 111, 113, 114, 115, 118, 119, 122, 123, 127, 129, 130, + 131, 133, 134, 137, 138, 139, 141, 142, 143, 145, 146, + 149, 151, 154, 155, 157, 158, 159, 161, 163, 165, 166, + 167, 170, 173, 174, 177, 178, 179, 181, 182, 183, 185, + 186, 187, 190, 191, 193, 194, 195, 197, 199, 201, 202, + 203, 205, 206, 209, 210, 211, 213, 214, 215, 217, 218, + 219, 221, 222, 223, 226, 227, 229, 230, 231, 233, 235, + 237, 238, 239, 241, 246, 247, 249, 251, 253, 254, 255, + 257, 258, 259, 262, 263, 265, 266, 267, 269, 271, 273, + 274, 277, 278, 281, 282, 283, 285, 286, 287, 290, 291, + 293, 295, 298, 299, 301, 302, 303, 305, 307, 309, 310, + 311, 313, 314, 317, 318, 319, 321, 322, 323, 326, 327, + 329, 330, 331, 334, 335, 337, 339, 341, 345, 346, 347, + 349, 353, 354, 355, 357, 358, 359, 362, 365, 366, 367, + 370, 371, 373, 374, 377, 379, 381, 382, 383, 385, 386, + 389, 390, 391, 393, 394, 395, 397, 398, 399, 401, 402, + 403, 406, 407, 409, 410, 411, 413, 415, 417, 418, 419, + 421, 422, 426, 427, 429, 430, 431, 433, 434, 435, 437, + 438, 439, 442, 443, 445, 446, 447, 449, 451, 453, 454, + 455, 457, 458, 461, 462, 463, 465, 466, 467, 469, 470, + 471, 473, 474, 478, 479, 481, 482, 483, 485, 487, 489, + 491, 493, 494, 497, 498, 499, + ],'example 3'); + +sub genprimes($mx) { + my @primes; + { + my $primesh=(2,3).SetHash; + loop (my $i=6;$i <= $mx+1; $i += 6) { + for ($i-1,$i+1) -> $j { + if ($j <= $mx) { + $primesh{$j}=True; + } + } + } + my $p=2; + my @q=[2,3,5,7]; + my $mr=floor(sqrt($mx)); + while ($p <= $mr) { + if ($primesh{$p}:exists) { + my $i=$p*$p; + while ($i <= $mx) { + $primesh{$i}:delete; + $i += $p; + } + } + if (@q.elems < 2) { + @q.push(@q[*-1]+4); + @q.push(@q[*-1]+2); + } + $p=@q.shift; + } + @primes=$primesh.keys.sort; + } + return @primes; +} + +sub squarefree($max) { + my @out; + my @primes=genprimes(floor(sqrt($max))); + my $plimit=1; + for 1..$max -> $candidate { + my $squarefree=True; + while ($plimit * $plimit < $candidate) { + $plimit++; + } + my $cc=$candidate; + for @primes -> $pr { + if ($pr > $plimit) { + last; + } + my $n=0; + while ($cc % $pr == 0) { + $n++; + if ($n>1) { + $squarefree=False; + last; + } + $cc div= $pr; + } + unless ($squarefree) { + last; + } + } + if ($squarefree) { + @out.push($candidate); + } + } + return @out; +} diff --git a/challenge-150/roger-bell-west/ruby/ch-1.rb b/challenge-150/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..f4f421f1a1 --- /dev/null +++ b/challenge-150/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,25 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def fibstr(aa,bb,limit) + a=aa + b=bb + while true do + c=a+b + print("#{c}\n") + if c.length >= limit then + return c[limit-1] + end + a=b + b=c + end +end + +class TestFibstr < Test::Unit::TestCase + + def test_ex1 + assert_equal("7",fibstr("1234","5678",51)) + end + +end diff --git a/challenge-150/roger-bell-west/ruby/ch-2.rb b/challenge-150/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..a1dd36f992 --- /dev/null +++ b/challenge-150/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,114 @@ +#! /usr/bin/ruby + +require 'set' + +def genprimes(mx) + primesh=Set.new([2,3]) + (6..mx+1).step(6) do |i| + (i-1..i+1).step(2) do |j| + if j <= mx then + primesh.add(j) + end + end + end + q=[2,3,5,7] + p=q.shift + mr=Integer.sqrt(mx) + while p <= mr do + if primesh.include?(p) + (p*p..mx).step(p) do |i| + primesh.delete?(i) + end + end + if q.length<2 then + q.push(q[-1]+4) + q.push(q[-1]+2) + end + p=q.shift + end + return primesh.each.sort +end + +def squarefree(max) + out=[] + primes=genprimes(Integer.sqrt(max)) + plimit=1 + 1.upto(max) do |candidate| + squarefree=true + while plimit * plimit < candidate do + plimit += 1 + end + cc=candidate + primes.each do |pr| + if pr > plimit then + break + end + n=0 + while cc % pr == 0 do + n += 1 + if n > 1 then + squarefree=false + break + end + cc=(cc/pr).to_i + end + if !squarefree then + break + end + end + if squarefree then + out.push(candidate) + end + end + return out +end + +require 'test/unit' + +class TestSquarefree < Test::Unit::TestCase + + def test_ex1 + assert_equal([1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, + 22, 23, 26, 29, 30],squarefree(30)) + end + + def test_ex2 + assert_equal([1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, + 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, + 43, 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, + 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, + 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, + 110, 111, 113],squarefree(113)) + end + + def test_ex3 + assert_equal([1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, + 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, + 43, 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, + 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, + 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, + 110, 111, 113, 114, 115, 118, 119, 122, 123, 127, 129, + 130, 131, 133, 134, 137, 138, 139, 141, 142, 143, 145, + 146, 149, 151, 154, 155, 157, 158, 159, 161, 163, 165, + 166, 167, 170, 173, 174, 177, 178, 179, 181, 182, 183, + 185, 186, 187, 190, 191, 193, 194, 195, 197, 199, 201, + 202, 203, 205, 206, 209, 210, 211, 213, 214, 215, 217, + 218, 219, 221, 222, 223, 226, 227, 229, 230, 231, 233, + 235, 237, 238, 239, 241, 246, 247, 249, 251, 253, 254, + 255, 257, 258, 259, 262, 263, 265, 266, 267, 269, 271, + 273, 274, 277, 278, 281, 282, 283, 285, 286, 287, 290, + 291, 293, 295, 298, 299, 301, 302, 303, 305, 307, 309, + 310, 311, 313, 314, 317, 318, 319, 321, 322, 323, 326, + 327, 329, 330, 331, 334, 335, 337, 339, 341, 345, 346, + 347, 349, 353, 354, 355, 357, 358, 359, 362, 365, 366, + 367, 370, 371, 373, 374, 377, 379, 381, 382, 383, 385, + 386, 389, 390, 391, 393, 394, 395, 397, 398, 399, 401, + 402, 403, 406, 407, 409, 410, 411, 413, 415, 417, 418, + 419, 421, 422, 426, 427, 429, 430, 431, 433, 434, 435, + 437, 438, 439, 442, 443, 445, 446, 447, 449, 451, 453, + 454, 455, 457, 458, 461, 462, 463, 465, 466, 467, 469, + 470, 471, 473, 474, 478, 479, 481, 482, 483, 485, 487, + 489, 491, 493, 494, 497, 498, 499],squarefree(500)) + end + +end diff --git a/challenge-150/roger-bell-west/rust/ch-1.rs b/challenge-150/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e00d44cbb3 --- /dev/null +++ b/challenge-150/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,21 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(fibstr("1234", "5678", 51), '7'); +} + +fn fibstr(aa: &str, bb: &str, limit: usize) -> char { + let mut a = aa.to_string(); + let mut b = bb.to_string(); + loop { + let c = a + &b; + println!("{}", c); + if c.len() >= limit { + return c.chars().nth(limit - 1).unwrap(); + } + a = b; + b = c; + } +} diff --git a/challenge-150/roger-bell-west/rust/ch-2.rs b/challenge-150/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..dd93db19f5 --- /dev/null +++ b/challenge-150/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,124 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::{BTreeSet, VecDeque}; +use std::iter::FromIterator; + +#[test] +fn test_ex1() { + assert_eq!( + squarefree(30), + vec![ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, + 30 + ] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + squarefree(113), + vec![ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, + 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 51, 53, 55, 57, + 58, 59, 61, 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, + 85, 86, 87, 89, 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, + 109, 110, 111, 113 + ] + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + squarefree(500), + vec![ + 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, + 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 51, 53, 55, 57, + 58, 59, 61, 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, + 85, 86, 87, 89, 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, + 109, 110, 111, 113, 114, 115, 118, 119, 122, 123, 127, 129, 130, + 131, 133, 134, 137, 138, 139, 141, 142, 143, 145, 146, 149, 151, + 154, 155, 157, 158, 159, 161, 163, 165, 166, 167, 170, 173, 174, + 177, 178, 179, 181, 182, 183, 185, 186, 187, 190, 191, 193, 194, + 195, 197, 199, 201, 202, 203, 205, 206, 209, 210, 211, 213, 214, + 215, 217, 218, 219, 221, 222, 223, 226, 227, 229, 230, 231, 233, + 235, 237, 238, 239, 241, 246, 247, 249, 251, 253, 254, 255, 257, + 258, 259, 262, 263, 265, 266, 267, 269, 271, 273, 274, 277, 278, + 281, 282, 283, 285, 286, 287, 290, 291, 293, 295, 298, 299, 301, + 302, 303, 305, 307, 309, 310, 311, 313, 314, 317, 318, 319, 321, + 322, 323, 326, 327, 329, 330, 331, 334, 335, 337, 339, 341, 345, + 346, 347, 349, 353, 354, 355, 357, 358, 359, 362, 365, 366, 367, + 370, 371, 373, 374, 377, 379, 381, 382, 383, 385, 386, 389, 390, + 391, 393, 394, 395, 397, 398, 399, 401, 402, 403, 406, 407, 409, + 410, 411, 413, 415, 417, 418, 419, 421, 422, 426, 427, 429, 430, + 431, 433, 434, 435, 437, 438, 439, 442, 443, 445, 446, 447, 449, + 451, 453, 454, 455, 457, 458, 461, 462, 463, 465, 466, 467, 469, + 470, 471, 473, 474, 478, 479, 481, 482, 483, 485, 487, 489, 491, + 493, 494, 497, 498, 499, + ] + ); +} + +fn genprimes(mx: u32) -> Vec { + let mut primesh: BTreeSet = BTreeSet::from_iter(2..=3); + for i in (6..=mx + 1).step_by(6) { + for j in [i - 1, i + 1] { + if j <= mx { + primesh.insert(j); + } + } + } + let mut q = VecDeque::from([2, 3, 5, 7]); + let mut p = q.pop_front().unwrap(); + let mr = (mx as f64).sqrt() as u32; + while p <= mr { + if primesh.contains(&p) { + for i in (p * p..=mx).step_by(p as usize) { + primesh.remove(&i); + } + } + if q.len() < 2 { + let t = q[0] + 4; + q.push_back(t); + q.push_back(t + 2); + } + p = q.pop_front().unwrap(); + } + primesh.iter().map(|i| *i).collect::>() +} + +fn squarefree(max: u32) -> Vec { + let mut out: Vec = Vec::new(); + let primes = genprimes((max as f64).sqrt() as u32); + let mut plimit = 1; + for candidate in 1..=max { + let mut squarefree = true; + while plimit * plimit < candidate { + plimit += 1; + } + let mut cc = candidate; + for &pr in &primes { + if pr > plimit { + break; + } + let mut n = 0; + while cc % pr == 0 { + n += 1; + if n > 1 { + squarefree = false; + break; + } + cc = cc / pr; + } + if !squarefree { + break; + } + } + if squarefree { + out.push(candidate); + } + } + out +} -- cgit From 786a071b0dab9f93c0c230ead72fda7fcfac3746 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Mon, 31 Jan 2022 21:38:02 +0800 S