From a3826633cf3113eb7f609287c24d969ba4785a44 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 20 Dec 2021 11:54:07 +0000 Subject: Solutions for challenge #144 --- challenge-144/roger-bell-west/kotlin/ch-1.kt | 43 +++++++++++ challenge-144/roger-bell-west/kotlin/ch-2.kt | 42 +++++++++++ challenge-144/roger-bell-west/lua/ch-1.lua | 77 +++++++++++++++++++ challenge-144/roger-bell-west/lua/ch-2.lua | 74 ++++++++++++++++++ challenge-144/roger-bell-west/perl/ch-1.pl | 44 +++++++++++ challenge-144/roger-bell-west/perl/ch-2.pl | 32 ++++++++ challenge-144/roger-bell-west/postscript/ch-1.ps | 95 ++++++++++++++++++++++++ challenge-144/roger-bell-west/postscript/ch-2.ps | 63 ++++++++++++++++ challenge-144/roger-bell-west/python/ch-1.py | 35 +++++++++ challenge-144/roger-bell-west/python/ch-2.py | 31 ++++++++ challenge-144/roger-bell-west/raku/ch-1.p6 | 41 ++++++++++ challenge-144/roger-bell-west/raku/ch-2.p6 | 29 ++++++++ challenge-144/roger-bell-west/ruby/ch-1.rb | 43 +++++++++++ challenge-144/roger-bell-west/ruby/ch-2.rb | 39 ++++++++++ challenge-144/roger-bell-west/rust/ch-1.rs | 49 ++++++++++++ challenge-144/roger-bell-west/rust/ch-2.rs | 37 +++++++++ 16 files changed, 774 insertions(+) create mode 100644 challenge-144/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-144/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-144/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-144/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-144/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-144/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-144/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-144/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-144/roger-bell-west/python/ch-1.py create mode 100755 challenge-144/roger-bell-west/python/ch-2.py create mode 100755 challenge-144/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-144/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-144/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-144/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-144/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-144/roger-bell-west/rust/ch-2.rs diff --git a/challenge-144/roger-bell-west/kotlin/ch-1.kt b/challenge-144/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..507b113ba5 --- /dev/null +++ b/challenge-144/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,43 @@ +fun semiprime(mx: Int): ArrayList { + val mxx: Int=mx/2 + var primesh=mutableSetOf() + for (i in 2..mxx) { + primesh.add(i) + } + var p: Int=2 + while (p*p <= mxx) { + if (primesh.contains(p)) { + for (i in p*p..mxx step p) { + primesh.remove(i) + } + } + if (p==2) { + p -= 1 + } + p += 2 + } + var primes=ArrayList(primesh.distinct()) + primes.sort() + var semiprimesh=mutableSetOf() + for (i in 0..primes.size-1) { + for (j in i..primes.size-1) { + val t=primes[i]*primes[j] + if (t < mx) { + semiprimesh.add(t) + } else { + break + } + } + } + var semiprimes=ArrayList(semiprimesh.distinct()) + semiprimes.sort() + return semiprimes +} + +fun main() { + if (semiprime(100) == listOf(4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95)) { + println("Pass") + } else { + println("FAIL") + } +} diff --git a/challenge-144/roger-bell-west/kotlin/ch-2.kt b/challenge-144/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..fc93ea4a7e --- /dev/null +++ b/challenge-144/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,42 @@ +fun ulam(u: Int, v: Int, ccount: Int): ArrayList { + var ulams=arrayListOf(u,v) + var sieve=ArrayList() + var uc=v + while (ulams.size < ccount) { + for (i in sieve.size..uc+ulams[ulams.size-2]) { + sieve.add(0) + } + for (i in 0..ulams.size-2) { + sieve[uc + ulams[i] - 1] += 1 + } + for (i in uc..sieve.size-1) { + if (sieve[i] == 1) { + uc=i+1 + ulams.add(uc) + break + } + } + } + return ulams +} + +fun main() { + if (ulam(1,2,10) == listOf(1, 2, 3, 4, 6 , 8, 11, 13, 16, 18)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (ulam(2,3,10) == listOf(2, 3, 5, 7, 8, 9, 13, 14, 18, 19)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (ulam(2,5,10) == listOf(2, 5, 7, 9, 11, 12, 13, 15, 19, 23)) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-144/roger-bell-west/lua/ch-1.lua b/challenge-144/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..bdb74a1e02 --- /dev/null +++ b/challenge-144/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,77 @@ +#! /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 semiprime(mx) + mxx = math.tointeger(mx/2) + primesh = {} + for i = 2, mxx do + primesh[i] = true + end + p = 2 + while p * p <= mxx do + if primesh[p] ~= nil then + for i = p*p,mxx,p do + primesh[i] = nil + end + end + if p == 2 then + p = p - 1 + end + p = p + 2 + end + primes = {} + for k,v in pairs(primesh) do + table.insert(primes,k) + end + semiprimesh = {} + for i = 1,#primes-1 do + for j = i,#primes do + t = primes[i]*primes[j] + if t < mx then + semiprimesh[t]=true + else + break + end + end + end + semiprimes = {} + for k,v in pairs(semiprimesh) do + table.insert(semiprimes,k) + end + table.sort(semiprimes) + return semiprimes +end + +if recursive_compare(semiprime(100),{4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") \ No newline at end of file diff --git a/challenge-144/roger-bell-west/lua/ch-2.lua b/challenge-144/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..efda3fdf26 --- /dev/null +++ b/challenge-144/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,74 @@ +#! /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 ulam(u,v,count) + ulams={u,v} + sieve={} + uc=v + while #ulams < count do + for i = #sieve+1,uc + ulams[#ulams-1] do + table.insert(sieve,0) + end + for i = 1,#ulams-1 do + j=uc+ulams[i] + sieve[j] = sieve[j] + 1 + end + for i = uc+1,#sieve do + if sieve[i] == 1 then + uc = i + table.insert(ulams,uc) + break + end + end + end + return ulams +end + +if recursive_compare(ulam(1,2,10),{1, 2, 3, 4, 6, 8, 11, 13, 16, 18}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(ulam(2,3,10),{2, 3, 5, 7, 8, 9, 13, 14, 18, 19}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(ulam(2,5,10),{2, 5, 7, 9, 11, 12, 13, 15, 19, 23}) then + io.write("Pass") +else + io.write("FAIL") +end + +print("") \ No newline at end of file diff --git a/challenge-144/roger-bell-west/perl/ch-1.pl b/challenge-144/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..ed9ff0ba3d --- /dev/null +++ b/challenge-144/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,44 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +is_deeply(semiprime(100),[4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95],'example 1'); + +sub semiprime { + my $mx=shift; + my $mxx=int($mx/2); + my @primes; + { + my %primesh=map {$_ => 1} (2..$mxx); + my $p=2; + while ($p*$p <= $mxx) { + if ($primesh{$p}) { + my $i=$p*$p; + while ($i <= $mxx) { + delete $primesh{$i}; + $i += $p; + } + } + if ($p==2) { + $p--; + } + $p+=2; + } + @primes=sort {$a <=> $b} keys %primesh; + } + my %semiprimesh; + foreach my $i (0..$#primes) { + foreach my $j ($i..$#primes) { + my $t=$primes[$i]*$primes[$j]; + if ($t <= $mx) { + $semiprimesh{$t}=1; + } else { + next; + } + } + } + return [sort {$a <=> $b} keys %semiprimesh]; +} diff --git a/challenge-144/roger-bell-west/perl/ch-2.pl b/challenge-144/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..7f9a8d2de3 --- /dev/null +++ b/challenge-144/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +is_deeply(ulam(1,2,10),[1, 2, 3, 4, 6 , 8, 11, 13, 16, 18],'example 1'); +is_deeply(ulam(2,3,10),[2, 3, 5, 7, 8, 9, 13, 14, 18, 19],'example 2'); +is_deeply(ulam(2,5,10),[2, 5, 7, 9, 11, 12, 13, 15, 19, 23],'example 3'); + +sub ulam { + my ($u,$v,$count)=@_; + my @ulams=($u,$v); + my @sieve; + my $uc=$v; + while (scalar @ulams < $count) { + my $ss=$uc+$ulams[-2]; + push @sieve,(0) x ($ss-scalar @sieve); + foreach my $i (0..$#ulams-1) { + $sieve[$uc + $ulams[$i] - 1]++; + } + foreach my $i ($uc..$#sieve) { + if ($sieve[$i]==1) { + $uc=$i+1; + push @ulams,$uc; + last; + } + } + } + return \@ulams; +} diff --git a/challenge-144/roger-bell-west/postscript/ch-1.ps b/challenge-144/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..4c14a618b4 --- /dev/null +++ b/challenge-144/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,95 @@ +%!PS + +/bubblesort { + mark exch aload pop counttomark /idx + exch store + { + 0 1 idx 1 sub { + pop 2 copy gt { + exch + } if idx 1 roll + } for + idx 1 roll /idx idx 1 sub store + idx 0 eq { + exit + } if + } loop +] +} store + +/aeq { + 2 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 + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/semiprime { + /mx exch def + /mxx mx 2 idiv def + /primesh mxx dict def + 2 1 mxx { + primesh exch 1 put + } for + /p 2 def + { + p dup mul mxx le not { + exit + } if + primesh p known { + p dup mul p mxx { + primesh exch undef + } for + } if + p 2 eq { + /p p 1 sub def + } if + /p p 2 add def + } loop + /primes 0 array def + primesh { + pop + /primes exch primes exch apush def + } forall + /primes primes bubblesort def + /semiprimesh mx dict def + 0 1 primes length 1 sub { + /i exch def + i 1 primes length 1 sub { + primes exch get primes i get mul /t exch def + t mx le { + semiprimesh t 1 put + } { + exit + } ifelse + } for + } for + /semiprimes 0 array def + semiprimesh { + pop + /semiprimes exch semiprimes exch apush def + } forall + /semiprimes semiprimes bubblesort def + semiprimes +} bind def + +100 semiprime [ 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95 ] +aeq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-144/roger-bell-west/postscript/ch-2.ps b/challenge-144/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..cc962342c0 --- /dev/null +++ b/challenge-144/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,63 @@ +%!PS + +/aeq { + 2 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 + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/ulam { + /ccount exch def + /v exch def + /u exch def + /ulams [ u v ] def + /sieve 0 array def + /uc v def + { + ulams length ccount ge { + exit + } if + uc ulams ulams length 2 sub get add sieve length sub { + /sieve sieve 0 apush def + } repeat + 0 1 ulams length 2 sub { + ulams exch get uc add 1 sub /t exch def + sieve t sieve t get 1 add put + } for + uc 1 sieve length 1 sub { + /i exch def + sieve i get 1 eq { + /uc i 1 add def + /ulams ulams uc apush def + exit + } if + } for + } loop + ulams +} bind def + +1 2 10 ulam [ 1 2 3 4 6 8 11 13 16 18 ] +aeq { (Pass) } { (FAIL) } ifelse print ( ) print +2 3 10 ulam [ 2 3 5 7 8 9 13 14 18 19 ] +aeq { (Pass) } { (FAIL) } ifelse print ( ) print +2 5 10 ulam [ 2 5 7 9 11 12 13 15 19 23 ] +aeq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-144/roger-bell-west/python/ch-1.py b/challenge-144/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..34a6e6d424 --- /dev/null +++ b/challenge-144/roger-bell-west/python/ch-1.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +import unittest + +def semiprime(mx): + mxx=int(mx/2) + primesh=set(range(2,mxx+1)) + p=2 + while p*p <= mxx: + if p in primesh: + for i in range(p*p,mx+1,p): + primesh.discard(i) + if p==2: + p -= 1 + p += 2 + primes=list(primesh) + primes.sort() + semiprimesh=set() + for i in range(len(primes)): + for j in range(i,len(primes)): + t=primes[i]*primes[j] + if t <= mx: + semiprimesh.add(t) + else: + break + semiprimes=list(semiprimesh) + semiprimes.sort() + return semiprimes + +class TestSemiprime(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(semiprime(100),[4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95],'example 1') + +unittest.main() diff --git a/challenge-144/roger-bell-west/python/ch-2.py b/challenge-144/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..b569b0f8cf --- /dev/null +++ b/challenge-144/roger-bell-west/python/ch-2.py @@ -0,0 +1,31 @@ +#! /usr/bin/python3 + +import unittest + +def ulam(u,v,count): + ulams=[u,v] + sieve=[] + uc=v + while len(ulams) < count: + sieve.extend([0] * (uc+ulams[-2]-len(sieve))) + for i in range(len(ulams)-1): + sieve[uc + ulams[i] - 1] += 1 + for i in range(uc,len(sieve)): + if sieve[i]==1: + uc=i+1 + ulams.append(uc) + break + return ulams + +class TestUlam(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(ulam(1,2,10),[1, 2, 3, 4, 6 , 8, 11, 13, 16, 18],'example 1') + + def test_ex2(self): + self.assertEqual(ulam(2,3,10),[2, 3, 5, 7, 8, 9, 13, 14, 18, 19],'example 2') + + def test_ex3(self): + self.assertEqual(ulam(2,5,10),[2, 5, 7, 9, 11, 12, 13, 15, 19, 23],'example 3') + +unittest.main() diff --git a/challenge-144/roger-bell-west/raku/ch-1.p6 b/challenge-144/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..62b4f98280 --- /dev/null +++ b/challenge-144/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,41 @@ +#! /usr/bin/perl6 + +use Test; +plan 1; + +is-deeply(semiprime(100),(4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95),'example 1'); + +sub semiprime($mx) { + my @primes; + { + my $mxx=floor($mx/2); + my $primesh=(2..$mxx).SetHash; + my $p=2; + while ($p*$p <= $mxx) { + if ($primesh{$p}:exists) { + my $i=$p*$p; + while ($i <= $mxx) { + $primesh{$i}:delete; + $i += $p; + } + } + if ($p==2) { + $p--; + } + $p+=2; + } + @primes=$primesh.keys.sort; + } + my $semiprimesh=SetHash(); + for 0..@primes.end -> $i { + for $i..@primes.end -> $j { + my $t=@primes[$i]*@primes[$j]; + if ($t <= $mx) { + $semiprimesh{$t}++; + } else { + next; + } + } + } + return $semiprimesh.keys.map({$_+0}).sort; +} diff --git a/challenge-144/roger-bell-west/raku/ch-2.p6 b/challenge-144/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..4dace477d0 --- /dev/null +++ b/challenge-144/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,29 @@ +#! /usr/bin/perl6 + +use Test; +plan 3; + +is-deeply(ulam(1,2,10),[1, 2, 3, 4, 6 , 8, 11, 13, 16, 18],'example 1'); +is-deeply(ulam(2,3,10),[2, 3, 5, 7, 8, 9, 13, 14, 18, 19],'example 2'); +is-deeply(ulam(2,5,10),[2, 5, 7, 9, 11, 12, 13, 15, 19, 23],'example 3'); + +sub ulam($u,$v,$count) { + my @ulams=[$u,$v]; + my @sieve; + my $uc=$v; + while (@ulams.elems < $count) { + my $ss=$uc+@ulams[*-2]; + @sieve.append(0 xx ($ss-@sieve.elems)); + for (0..@ulams.end-1) -> $i { + @sieve[$uc + @ulams[$i] - 1]++; + } + for ($uc..@sieve.end) -> $i { + if (@sieve[$i]==1) { + $uc=$i+1; + push @ulams,$uc; + last; + } + } + } + return @ulams; +} diff --git a/challenge-144/roger-bell-west/ruby/ch-1.rb b/challenge-144/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..77d5650ccd --- /dev/null +++ b/challenge-144/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,43 @@ +#! /usr/bin/ruby + +require 'set' + +def semiprime(mx) + mxx=(mx/2).to_i + primesh=Set.new(2..mxx) + p=2 + while p*p <= mxx do + if primesh.include?(p) + (p*p..mxx).step(p) do |i| + primesh.delete?(i) + end + end + if p==2 then + p -= 1 + end + p += 2 + end + primes=primesh.each.sort + semiprimesh=Set.new + 0.upto(primes.length()-1) do |i| + i.upto(primes.length()-1) do |j| + t=primes[i]*primes[j] + if t <= mx then + semiprimesh.add(t) + else + break + end + end + end + return semiprimesh.each.sort +end + +require 'test/unit' + +class TestSemiprime < Test::Unit::TestCase + + def test_ex1 + assert_equal([4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95],semiprime(100)) + end + +end diff --git a/challenge-144/roger-bell-west/ruby/ch-2.rb b/challenge-144/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..9ec1e9a7d9 --- /dev/null +++ b/challenge-144/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,39 @@ +#! /usr/bin/ruby + +def ulam(u,v,count) + ulams=[u,v] + sieve=[] + uc=v + while ulams.length() < count do + sieve.concat(Array.new(uc+ulams[-2]-sieve.length,0)) + 0.upto(ulams.length-2) do |i| + sieve[uc + ulams[i] - 1] += 1 + end + uc.upto(sieve.length-1) do |i| + if sieve[i]==1 then + uc=i+1 + ulams.push(uc) + break + end + end + end + return ulams +end + +require 'test/unit' + +class TestUlam < Test::Unit::TestCase + + def test_ex1 + assert_equal([1, 2, 3, 4, 6 , 8, 11, 13, 16, 18],ulam(1,2,10)) + end + + def test_ex2 + assert_equal([2, 3, 5, 7, 8, 9, 13, 14, 18, 19],ulam(2,3,10)) + end + + def test_ex3 + assert_equal([2, 5, 7, 9, 11, 12, 13, 15, 19, 23],ulam(2,5,10)) + end + +end diff --git a/challenge-144/roger-bell-west/rust/ch-1.rs b/challenge-144/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..d34828e30b --- /dev/null +++ b/challenge-144/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,49 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::HashSet; +use std::iter::FromIterator; + +#[test] +fn test_ex1() { + assert_eq!( + semiprime(100), + vec![ + 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, + 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95 + ] + ); +} + +fn semiprime(mx: u32) -> Vec { + let mxx = mx / 2; + let mut primesh: HashSet = HashSet::from_iter(2..=mxx); + let mut p = 2; + while p * p <= mxx { + if primesh.contains(&p) { + for i in (p * p..=mxx).step_by(p as usize) { + primesh.remove(&i); + } + } + if p == 2 { + p -= 1; + } + p += 2; + } + let mut primes = primesh.iter().map(|i| *i).collect::>(); + primes.sort(); + let mut semiprimesh: HashSet = HashSet::new(); + for i in 0..primes.len() { + for j in i..primes.len() { + let t = primes[i] * primes[j]; + if t < mx { + semiprimesh.insert(t); + } else { + break; + } + } + } + let mut semiprimes = semiprimesh.iter().map(|i| *i).collect::>(); + semiprimes.sort(); + semiprimes +} diff --git a/challenge-144/roger-bell-west/rust/ch-2.rs b/challenge-144/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..609a177407 --- /dev/null +++ b/challenge-144/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,37 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(ulam(1, 2, 10), vec![1, 2, 3, 4, 6, 8, 11, 13, 16, 18]); +} + +#[test] +fn test_ex2() { + assert_eq!(ulam(2, 3, 10), vec![2, 3, 5, 7, 8, 9, 13, 14, 18, 19]); +} + +#[test] +fn test_ex3() { + assert_eq!(ulam(2, 5, 10), vec![2, 5, 7, 9, 11, 12, 13, 15, 19, 23]); +} + +fn ulam(u: usize, v: usize, count: usize) -> Vec { + let mut ulams = Vec::from([u, v]); + let mut sieve = vec![]; + let mut uc = v; + while ulams.len() < count { + sieve.extend(vec![0; uc + ulams[ulams.len() - 2] - sieve.len()]); + for i in 0..=ulams.len() - 2 { + sieve[uc + ulams[i] - 1] += 1; + } + for i in uc..sieve.len() { + if sieve[i] == 1 { + uc = i + 1; + ulams.push(uc); + break; + } + } + } + ulams +} -- cgit From 6970d0fd460308be9ca07b9bb2870b74ed894863 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 20 Dec 2021 14:16:42 +0100 Subject: Task 1 done --- challenge-144/luca-ferrari/raku/ch-1.p6 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 challenge-144/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-144/luca-ferrari/raku/ch-1.p6 b/challenge-144/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..70462d1747 --- /dev/null +++ b/challenge-144/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#!raku + +sub MAIN( Int $limit where { $limit > 1 } = 100 ) { + my @semi-primes; + + for 1 .. $limit -> $current-number { + @semi-primes.push: $current-number if ( 1 .. $current-number ).grep( { $_.is-prime + && $current-number %% $_ + && ( $current-number / $_ ).Int.is-prime } ) + .elems > 0; + # for 1 .. $current-number -> $current-divisor { + # next if ! $current-divisor.is-prime; + # next if $current-number !%% $current-divisor; + # next if ! ( $current-number / $current-divisor ).Int.is-prime; + # @semi-primes.push: $current-number; + # last; + # } + } + + @semi-primes.join( ',' ).say; +} -- cgit From 15594821cbb4b8929a5d13f40a8abd53408cfcbb Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 20 Dec 2021 15:55:03 +0100 Subject: Task 2 done --- challenge-144/luca-ferrari/raku/ch-2.p6 | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 challenge-144/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-144/luca-ferrari/raku/ch-2.p6 b/challenge-144/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..68ed1b9ebe --- /dev/null +++ b/challenge-144/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,40 @@ +#!raku + +# Sums all the couple of numbers +# in only direction, skipping the duplicated +# values +sub do-sum( @array ) { + my %sums; + + for 0 .. @array.end -> $index-left { + for $index-left ^.. @array.end -> $index-right { + next if $index-left == $index-right; + + %sums{ @array[ $index-left ] + @array[ $index-right ] }++; + } + } + + + my @found; + @found.push: $_.Int if %sums{ $_ } == 1 for %sums.keys; + @found; + +} + + +sub MAIN( Int $u where { $u > 0 }, + Int $v where { $v > 0 }, + Int $limit = 10 ) { + + + + my @ulam = $u, $v, $u + $v; + + while @ulam.elems < $limit { + @ulam.push: do-sum( @ulam ).grep( { $_ > @ulam[ * - 1 ] } ).min; + } + + + @ulam.join( ',' ).say; + +} -- cgit From cb6b797384f6e8891dc8f4c71685fcb7a1a4e8a6 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 20 Dec 2021 16:13:46 +0100 Subject: Task 1 PostgreSQL done --- challenge-144/luca-ferrari/postgresql/ch-1.sql | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 challenge-144/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-144/luca-ferrari/postgresql/ch-1.sql b/challenge-144/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..c8d7264e7c --- /dev/null +++ b/challenge-144/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,71 @@ +/* + * Check if the current number is prime + */ +CREATE OR REPLACE FUNCTION f_is_prime( n int ) +RETURNS bool +AS $CODE$ +DECLARE + divisor int; +BEGIN + FOR divisor IN 2 .. ( n - 1 ) LOOP + IF mod( n, divisor ) = 0 THEN + RETURN false; + END IF; + END LOOP; + + RETURN TRUE; +END +$CODE$ +LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION f_semiprime_factors( n int ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current_number int; +BEGIN + FOR current_number IN 1 .. n LOOP + IF f_is_prime( current_number ) AND mod( n, current_number ) = 0 AND f_is_prime( ( n / current_number ) ) THEN + RETURN NEXT current_number; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; + + + +CREATE OR REPLACE FUNCTION f_is_semiprime( n int ) +RETURNS bool +AS $CODE$ + SELECT CASE count( * ) + WHEN 0 THEN false + ELSE true + END + FROM f_semiprime_factors( n ) s; +$CODE$ +LANGUAGE sql; + + + +CREATE OR REPLACE FUNCTION f_find_semiprimes( lim int default 100 ) +RETURNS SETOF int +AS $CODE$ +DECLARE + n int; +BEGIN + n := 1; + + WHILE n < lim LOOP + IF f_is_semiprime( n ) THEN + RETURN NEXT n; + END IF; + n := n + 1; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 93982aec8b78fbed06557f3fe4f6cac449c7e793 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 20 Dec 2021 16:37:23 +0100 Subject: Task 2 in PostgreSQL done --- challenge-144/luca-ferrari/postgresql/ch-2.sql | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 challenge-144/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-144/luca-ferrari/postgresql/ch-2.sql b/challenge-144/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..0fa2b07521 --- /dev/null +++ b/challenge-144/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,52 @@ +CREATE OR REPLACE FUNCTION f_ulam_do_sum( ulam int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + left_index int; + right_index int; +BEGIN + + FOR left_index IN 1 .. array_length( ulam, 1 ) LOOP + FOR right_index IN left_index + 1 .. array_length( ulam, 1 ) LOOP + RETURN NEXT ulam[ left_index ] + ulam[ right_index ]; + END LOOP; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION f_ulam( u int, v int, lim int default 10 ) +RETURNS int[] +AS $CODE$ +DECLARE + ulam int[]; + next_value int; +BEGIN +-- PERFORM array_append( ulam, u ); +-- PERFORM array_append( ulam, v ); +-- PERFORM array_append( ulam, u + v ); + + + ulam := ulam || u || v || u + v; + + WHILE array_length( ulam, 1 ) < lim LOOP + + SELECT vv + INTO next_value + FROM f_ulam_do_sum( ulam ) AS sums( vv ) + WHERE vv > ulam[ array_length( ulam, 1 ) ] + GROUP BY 1 + HAVING COUNT( * ) = 1 + ORDER BY vv + LIMIT 1; + + ulam := ulam || next_value; + END LOOP; + + RETURN ulam; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 82300f89acda50a186fa56a9e737cb3edf9a1030 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 20 Dec 2021 16:41:30 +0000 Subject: Add Perl and Python solutions to challenge 144 --- challenge-001/paulo-custodio/untabify.pl | 2 +- challenge-144/paulo-custodio/perl/ch-1.pl | 27 ++++++++++ challenge-144/paulo-custodio/perl/ch-2.pl | 79 +++++++++++++++++++++++++++++ challenge-144/paulo-custodio/python/ch-1.py | 39 ++++++++++++++ challenge-144/paulo-custodio/python/ch-2.py | 66 ++++++++++++++++++++++++ challenge-144/paulo-custodio/t/test-1.yaml | 5 ++ challenge-144/paulo-custodio/t/test-2.yaml | 15 ++++++ 7 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 challenge-144/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-144/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-144/paulo-custodio/python/ch-1.py create mode 100644 challenge-144/paulo-custodio/python/ch-2.py create mode 100644 challenge-144/paulo-custodio/t/test-1.yaml create mode 100644 challenge-144/paulo-custodio/t/test-2.yaml diff --git a/challenge-001/paulo-custodio/untabify.pl b/challenge-001/paulo-custodio/untabify.pl index dfdd1f1143..b52da1ff08 100644 --- a/challenge-001/paulo-custodio/untabify.pl +++ b/challenge-001/paulo-custodio/untabify.pl @@ -10,7 +10,7 @@ for my $dir () { my $iter = path($dir)->iterator({recurse=>1}); while (defined(my $path = $iter->())) { next unless $path->is_file; - next unless -T $path; + next unless -T $path; next if $path =~ /~$/; # temp files my $ext = ""; $path->basename =~ /(\.\w+)$/ and $ext = $1; next if $ext eq "" || $ext =~ /\.(exe|o|obj|ali|ads)$/; # binaries diff --git a/challenge-144/paulo-custodio/perl/ch-1.pl b/challenge-144/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..0b3014813b --- /dev/null +++ b/challenge-144/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +# Challenge 144 +# +# TASK #1 > Semiprime +# Submitted by: Mohammad S Anwar +# Write a script to generate all Semiprime number <= 100. +# +# For more information about Semiprime, please checkout the wikipedia page. +# +# +# In mathematics, a semiprime is a natural number that is the product of +# exactly two prime numbers. The two primes in the product may equal each +# other, so the semiprimes include the squares of prime numbers. +# +# +# Example +# 10 is Semiprime as 10 = 2 x 5 +# 15 is Semiprime as 15 = 3 x 5 + +use Modern::Perl; +use ntheory 'semi_primes'; + +use constant MAX_NUM => 100; + +my $n = shift||MAX_NUM; +say join(", ", @{semi_primes($n)}); diff --git a/challenge-144/paulo-custodio/perl/ch-2.pl b/challenge-144/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..180042f82c --- /dev/null +++ b/challenge-144/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl + +# Challenge 144 +# +# TASK #2 > Ulam Sequence +# Submitted by: Mohammad S Anwar +# You are given two positive numbers, $u and $v. +# +# Write a script to generate Ulam Sequence having at least 10 Ulam numbers +# where $u and $v are the first 2 Ulam numbers. +# +# For more information about Ulam Sequence, please checkout the website. +# +# The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and +# U2 = 2. Then for n > 2, Un is defined to be the smallest integer that is the +# sum of two distinct earlier terms in exactly one way and larger than all +# earlier terms. + +use Modern::Perl; +use Math::Combinatorics; +use List::Util 'sum'; + +use constant NUM_TERMS => 10; + +sub next_ulam { + my(@terms) = @_; + + # get all combinations of 2 items from previous terms + my @combin = combine(2, @terms); + + # compute sum for all combinations + my %sums; + for (@combin) { + my @items = @$_; + my $n = sum(@items); + $sums{$n} ||= []; + push @{$sums{$n}}, \@items; + } + + # choose smallest sum that has only one possible combination and is larger + # than previous ones + for my $n (sort {$a<=>$b} keys %sums) { + next unless $n > $terms[-1]; # item not larger than previous + my @items = @{$sums{$n}}; + next if @items>1; # more than one sum + return $n; + } + + die "next item not foundm terms=@terms"; +} + +sub ulam_iter { + my($u, $v) = @_; + my @terms; + return sub { + if (@terms==0) { + push @terms, $u; + return $u; + } + elsif (@terms==1) { + push @terms, $v; + return $v; + } + else { + my $n = next_ulam(@terms); + push @terms, $n; + return $n; + } + }; +} + +my($u, $v) = @ARGV; +my $it = ulam_iter($u, $v); +my @seq; +for (1..NUM_TERMS) { + push @seq, $it->(); +} + +say join(", ", @seq); diff --git a/challenge-144/paulo-custodio/python/ch-1.py b/challenge-144/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6598deecc0 --- /dev/null +++ b/challenge-144/paulo-custodio/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +# Challenge 144 +# +# TASK #1 > Semiprime +# Submitted by: Mohammad S Anwar +# Write a script to generate all Semiprime number <= 100. +# +# For more information about Semiprime, please checkout the wikipedia page. +# +# +# In mathematics, a semiprime is a natural number that is the product of +# exactly two prime numbers. The two primes in the product may equal each +# other, so the semiprimes include the squares of prime numbers. +# +# +# Example +# 10 is Semiprime as 10 = 2 x 5 +# 15 is Semiprime as 15 = 3 x 5 + +import sys +from primePy import primes + +MAX_NUM = 100 + +# list of primes up to max/smallest_prime +prime_nums = primes.upto(MAX_NUM/2) + +# set of all semiprimes +semiprime_set = set() +for i in range(len(prime_nums)): + for j in range(len(prime_nums)): + n = prime_nums[i]*prime_nums[j] + semiprime_set.add(n) + +# sort and filter <= MAX_NUM +semiprimes = sorted(filter(lambda x:x <= MAX_NUM, list(semiprime_set))) + +print(*semiprimes, sep=", ") diff --git a/challenge-144/paulo-custodio/python/ch-2.py b/challenge-144/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..42397b1f50 --- /dev/null +++ b/challenge-144/paulo-custodio/python/ch-2.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 + +# Challenge 144 +# +# TASK #2 > Ulam Sequence +# Submitted by: Mohammad S Anwar +# You are given two positive numbers, $u and $v. +# +# Write a script to generate Ulam Sequence having at least 10 Ulam numbers +# where $u and $v are the first 2 Ulam numbers. +# +# For more information about Ulam Sequence, please checkout the website. +# +# The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and +# U2 = 2. Then for n > 2, Un is defined to be the smallest integer that is the +# sum of two distinct earlier terms in exactly one way and larger than all +# earlier terms. + +import sys +from itertools import combinations + +NUM_TERMS = 10 + +def ulam_seq(u, v): + def next_ulam(terms): + # get all combinations of 2 items from previous terms + # compute sum for all combinations + sums = {} + for items in combinations(terms, 2): + n = sum(items) + if n not in sums: + sums[n] = [] + sums[n].append(items) + + # choose smallest sum that has only one possible combination and + # is larger than previous ones + for n in sorted(sums): + if n > terms[-1]: + if len(sums[n])==1: + return n + print("no solution found for ", *terms) + sys.exit(1) + + terms = [] + + # first two terms + terms.append(u) + yield u + + terms.append(v) + yield v + + # other terms + while True: + n = next_ulam(terms) + terms.append(n) + yield n + +u = int(sys.argv[1]) +v = int(sys.argv[2]) +seq = [] +for n in ulam_seq(u, v): + seq.append(n) + if len(seq) >= NUM_TERMS: + break +print(*seq, sep=", ") diff --git a/challenge-144/paulo-custodio/t/test-1.yaml b/challenge-144/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..a78b6e85cd --- /dev/null +++ b/challenge-144/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 100 + input: + output: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95 diff --git a/challenge-144/paulo-custodio/t/test-2.yaml b/challenge-144/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..dc065304a3 --- /dev/null +++ b/challenge-144/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 + input: + output: 1, 2, 3, 4, 6, 8, 11, 13, 16, 18 +- setup: + cleanup: + args: 2 3 + input: + output: 2, 3, 5, 7, 8, 9, 13, 14, 18, 19 +- setup: + cleanup: + args: 2 5 + input: + output: 2, 5, 7, 9, 11, 12, 13, 15, 19, 23 -- cgit From 91561362a910cc8a9fe939a04225df51d4d1afa7 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 20 Dec 2021 18:09:11 +0100 Subject: Add solutions to 144: Semiprime & Ulam Sequence by E. Choroba --- challenge-144/e-choroba/perl/ch-1.pl | 60 ++++++++++++++++++++++++++++++++++++ challenge-144/e-choroba/perl/ch-2.pl | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100755 challenge-144/e-choroba/perl/ch-1.pl create mode 100755 challenge-144/e-choroba/perl/ch-2.pl diff --git a/challenge-144/e-choroba/perl/ch-1.pl b/challenge-144/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..f09b0d7aa4 --- /dev/null +++ b/challenge-144/e-choroba/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use warnings; +use strict; + +{ package Semiprime; + use Moo; + has _primes => (is => 'rw', default => sub { [2, 3] }); + + sub is_semiprime { + my ($self, $n) = @_; + $self->_extend_primes while $n > $self->_primes->[-1]; + for my $p (@{ $self->_primes }) { + return $self->_is_prime($n / $p) ? 1 : 0 + if 0 == $n % $p; + } + return 0 + } + + sub _extend_primes { + my ($self) = @_; + my $candidate = $self->_primes->[-1]; + + CANDIDATE: + while ($candidate += 2) { + for my $p (@{ $self->_primes }) { + next CANDIDATE if 0 == $candidate % $p; + } + last CANDIDATE + } + push @{ $self->_primes }, $candidate; + } + + sub _is_prime { + my ($self, $n) = @_; + return 0 if $n <= 1; + + for my $p (@{ $self->_primes }) { + return $p == $n if 0 == $n % $p; + } + return + } +} + +use Test2::V0; +plan 6; + +my $s = 'Semiprime'->new; + +is $s->is_semiprime(10), 1, 'Example 1'; +is $s->is_semiprime(15), 1, 'Example 2'; + +is $s->is_semiprime(1), 0, 'One'; +is $s->is_semiprime(23), 0, 'Prime'; +is $s->is_semiprime(30), 0, 'Three primes'; + +my @under100 = grep $s->is_semiprime($_), 0 .. 100; +is \@under100, [4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, + 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, + 94, 95], + 'List <= 100'; # From Wikipedia. diff --git a/challenge-144/e-choroba/perl/ch-2.pl b/challenge-144/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..cda3452b7f --- /dev/null +++ b/challenge-144/e-choroba/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl +use warnings; +use strict; + +{ package Ulam::Sequence; + use Moo; + + has u => (is => 'ro', requried => 1); + has v => (is => 'ro', required => 1); + has sequence => (is => 'lazy', builder => 1); + + sub extend { + my ($self, $l) = @_; + $l //= 1 + @{ $self->sequence }; + + my $n = $self->sequence->[-1]; + while ($l != @{ $self->sequence }) { + while (++$n) { + + my %seen; + @seen{ @{ $self->sequence } } = (1) x @{ $self->sequence }; + + for my $s (@{ $self->sequence }) { + ++$seen{$n - $s} unless $n - $s == $s; + } + my $twice = grep 2 == $_, values %seen; + if ($twice && $twice < 3) { + push @{ $self->sequence }, $n; + last + } + } + } + } + + sub _build_sequence { + my ($self) = @_; + return [ $self->u, $self->v ] + } +} + +use Test2::V0; +plan 4; + +my $us1 = 'Ulam::Sequence'->new(u => 1, v => 2); +$us1->extend(10); +is $us1->sequence, [1, 2, 3, 4, 6, 8, 11, 13, 16, 18], 'Example 1'; + +my $us2 = 'Ulam::Sequence'->new(u => 2, v => 3); +$us2->extend(10); +is $us2->sequence, [2, 3, 5, 7, 8, 9, 13, 14, 18, 19], 'Example 2'; + +my $us3 = 'Ulam::Sequence'->new(u => 2, v => 5); +$us3->extend(10); +is $us3->sequence, [2, 5, 7, 9, 11, 12, 13, 15, 19, 23], 'Example 3'; + +my $us4 = 'Ulam::Sequence'->new(u => 1, v => 3); +$us4->extend(10); +is $us4->sequence, [1, 3, 4, 5, 6, 8, 10, 12, 17, 21], '(1,3)-U'; -- cgit From 236b5d9862f583749e7d27f0272e1dd6230d1c51 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 20 Dec 2021 18:17:56 +0100 Subject: Blog references --- challenge-144/luca-ferrari/blog-1.txt | 1 + challenge-144/luca-ferrari/blog-2.txt | 1 + challenge-144/luca-ferrari/blog-3.txt | 1 + challenge-144/luca-ferrari/blog-4.txt | 1 + 4 files changed, 4 insertions(+) create mode 100644 challenge-144/luca-ferrari/blog-1.txt create mode 100644 challenge-144/luca-ferrari/blog-2.txt create mode 100644 challenge-144/luca-ferrari/blog-3.txt create mode 100644 challenge-144/luca-ferrari/blog-4.txt diff --git a/challenge-144/luca-ferrari/blog-1.txt b/challenge-144/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..439af3081a --- /dev/null +++ b/challenge-144/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/20/PerlWeeklyChallenge144.html#task1 diff --git a/challenge-144/luca-ferrari/blog-2.txt b/challenge-144/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..80ac3eb036 --- /dev/null +++ b/challenge-144/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/20/PerlWeeklyChallenge144.html#task2 diff --git a/challenge-144/luca-ferrari/blog-3.txt b/challenge-144/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..409920a331 --- /dev/null +++ b/challenge-144/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/20/PerlWeeklyChallenge144.html#task1pg diff --git a/challenge-144/luca-ferrari/blog-4.txt b/challenge-144/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..409920a331 --- /dev/null +++ b/challenge-144/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/20/PerlWeeklyChallenge144.html#task1pg -- cgit From cfdee22178f1ded9d184ca5b2b0123104f3040d5 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 21 Dec 2021 15:10:14 +0000 Subject: - Added solutions by Roger Bell_West. --- stats/pwc-challenge-143.json | 525 ++++++++ stats/pwc-current.json | 518 +------ stats/pwc-language-breakdown-summary.json | 68 +- stats/pwc-language-breakdown.json | 2079 +++++++++++++++-------------- stats/pwc-leaders.json | 390 +++--- stats/pwc-summary-1-30.json | 44 +- stats/pwc-summary-121-150.json | 30 +- stats/pwc-summary-151-180.json | 106 +- stats/pwc-summary-181-210.json | 114 +- stats/pwc-summary-211-240.json | 42 +- stats/pwc-summary-241-270.json | 48 +- stats/pwc-summary-31-60.json | 40 +- stats/pwc-summary-61-90.json | 50 +- stats/pwc-summary-91-120.json | 36 +- stats/pwc-summary.json | 550 ++++---- 15 files changed, 2365 insertions(+), 2275 deletions(-) create mode 100644 stats/pwc-challenge-143.json diff --git a/stats/pwc-challenge-143.json b/stats/pwc-challenge-143.json new file mode 100644 index 0000000000..8f7cf67249 --- /dev/null +++ b/stats/pwc-challenge-143.json @@ -0,0 +1,525 @@ +{ + "chart" : { + "type" : "column" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "drilldown" : { + "series" : [ + { + "name" : "Abigail", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Abigail" + }, + { + "name" : "Adam Russell", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Adam Russell" + }, + { + "id" : "Alexander Pankoff", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Alexander Pankoff" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer", + "name" : "Arne Sommer" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 1 + ] + ], + "id" : "Athanasius", + "name" : "Athanasius" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Colin Crain", + "name" : "Colin Crain" + }, + { + "name" : "Dave Jacoby", + "id" : "Dave Jacoby", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "David Santiago", + "name" : "David Santiago" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Duncan C. White", + "name" : "Duncan C. White" + }, + { + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "E. Choroba" + }, + { + "name" : "Flavio Poletti", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Flavio Poletti" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "James Smith", + "name" : "James Smith" + }, + { + "name" : "Jan Krnavek", + "id" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 1 + ] + ] + }, + { + "id" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Laurent Rosenfeld" + }, + { + "id" : "Luca Ferrari", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 3 + ] + ], + "name" : "Luca Ferrari" + }, + { + "name" : "Mark Senn", + "id" : "Mark Senn", + "data" : [ + [ + "Raku", + 1 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Matthew Neleigh", + "data" : [ + [ + "Perl", + 1 + ] + ], + "id" : "Matthew Neleigh" + }, + { + "name" : "Mohammad S Anwar", + "id" : "Mohammad S Anwar", + "data" : [ + [ + "Perl", + 1 + ] + ] + }, + { + "id" : "Olivier Delouya", + "data" : [ + [ + "Perl", + 1 + ] + ], + "name" : "Olivier Delouya" + }, + { + "id" : "Paulo Custodio", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Paulo Custodio" + }, + { + "id" : "Pete Houston", + "data" : [ + [ + "Perl", + 1 + ] + ], + "name" : "Pete Houston" + }, + { + "name" : "Peter Campbell Smith", + "id" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Robert DiCicco", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Robert DiCicco" + }, + { + "name" : "Roger Bell_West", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Roger Bell_West" + }, + { + "id" : "Simon Green", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Simon Green" + }, + { + "name" : "Ulrich Rieke", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ] + ], + "id" : "Ulrich Rieke" + }, + { + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + } + ] + }, + "tooltip" : { + "headerFormat" : "{series.name}
", + "pointFormat" : "{point.name}: {point.y:f}
", + "followPointer" : 1 + }, + "title" : { + "text" : "The Weekly Challenge - 143" + }, + "legend" : { + "enabled" : 0 + }, + "xAxis" : { + "type" : "category" + }, + "series" : [ + { + "name" : "The Weekly Challenge - 143", + "colorByPoint" : 1, + "data" : [ + { + "name" : "Abigail", + "y" : 2, + "drilldown" : "Abigail" + }, + { + "name" : "Adam Russell", + "y" : 4, + "drilldown" : "Adam Russell" + }, + { + "name" : "Alexander Pankoff", + "y" : 2, + "drilldown" : "Alexander Pankoff" + }, + { + "name" : "Arne Sommer", + "drilldown" : "Arne Sommer", + "y" : 5 + }, + { + "drilldown" : "Athanasius", + "y" : 3, + "name" : "Athanasius" + }, + { + "y" : 2, + "drilldown" : "Colin Crain", + "name" : "Colin Crain" + }, + { + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby", + "y" : 3 + }, + { + "drilldown" : "David Santiago", + "y" : 2, + "name" : "David Santiago" + }, + { + "drilldown" : "Duncan C. White", + "y" : 2, + "name" : "Duncan C. White" + }, + { + "drilldown" : "E. Choroba", + "y" : 2, + "name" : "E. Choroba" + }, + { + "drilldown" : "Flavio Poletti", + "y" : 6, + "name" : "Flavio Poletti" + }, + { + "name" : "James Smith", + "drilldown" : "James Smith", + "y" : 3 + }, + { + "drilldown" : "Jan Krnavek", + "y" : 1, + "name" : "Jan Krnavek" + }, + { + "y" : 5, + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Luca Ferrari", + "y" : 5, + "name" : "Luca Ferrari" + }, + { + "name" : "Mark Senn", + "drilldown" : "Mark Senn", + "y" : 2 + }, + { + "y" : 1, + "drilldown" : "Matthew Neleigh", + "name" : "Matthew Neleigh" + }, + { + "y" : 1, + "drilldown" : "Mohammad S Anwar", + "name" : "Mohammad S Anwar" + }, + { + "y" : 1, + "drilldown" : "Olivier Delouya", + "name" : "Olivier Delouya" + }, + { + "name" : "Paulo Custodio", + "y" : 2, + "drilldown" : "Paulo Custodio" + }, + { + "y" : 1, + "drilldown" : "Pete Houston", + "name" : "Pete Houston" + }, + { + "y" : 3, + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "y" : 2, + "drilldown" : "Robert DiCicco", + "name" : "Robert DiCicco" + }, + { + "drilldown" : "Roger Bell_West", + "y" : 5, + "name" : "Roger Bell_West" + }, + { + "drilldown" : "Simon Green", + "y" : 3, + "name" : "Simon Green" + }, + { + "drilldown" : "Ulrich Rieke", + "y" : 2, + "name" : "Ulrich Rieke" + }, + { + "y" : 3, + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan" + } + ] + } + ], + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, + "subtitle" : { + "text" : "[Champions: 27] Last updated at 2021-12-21 15:01:27 GMT" + } +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 6f8442be38..017c680b30 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,126 +1,6 @@ { "drilldown" : { "series" : [ - { - "data" : [ - [ - "Perl", - 2 - ] - ], - "name" : "Abigail", - "id" : "Abigail" - }, - { - "name" : "Adam Russell", - "id" : "Adam Russell", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Blog", - 2 - ] - ] - }, - { - "data" : [ - [ - "Perl", - 2 - ] - ], - "name" : "Alexander Pankoff", - "id" : "Alexander Pankoff" - }, - { - "data" : [ - [ - "Perl", - 2 - ], - [ - "Raku", - 2 - ], - [ - "Blog", - 1 - ] - ], - "id" : "Arne Sommer", - "name" : "Arne Sommer" - }, - { - "name" : "Athanasius", - "id" : "Athanasius", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Raku", - 1 - ] - ] - }, - { - "name" : "Colin Crain", - "id" : "Colin Crain", - "data" : [ - [ - "Perl", - 2 - ] - ] - }, - { - "name" : "Dave Jacoby", - "id" : "Dave Jacoby", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Blog", - 1 - ] - ] - }, - { - "name" : "David Santiago", - "id" : "David Santiago", - "data" : [ - [ - "Raku", - 2 - ] - ] - }, - { - "data" : [ - [ - "Perl", - 2 - ] - ], - "id" : "Duncan C. White", - "name" : "Duncan C. White" - }, - { - "name" : "E. Choroba", - "id" : "E. Choroba", - "data" : [ - [ - "Perl", - 2 - ] - ] - }, { "data" : [ [ @@ -130,396 +10,58 @@ [ "Raku", 2 - ], - [ - "Blog", - 2 - ] - ], - "id" : "Flavio Poletti", - "name" : "Flavio Poletti" - }, - { - "data" : [ - [ - "Perl", - 2 - ], - [ - "Blog", - 1 - ] - ], - "name" : "James Smith", - "id" : "James Smith" - }, - { - "name" : "Jan Krnavek", - "id" : "Jan Krnavek", - "data" : [ - [ - "Raku", - 1 - ] - ] - }, - { - "id" : "Laurent Rosenfeld", - "name" : "Laurent Rosenfeld", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Raku", - 2 - ], - [ - "Blog", - 1 - ] - ] - }, - { - "name" : "Luca Ferrari", - "id" : "Luca Ferrari", - "data" : [ - [ - "Raku", - 2 - ], - [ - "Blog", - 3 - ] - ] - }, - { - "name" : "Mark Senn", - "id" : "Mark Senn", - "data" : [ - [ - "Raku", - 1 - ], - [ - "Blog", - 1 - ] - ] - }, - { - "name" : "Matthew Neleigh", - "id" : "Matthew Neleigh", - "data" : [ - [ - "Perl", - 1 - ] - ] - }, - { - "name" : "Mohammad S Anwar", - "id" : "Mohammad S Anwar", -