diff options
18 files changed, 890 insertions, 0 deletions
diff --git a/challenge-198/roger-bell-west/javascript/ch-1.js b/challenge-198/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..0981f12f39 --- /dev/null +++ b/challenge-198/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,31 @@ +#! /usr/bin/node + +"use strict" + +function maxgap(l0) { + if (l0.length < 2) { + return 0; + } + let l = l0; + l.sort(); + let q = []; + for (let i = 0; i < l.length - 1; i++) { + q.push(l[i + 1] - l[i]); + } + let mq = Math.max(...q); + return q.filter(i => i == mq).length; +} + +if (maxgap([2, 5, 8, 1]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (maxgap([3]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-198/roger-bell-west/javascript/ch-2.js b/challenge-198/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..bbe7915d90 --- /dev/null +++ b/challenge-198/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,70 @@ +#! /usr/bin/node + +"use strict" + +function genprimes(mx) { + let primesh=new Set; + for (let i = 2; i <= Math.min(3, mx); i++) { + primesh.add(i); + } + for (let i = 6; i <= mx; 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 primecount(l) { + return genprimes(l - 1).length; +} + +if (primecount(10) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (primecount(15) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (primecount(1) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (primecount(25) == 9) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-198/roger-bell-west/kotlin/ch-1.kt b/challenge-198/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..a180e6f1bd --- /dev/null +++ b/challenge-198/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,29 @@ +fun maxgap(l0: List<Int>): Int { + if (l0.size < 2) { + return 0 + } + var l = ArrayList(l0) + l.sort() + var q = ArrayList<Int>() + for (i in l.windowed(size = 2)) { + q.add(i[1] - i[0]) + } + val mq = q.maxOrNull()!! + return q.filter{it == mq}.size +} + +fun main() { + if (maxgap(listOf(2, 5, 8, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + + if (maxgap(listOf(3)) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") +} diff --git a/challenge-198/roger-bell-west/kotlin/ch-2.kt b/challenge-198/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..8033e407ad --- /dev/null +++ b/challenge-198/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,67 @@ +import kotlin.math.* + +fun genprimes(mx: Int): ArrayList<Int> { + var primesh=mutableSetOf<Int>() + for (i in 2..min(3, mx)) { + 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 primecount(l: Int): Int { + return genprimes(l-1).size +} + +fun main() { + if (primecount(10) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + + if (primecount(15) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + + if (primecount(1) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + + if (primecount(25) == 9) { + print("Pass") + } else { + print("Fail") + } + println("") +} diff --git a/challenge-198/roger-bell-west/lua/ch-1.lua b/challenge-198/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..7c73992f03 --- /dev/null +++ b/challenge-198/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,34 @@ +#! /usr/bin/lua + +function maxgap(l0) + if #l0 < 2 then + return 0 + end + l = l0 + table.sort(l0) + q = {} + for i = 1, #l-1 do + table.insert(q, l[i + 1] - l[i]) + end + mq = math.max(table.unpack(q)) + r = 0 + for i, v in ipairs(q) do + if v == mq then + r = r + 1 + end + end + return r +end + +if maxgap({2, 5, 8, 1}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if maxgap({3}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-198/roger-bell-west/lua/ch-2.lua b/challenge-198/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..3bcf2a71ef --- /dev/null +++ b/challenge-198/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,65 @@ +#! /usr/bin/lua + +function genprimes(mx) + local primesh = {} + for i = 2, math.min(3, mx) do + primesh[i] = true + end + for i = 6, mx, 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 primecount(l) + return #genprimes(l-1) +end + +if primecount(10) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if primecount(15) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if primecount(1) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if primecount(25) == 9 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-198/roger-bell-west/perl/ch-1.pl b/challenge-198/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..28229d7e5a --- /dev/null +++ b/challenge-198/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +use List::Util qw(max); + +is(maxgap([2, 5, 8, 1]), 2, 'example 1'); +is(maxgap([3]), 0, 'example 2'); + +sub maxgap($l0) { + if (scalar @{$l0} < 2) { + return 0; + } + my @l = sort @{$l0}; + my @q; + foreach my $i (0 .. $#l-1) { + push @q,$l[$i+1] - $l[$i]; + } + my $mq = max(@q); + return scalar grep {$_ == $mq} @q; +} diff --git a/challenge-198/roger-bell-west/perl/ch-2.pl b/challenge-198/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..5686e11bbf --- /dev/null +++ b/challenge-198/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,18 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Math::Prime::Util qw(primes); + +use Test::More tests => 4; + +is(primecount(10), 4, 'example 1'); +is(primecount(15), 6, 'example 2'); +is(primecount(1), 0, 'example 3'); +is(primecount(25), 9, 'example 4'); + +sub primecount($l) { + return scalar @{primes($l-1)}; +} diff --git a/challenge-198/roger-bell-west/postscript/ch-1.ps b/challenge-198/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..939fc74006 --- /dev/null +++ b/challenge-198/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,160 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + 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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/listmax { + { max } reduce +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} 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 + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + 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 + + +% end included library code + +/maxgap { + 3 dict begin + quicksort /l exch def + l length 2 lt { + 0 + } { + [ + 0 1 l length 2 sub { + /i exch def + l i 1 add get l i get sub + } for + ] + dup listmax /lm exch def + { lm eq } filter length + } ifelse + end +} bind def + +(maxgap) test.start +[ 2 5 8 1 ] maxgap 2 eq test +[ 3 ] maxgap 0 eq test +test.end diff --git a/challenge-198/roger-bell-west/postscript/ch-2.ps b/challenge-198/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..7217df1d59 --- /dev/null +++ b/challenge-198/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,107 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/genprimes { + 6 dict begin + /mx exch def + /primesh mx dict def + 2 1 3 { + dup mx le { + primesh exch true put + } { + pop + } ifelse + } 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 + primesh keys + end +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/apush { apush.right } bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/primecount { + 1 sub genprimes length +} bind def + +(primecount) test.start +10 primecount 4 eq test +15 primecount 6 eq test +1 primecount 0 eq test +25 primecount 9 eq test +test.end diff --git a/challenge-198/roger-bell-west/python/ch-1.py b/challenge-198/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e8df81b500 --- /dev/null +++ b/challenge-198/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest +from itertools import tee + +def maxgap(l0): + if len(l0) < 2: + return 0 + l = l0 + l.sort() + q = [] + j, k = tee(l) + next(k, None) + for i in zip(j, k): + q.append(i[1] - i[0]) + qm = max(q) + return len([i for i in q if i == qm]) + +class TestMaxgap(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maxgap([2, 5, 8, 1]), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(maxgap([3]), 0, 'example 2') + +unittest.main() diff --git a/challenge-198/roger-bell-west/python/ch-2.py b/challenge-198/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..1a54e280d8 --- /dev/null +++ b/challenge-198/roger-bell-west/python/ch-2.py @@ -0,0 +1,46 @@ +#! /usr/bin/python3 + +import unittest + +from math import sqrt,floor +from collections import deque + +def genprimes(mx): + primesh=set(range(2,min(mx+1,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 primecount(l): + return len(genprimes(l-1)) + +class TestPrimecount(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(primecount(10), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(primecount(15), 6, 'example 2') + + def test_ex3(self): + self.assertEqual(primecount(1), 0, 'example 3') + + def test_ex4(self): + self.assertEqual(primecount(25), 9, 'example 4') + +unittest.main() diff --git a/challenge-198/roger-bell-west/raku/ch-1.p6 b/challenge-198/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..33295ed7ab --- /dev/null +++ b/challenge-198/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +sub maxgap(@l0) { + if (@l0.elems < 2) { + return 0; + } + my @l = sort @l0; + my @q; + for @l.rotor(2 => -1) -> @i { + push @q,@i[1] - @i[0]; + } + my $mq = @q.max; + return (grep {$_ == $mq}, @q).elems; +} + +is(maxgap([2, 5, 8, 1]), 2, 'example 1'); +is(maxgap([3]), 0, 'example 2'); diff --git a/challenge-198/roger-bell-west/raku/ch-2.p6 b/challenge-198/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..82742a1363 --- /dev/null +++ b/challenge-198/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,47 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is(primecount(10), 4, 'example 1'); +is(primecount(15), 6, 'example 2'); +is(primecount(1), 0, 'example 3'); +is(primecount(25), 9, 'example 4'); + +sub genprimes($mx) { + my @primes; + { + my $primesh=(2..(3,$mx).min).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 primecount($l) { + return genprimes($l-1).elems; +} diff --git a/challenge-198/roger-bell-west/ruby/ch-1.rb b/challenge-198/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..ee5eb58aa8 --- /dev/null +++ b/challenge-198/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,28 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def maxgap(l0) + if l0.length < 2 then + return 0 + end + l = l0.sort + q = [] + l.each_cons(2) do |i| + q.push(i[1] - i[0]) + end + mq = q.max + return q.count {|i| i == mq} +end + +class TestMaxgap < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, maxgap([2, 5, 8, 1])) + end + + def test_ex2 + assert_equal(0, maxgap([3])) + end + +end diff --git a/challenge-198/roger-bell-west/ruby/ch-2.rb b/challenge-198/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..946d1a1770 --- /dev/null +++ b/challenge-198/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,29 @@ +#! /usr/bin/ruby + +require 'test/unit' + +require 'prime' + +def primecount(l) + return Prime.each.take_while { |p| p < l }.length +end + +class TestPrimecount < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, primecount(10)) + end + + def test_ex2 + assert_equal(6, primecount(15)) + end + + def test_ex3 + assert_equal(0, primecount(1)) + end + + def test_ex4 + assert_equal(9, primecount(25)) + end + +end diff --git a/challenge-198/roger-bell-west/rust/ch-1.rs b/challenge-198/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..52114bf136 --- /dev/null +++ b/challenge-198/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,26 @@ +#! /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!(maxgap(vec![2, 5, 8, 1]), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(maxgap(vec![3]), 0); +} + +fn maxgap(l0: Vec<i32>) -> usize { + if l0.len() < 2 { + return 0; + } + let mut l = l0; + l.sort(); + let mut q = Vec::new(); + for i in l.windows(2) { + q.push(i[1] - i[0]); + } + let mq = *q.iter().max().unwrap(); + q.into_iter().filter(|i| i == &mq).count() +} diff --git a/challenge-198/roger-bell-west/rust/ch-2.rs b/challenge-198/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..0e094ceda8 --- /dev/null +++ b/challenge-198/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,60 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::cmp::min; +use std::collections::{HashSet, VecDeque}; +use std::iter::FromIterator; + +#[test] +fn test_ex1() { + assert_eq!(primecount(10), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(primecount(15), 6); +} + +#[test] +fn test_ex3() { + assert_eq!(primecount(1), 0); +} + +#[test] +fn test_ex4() { + assert_eq!(primecount(25), 9); +} + +fn genprimes(mx: usize) -> Vec<usize> { + let mut primesh: HashSet<usize> = HashSet::from_iter(2..=min(mx, 3)); + for i in (6..=mx).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 usize; + 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(); + } + let mut primes = primesh.iter().map(|i| *i).collect::<Vec<usize>>(); + primes.sort(); + primes +} + +fn primecount(l: usize) -> usize { + genprimes(l - 1).len() +} |
