From 89b75091efcc8ab7ec62564a9dbe319cb0552eba Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 10 Jan 2022 10:58:14 +0000 Subject: Solutions for challenge #147 --- challenge-147/roger-bell-west/javascript/ch-1.js | 97 +++++++++++++++++ challenge-147/roger-bell-west/javascript/ch-2.js | 41 ++++++++ challenge-147/roger-bell-west/kotlin/ch-1.kt | 69 ++++++++++++ challenge-147/roger-bell-west/kotlin/ch-2.kt | 44 ++++++++ challenge-147/roger-bell-west/lua/ch-1.lua | 101 ++++++++++++++++++ challenge-147/roger-bell-west/lua/ch-2.lua | 41 ++++++++ challenge-147/roger-bell-west/perl/ch-1.pl | 68 ++++++++++++ challenge-147/roger-bell-west/perl/ch-2.pl | 45 ++++++++ challenge-147/roger-bell-west/postscript/ch-1.ps | 128 +++++++++++++++++++++++ challenge-147/roger-bell-west/python/ch-1.py | 53 ++++++++++ challenge-147/roger-bell-west/python/ch-2.py | 41 ++++++++ challenge-147/roger-bell-west/raku/ch-1.p6 | 65 ++++++++++++ challenge-147/roger-bell-west/raku/ch-2.p6 | 41 ++++++++ challenge-147/roger-bell-west/ruby/ch-1.rb | 65 ++++++++++++ challenge-147/roger-bell-west/ruby/ch-2.rb | 41 ++++++++ challenge-147/roger-bell-west/rust/ch-1.rs | 70 +++++++++++++ challenge-147/roger-bell-west/rust/ch-2.rs | 62 +++++++++++ 17 files changed, 1072 insertions(+) create mode 100755 challenge-147/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-147/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-147/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-147/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-147/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-147/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-147/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-147/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-147/roger-bell-west/postscript/ch-1.ps create mode 100755 challenge-147/roger-bell-west/python/ch-1.py create mode 100755 challenge-147/roger-bell-west/python/ch-2.py create mode 100755 challenge-147/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-147/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-147/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-147/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-147/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-147/roger-bell-west/rust/ch-2.rs diff --git a/challenge-147/roger-bell-west/javascript/ch-1.js b/challenge-147/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..3ad09c3efd --- /dev/null +++ b/challenge-147/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,97 @@ +#! /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 tos(n) { + return n.toString(); +} + +function ltruncprimes(count) { + let out=[]; + let lt=0; + let p=genprimes(500).map(tos); + let pp=new Set(p); + for (let pc of p) { + let l=pc.length; + let c=true; + for (let i=1;i= count) { + break; + } + } + } + return out; +} + +if (deepEqual(ltruncprimes(20),[2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-147/roger-bell-west/javascript/ch-2.js b/challenge-147/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..8d55a5edde --- /dev/null +++ b/challenge-147/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,41 @@ +#! /usr/bin/node + +function pentagon(n) { + return Math.floor(n*(3*n-1)/2); +} + +function pentpair() { + let fpent=[0]; + let rpent=new Map(); + let mx=0; + let a=1; + while (true) { + while (mx < a) { + mx++; + fpent.push(pentagon(mx)); + rpent.set(fpent[mx],mx); + } + for (b = 1; b < a; b++) { + let d=fpent[a]-fpent[b]; + if (d < fpent[b]) { + break; + } + if (rpent.has(d)) { + let s=fpent[a]+fpent[b]; + while (s > fpent[mx]) { + mx++; + fpent.push(pentagon(mx)); + rpent.set(fpent[mx],mx); + } + if (rpent.has(s)) { + console.log("P(%d) + P(%d) = %d + %d = %d = P(%d)",a,b,fpent[a],fpent[b],s,rpent.get(s)); + console.log("P(%d) - P(%d) = %d - %d = %d = P(%d)",a,b,fpent[a],fpent[b],d,rpent.get(d)); + throw ''; + } + } + } + a++; + } +} + +pentpair(); diff --git a/challenge-147/roger-bell-west/kotlin/ch-1.kt b/challenge-147/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..a4675871d0 --- /dev/null +++ b/challenge-147/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,69 @@ +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 ltruncprimes(count: Int): ArrayList { + var out=ArrayList() + var lt=0 + val p=genprimes(500).map {it.toString()} + var pp=mutableSetOf() + for (i in p) { + pp.add(i) + } + for (pc in p) { + val l=pc.length-1 + var c=true + for (i in 1..l) { + if (!pp.contains(pc.slice(i..l))) { + c=false + break + } + } + if (c) { + out.add(pc.toInt()) + lt += 1 + if (lt >= count) { + break + } + } + } + return out +} + +fun main() { + if (ltruncprimes(20) == listOf(2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197)) { + println("Pass") + } else { + println("FAIL") + } +} diff --git a/challenge-147/roger-bell-west/kotlin/ch-2.kt b/challenge-147/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..e42ec3f622 --- /dev/null +++ b/challenge-147/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,44 @@ +import kotlin.system.exitProcess + +fun pentagon(n: Int): Int { + return n*(3*n-1)/2 +} + +fun pentpair() { + var fpent=ArrayList() + fpent.add(0) + var rpent=mutableMapOf() + var mx=0 + var a=1 + while (true) { + while (mx < a) { + mx += 1 + fpent.add(pentagon(mx)) + rpent.put(fpent[mx],mx) + } + for (b in 1..a-1) { + val d=fpent[a]-fpent[b] + if (d < fpent[b]) { + break + } + if (rpent.contains(d)) { + val s=fpent[a]+fpent[b] + while (s > fpent[mx]) { + mx += 1 + fpent.add(pentagon(mx)) + rpent.put(fpent[mx],mx) + } + if (rpent.contains(s)) { + println("P(%d) + P(%d) = %d + %d = %d = P(%d)".format(a,b,fpent[a],fpent[b],s,rpent[s])) + println("P(%d) - P(%d) = %d - %d = %d = P(%d)".format(a,b,fpent[a],fpent[b],d,rpent[d])) + exitProcess(0) + } + } + } + a += 1 + } +} + +fun main() { + pentpair() +} diff --git a/challenge-147/roger-bell-west/lua/ch-1.lua b/challenge-147/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..c2e65f23b0 --- /dev/null +++ b/challenge-147/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,101 @@ +#! /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 ltruncprimes(count) + out={} + lt=0 + p={} + pp={} + for i,v in ipairs(genprimes(500)) do + s=tostring(v) + table.insert(p,s) + pp[s]=true + end + for j,pc in ipairs(p) do + l=#pc + c=true + for i = 2,l do + if pp[string.sub(pc,i)] == nil then + c=false + break + end + end + if c then + table.insert(out,tonumber(pc)) + lt = lt + 1 + if lt >= count then + break + end + end + end + return out +end + +if recursive_compare(ltruncprimes(20),{2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-147/roger-bell-west/lua/ch-2.lua b/challenge-147/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c46defd3d4 --- /dev/null +++ b/challenge-147/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,41 @@ +#! /usr/bin/lua + +function pentagon(n) + return math.floor(n*(3*n-1)/2) +end + +function pentpair() + fpent={} + rpent={} + mx=0 + a=1 + while 1 do + while mx < a do + mx = mx + 1 + table.insert(fpent,pentagon(mx)) + rpent[fpent[mx]]=mx + end + for b = 1,a do + d=fpent[a]-fpent[b] + if d < fpent[b] then + break + end + if rpent[d] ~= nil then + s=fpent[a]+fpent[b] + while s > fpent[mx] do + mx = mx + 1 + table.insert(fpent,pentagon(mx)) + rpent[fpent[mx]]=mx + end + if rpent[s] ~= nil then + print(string.format("P(%d) + P(%d) = %d + %d = %d = P(%d)",a,b,fpent[a],fpent[b],s,rpent[s])) + print(string.format("P(%d) - P(%d) = %d - %d = %d = P(%d)",a,b,fpent[a],fpent[b],d,rpent[d])) + os.exit(0) + end + end + end + a = a + 1 + end +end + +pentpair() diff --git a/challenge-147/roger-bell-west/perl/ch-1.pl b/challenge-147/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..d3be91e4ff --- /dev/null +++ b/challenge-147/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,68 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +is_deeply(ltruncprimes(20),[2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197],'example 1'); + +sub ltruncprimes { + my $count=shift; + my @out; + my $lt=0; + my @p=@{genprimes(500)}; + my %p=map {$_ => 1} @p; + foreach my $pc (@p) { + my $l=length($pc); + my $c=1; + foreach my $i (1..$l-1) { + unless (exists $p{substr($pc,$i,$l+1-$i)}) { + $c=0; + last; + } + } + if ($c) { + push @out,$pc; + $lt++; + if ($lt >= $count) { + last; + } + } + } + return \@out; +} + +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; +} diff --git a/challenge-147/roger-bell-west/perl/ch-2.pl b/challenge-147/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..5a9c30c441 --- /dev/null +++ b/challenge-147/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,45 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +pentpair(); + +sub pentpair { + my @fpent=(0); + my %rpent; + my $mx=0; + my $a=1; + while (1) { + while ($mx < $a) { + $mx++; + push @fpent,pentagon($mx); + $rpent{$fpent[$mx]}=$mx; + } + foreach my $b (1..$a-1) { + my $d=$fpent[$a]-$fpent[$b]; + if ($d < $fpent[$b]) { + last; + } + if (exists $rpent{$d}) { + my $s=$fpent[$a]+$fpent[$b]; + while ($s > $fpent[$mx]) { + $mx++; + push @fpent,pentagon($mx); + $rpent{$fpent[$mx]}=$mx; + } + if (exists $rpent{$s}) { + print "P($a) + P($b) = $fpent[$a] + $fpent[$b] = $s = P($rpent{$s})\n"; + print "P($a) - P($b) = $fpent[$a] - $fpent[$b] = $d = P($rpent{$d})\n"; + exit 0; + } + } + } + $a++; + } +} + +sub pentagon { + my $n=shift; + return $n*(3*$n-1)/2; +} diff --git a/challenge-147/roger-bell-west/postscript/ch-1.ps b/challenge-147/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..ceede35ba4 --- /dev/null +++ b/challenge-147/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,128 @@ +%!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 + +/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 + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/i2s { + dup log cvi 1 add string cvs +} 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 bubblesort +} bind def + +/ltruncprimes { + /co exch def + /out 0 array def + /i 0 def + 500 genprimes dup length dup + /p exch array def + /pp exch dict def + { + i2s dup + p exch i exch put + pp exch true put + /i i 1 add def + } forall + p { + dup length /l exch def + /pc exch def + /c true def + 1 1 l 1 sub { + pc exch dup l exch sub + getinterval + pp exch known not { + /c false def + exit + } if + } for + c { + /out out pc cvi apush def + /co co 1 sub def + co 0 le { + exit + } if + } if + } forall + out +} bind def + +20 ltruncprimes [ 2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197 ] aeq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-147/roger-bell-west/python/ch-1.py b/challenge-147/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..18f827e01c --- /dev/null +++ b/challenge-147/roger-bell-west/python/ch-1.py @@ -0,0 +1,53 @@ +#! /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 ltruncprimes(count): + out=[] + lt=0 + p=[str(i) for i in genprimes(500)] + pp=set(p) + for pc in p: + l=len(pc) + c=True + for i in range(1,l): + if not pc[i:l] in pp: + c=False + break + if c: + out.append(int(pc)) + lt += 1 + if lt >= count: + break + return out + +class TestLtruncprimes(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(ltruncprimes(20),[2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197],'example 1') + +unittest.main() diff --git a/challenge-147/roger-bell-west/python/ch-2.py b/challenge-147/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..83f5c48bba --- /dev/null +++ b/challenge-147/roger-bell-west/python/ch-2.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 + +def pentagon(n): + return int(n*(3*n-1)/2) + +def pentpair(): + fpent=[0] + rpent=dict() + mx=0 + a=1 + while 1: + while mx < a: + mx += 1 + fpent.append(pentagon(mx)) + rpent[fpent[mx]]=mx + for b in range(1,a): + d=fpent[a]-fpent[b] + if d < fpent[b]: + break + if d in rpent: + s=fpent[a]+fpent[b] + while s > fpent[mx]: + mx += 1 + fpent.append(pentagon(mx)) + rpent[fpent[mx]]=mx + if s in rpent: + print("P({:d}) + P({:d}) = {:d} + {:d} = {:d} = P({:d})".format( + a, b, + fpent[a], fpent[b], + s, + rpent[s])) + print("P({:d}) - P({:d}) = {:d} - {:d} = {:d} = P({:d})".format( + a, b, + fpent[a], fpent[b], + d, + rpent[d])) + quit() + a += 1 + + +pentpair() diff --git a/challenge-147/roger-bell-west/raku/ch-1.p6 b/challenge-147/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..c78769532b --- /dev/null +++ b/challenge-147/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,65 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is-deeply(ltruncprimes(20),[2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197],'example 1'); + +sub ltruncprimes($count) { + my @out; + my $lt=0; + my @p=genprimes(500); + my %p=@p.SetHash; + for @p -> $pc { + my $l=chars($pc); + my $c=True; + for 1..$l-1 -> $i { + unless (%p{substr($pc,$i,$l+1-$i)}:exists) { + $c=False; + last; + } + } + if ($c) { + push @out,$pc; + $lt++; + if ($lt >= $count) { + last; + } + } + } + return @out; +} + +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; +} diff --git a/challenge-147/roger-bell-west/raku/ch-2.p6 b/challenge-147/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..0f784c1018 --- /dev/null +++ b/challenge-147/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,41 @@ +#! /usr/bin/perl6 + +pentpair(); + +sub pentpair { + my @fpent=[0]; + my %rpent; + my $mx=0; + my $a=1; + while (1) { + while ($mx < $a) { + $mx++; + push @fpent,pentagon($mx); + %rpent{@fpent[$mx]}=$mx; + } + for 1..$a-1 -> $b { + my $d=@fpent[$a]-@fpent[$b]; + if ($d < @fpent[$b]) { + last; + } + if (%rpent{$d}:exists) { + my $s=@fpent[$a]+@fpent[$b]; + while ($s > @fpent[$mx]) { + $mx++; + push @fpent,pentagon($mx); + %rpent{@fpent[$mx]}=$mx; + } + if (%rpent{$s}:exists) { + print "P($a) + P($b) = @fpent[$a] + @fpent[$b] = $s = P(%rpent{$s})\n"; + print "P($a) - P($b) = @fpent[$a] - @fpent[$b] = $d = P(%rpent{$d})\n"; + exit 0; + } + } + } + $a++; + } +} + +sub pentagon($n) { + return $n*(3*$n-1)/2; +} diff --git a/challenge-147/roger-bell-west/ruby/ch-1.rb b/challenge-147/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..747f06b761 --- /dev/null +++ b/challenge-147/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,65 @@ +#! /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 ltruncprimes(count) + out=[] + lt=0 + p=genprimes(500).map{|i| i.to_s} + pp=Set.new(p) + p.each do |pc| + l=pc.length()-1 + c=true + 1.upto(l) do |i| + if !pp.include?(pc[i..l]) then + c=false + break + end + end + if c then + out.push(pc.to_i) + lt += 1 + if lt >= count then + break + end + end + end + return out +end + +require 'test/unit' + +class TestLtruncprime < Test::Unit::TestCase + + def test_ex1 + assert_equal([2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197],ltruncprimes(20)) + end + +end diff --git a/challenge-147/roger-bell-west/ruby/ch-2.rb b/challenge-147/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..0c79ae306e --- /dev/null +++ b/challenge-147/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,41 @@ +#! /usr/bin/ruby + +def pentagon(n) + return (n*(3*n-1)/2).to_i +end + +def pentpair() + fpent=[0] + rpent=Hash.new + mx=0 + a=1 + while 1 do + while mx < a do + mx += 1 + fpent.push(pentagon(mx)) + rpent[fpent[mx]]=mx + end + 1.upto(a-1) do |b| + d=fpent[a]-fpent[b] + if d < fpent[b] then + break + end + if rpent.has_key?(d) then + s=fpent[a]+fpent[b] + while s > fpent[mx] do + mx += 1 + fpent.push(pentagon(mx)) + rpent[fpent[mx]]=mx + end + if rpent.has_key?(s) then + print("P(#{a}) + P(#{b}) = #{fpent[a]} + #{fpent[b]} = #{s} = P(#{rpent[s]})\n") + print("P(#{a}) - P(#{b}) = #{fpent[a]} - #{fpent[b]} = #{d} = P(#{rpent[d]})\n") + exit(0) + end + end + end + a += 1 + end +end + +pentpair() diff --git a/challenge-147/roger-bell-west/rust/ch-1.rs b/challenge-147/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..5dc5d1d6d3 --- /dev/null +++ b/challenge-147/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,70 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::{HashSet, VecDeque, BTreeSet}; +use std::iter::FromIterator; + +#[test] +fn test_ex1() { + assert_eq!( + ltruncprimes(20), + vec![ + 2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, + 167, 173, 197 + ] + ); +} + +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 ltruncprimes(count: usize) -> Vec { + let mut out: Vec = Vec::new(); + let mut lt = 0; + let p = + genprimes(500).iter().map(|i| i.to_string()).collect::>(); + let pp: HashSet = HashSet::from_iter(p.clone()); + for pc in p { + let l = pc.len() - 1; + let mut c = true; + for i in 1..=l { + if !pp.contains(&pc[i..=l]) { + c = false; + break; + } + } + if c { + out.push(pc.parse::().unwrap()); + lt += 1; + if lt >= count { + break; + } + } + } + out +} diff --git a/challenge-147/roger-bell-west/rust/ch-2.rs b/challenge-147/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..afce14516a --- /dev/null +++ b/challenge-147/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,62 @@ +#! /bin/sh +//usr/bin/env rustc $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::HashMap; + +fn pentagon(n: usize) -> usize { + n * (3 * n - 1) / 2 +} + +fn pentpair() { + let mut fpent: Vec = vec![0]; + let mut rpent: HashMap = HashMap::new(); + let mut mx = 0; + let mut a = 1; + loop { + while mx < a { + mx += 1; + fpent.push(pentagon(mx)); + rpent.insert(fpent[mx], mx); + } + for b in 1..=a - 1 { + let d = fpent[a] - fpent[b]; + if d < fpent[b] { + break; + } + if rpent.contains_key(&d) { + let s = fpent[a] + fpent[b]; + while s > fpent[mx] { + mx += 1; + fpent.push(pentagon(mx)); + rpent.insert(fpent[mx], mx); + } + if rpent.contains_key(&s) { + println!( + "P({}) + P({}) = {} + {} = {} = P({})", + a, + b, + fpent[a], + fpent[b], + s, + rpent.get(&s).unwrap() + ); + println!( + "P({}) - P({}) = {} - {} = {} = P({})", + a, + b, + fpent[a], + fpent[b], + d, + rpent.get(&d).unwrap() + ); + std::process::exit(0); + } + } + } + a += 1; + } +} + +fn main() { + pentpair(); +} -- cgit From a5295c7d01239392baa29a49a085d00fe165b7bd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 11:58:30 +0100 Subject: Task 1 done --- challenge-147/luca-ferrari/raku/ch-1.p6 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 challenge-147/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-147/luca-ferrari/raku/ch-1.p6 b/challenge-147/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..53eb20c0c0 --- /dev/null +++ b/challenge-147/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!raku + +sub MAIN( Int $limit = 20 ) { + + my @primes; + + for 10 .. Inf -> $current { + next if $current ~~ / 0 /; + next if ! $current.is-prime; + my @values.push: $current.comb[ $_ .. * - 1 ].join.Int for 0 ..^ $current.Str.chars; + @primes.push: $current if @values.grep( *.is-prime ).elems == @values.elems; + last if @primes.elems >= $limit; + + } + + @primes.join( "\n" ).say; +} -- cgit From f9c8c04e36aaedacaeafc77e1876fc23aa2f9712 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 11:58:34 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-1.raku | 19 +++++++++++++++++++ challenge-147/mark-anderson/raku/ch-2.raku | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenge-147/mark-anderson/raku/ch-1.raku create mode 100644 challenge-147/mark-anderson/raku/ch-2.raku diff --git a/challenge-147/mark-anderson/raku/ch-1.raku b/challenge-147/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..5780420a53 --- /dev/null +++ b/challenge-147/mark-anderson/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku + +say (1, 2, 3, { $_ + 2 } ... *).grep(&tp).head(20); + +multi tp(\n where *.index: 0) +{ + return False; +} + +multi tp(\n where * < 10) +{ + return n.is-prime +} + +multi tp(\n) +{ + return False unless n.is-prime; + samewith(n.substr: 1); +} diff --git a/challenge-147/mark-anderson/raku/ch-2.raku b/challenge-147/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..d7581ebace --- /dev/null +++ b/challenge-147/mark-anderson/raku/ch-2.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +# Through trial and error I came up with the ~2200 figure. + +my @p = reverse (1..2200).map(&pentagonal); + +for @p X @p +{ + if is-pentagonal(.head + .tail) and is-pentagonal(abs(.head - .tail)) + { + say .head ~ ", " ~ .tail; + last; + } +} + +sub pentagonal(\n) +{ + n * (3 * n - 1) / 2 +} + +sub is-pentagonal(\n) +{ + return (((sqrt(24 * n + 1)) + 1) / 6).narrow ~~ UInt; +} -- cgit From fbfebf58be7a8afcc48a3ec4157f69e7d5bb8c9b Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 12:01:27 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-147/mark-anderson/raku/ch-2.raku b/challenge-147/mark-anderson/raku/ch-2.raku index d7581ebace..eaadb8c978 100644 --- a/challenge-147/mark-anderson/raku/ch-2.raku +++ b/challenge-147/mark-anderson/raku/ch-2.raku @@ -20,5 +20,5 @@ sub pentagonal(\n) sub is-pentagonal(\n) { - return (((sqrt(24 * n + 1)) + 1) / 6).narrow ~~ UInt; + (((sqrt(24 * n + 1)) + 1) / 6).narrow ~~ UInt; } -- cgit From 8580bd80aa9245695b20c075a18dfac2061f7579 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 10 Jan 2022 12:04:26 +0000 Subject: Fixes to ch2 (premature optimisation) --- challenge-147/roger-bell-west/javascript/ch-2.js | 3 --- challenge-147/roger-bell-west/kotlin/ch-2.kt | 3 --- challenge-147/roger-bell-west/lua/ch-2.lua | 3 --- challenge-147/roger-bell-west/perl/ch-2.pl | 3 --- challenge-147/roger-bell-west/python/ch-2.py | 2 -- challenge-147/roger-bell-west/raku/ch-2.p6 | 3 --- challenge-147/roger-bell-west/ruby/ch-2.rb | 3 --- challenge-147/roger-bell-west/rust/ch-2.rs | 3 --- 8 files changed, 23 deletions(-) diff --git a/challenge-147/roger-bell-west/javascript/ch-2.js b/challenge-147/roger-bell-west/javascript/ch-2.js index 8d55a5edde..2cbfbc98a8 100755 --- a/challenge-147/roger-bell-west/javascript/ch-2.js +++ b/challenge-147/roger-bell-west/javascript/ch-2.js @@ -17,9 +17,6 @@ function pentpair() { } for (b = 1; b < a; b++) { let d=fpent[a]-fpent[b]; - if (d < fpent[b]) { - break; - } if (rpent.has(d)) { let s=fpent[a]+fpent[b]; while (s > fpent[mx]) { diff --git a/challenge-147/roger-bell-west/kotlin/ch-2.kt b/challenge-147/roger-bell-west/kotlin/ch-2.kt index e42ec3f622..165ab5ce2d 100644 --- a/challenge-147/roger-bell-west/kotlin/ch-2.kt +++ b/challenge-147/roger-bell-west/kotlin/ch-2.kt @@ -18,9 +18,6 @@ fun pentpair() { } for (b in 1..a-1) { val d=fpent[a]-fpent[b] - if (d < fpent[b]) { - break - } if (rpent.contains(d)) { val s=fpent[a]+fpent[b] while (s > fpent[mx]) { diff --git a/challenge-147/roger-bell-west/lua/ch-2.lua b/challenge-147/roger-bell-west/lua/ch-2.lua index c46defd3d4..cbe31d0ed8 100755 --- a/challenge-147/roger-bell-west/lua/ch-2.lua +++ b/challenge-147/roger-bell-west/lua/ch-2.lua @@ -17,9 +17,6 @@ function pentpair() end for b = 1,a do d=fpent[a]-fpent[b] - if d < fpent[b] then - break - end if rpent[d] ~= nil then s=fpent[a]+fpent[b] while s > fpent[mx] do diff --git a/challenge-147/roger-bell-west/perl/ch-2.pl b/challenge-147/roger-bell-west/perl/ch-2.pl index 5a9c30c441..1648a13d68 100755 --- a/challenge-147/roger-bell-west/perl/ch-2.pl +++ b/challenge-147/roger-bell-west/perl/ch-2.pl @@ -18,9 +18,6 @@ sub pentpair { } foreach my $b (1..$a-1) { my $d=$fpent[$a]-$fpent[$b]; - if ($d < $fpent[$b]) { - last; - } if (exists $rpent{$d}) { my $s=$fpent[$a]+$fpent[$b]; while ($s > $fpent[$mx]) { diff --git a/challenge-147/roger-bell-west/python/ch-2.py b/challenge-147/roger-bell-west/python/ch-2.py index 83f5c48bba..b524b7e5e5 100755 --- a/challenge-147/roger-bell-west/python/ch-2.py +++ b/challenge-147/roger-bell-west/python/ch-2.py @@ -15,8 +15,6 @@ def pentpair(): rpent[fpent[mx]]=mx for b in range(1,a): d=fpent[a]-fpent[b] - if d < fpent[b]: - break if d in rpent: s=fpent[a]+fpent[b] while s > fpent[mx]: diff --git a/challenge-147/roger-bell-west/raku/ch-2.p6 b/challenge-147/roger-bell-west/raku/ch-2.p6 index 0f784c1018..d333f3337f 100755 --- a/challenge-147/roger-bell-west/raku/ch-2.p6 +++ b/challenge-147/roger-bell-west/raku/ch-2.p6 @@ -15,9 +15,6 @@ sub pentpair { } for 1..$a-1 -> $b { my $d=@fpent[$a]-@fpent[$b]; - if ($d < @fpent[$b]) { - last; - } if (%rpent{$d}:exists) { my $s=@fpent[$a]+@fpent[$b]; while ($s > @fpent[$mx]) { diff --git a/challenge-147/roger-bell-west/ruby/ch-2.rb b/challenge-147/roger-bell-west/ruby/ch-2.rb index 0c79ae306e..3c0de3457a 100755 --- a/challenge-147/roger-bell-west/ruby/ch-2.rb +++ b/challenge-147/roger-bell-west/ruby/ch-2.rb @@ -17,9 +17,6 @@ def pentpair() end 1.upto(a-1) do |b| d=fpent[a]-fpent[b] - if d < fpent[b] then - break - end if rpent.has_key?(d) then s=fpent[a]+fpent[b] while s > fpent[mx] do diff --git a/challenge-147/roger-bell-west/rust/ch-2.rs b/challenge-147/roger-bell-west/rust/ch-2.rs index afce14516a..044722976e 100755 --- a/challenge-147/roger-bell-west/rust/ch-2.rs +++ b/challenge-147/roger-bell-west/rust/ch-2.rs @@ -20,9 +20,6 @@ fn pentpair() { } for b in 1..=a - 1 { let d = fpent[a] - fpent[b]; - if d < fpent[b] { - break; - } if rpent.contains_key(&d) { let s = fpent[a] + fpent[b]; while s > fpent[mx] { -- cgit From 1563c0f338e0ea069fa490609ef2cb0f3caab418 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 12:06:43 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-147/mark-anderson/raku/ch-2.raku b/challenge-147/mark-anderson/raku/ch-2.raku index eaadb8c978..8efa8baf9a 100644 --- a/challenge-147/mark-anderson/raku/ch-2.raku +++ b/challenge-147/mark-anderson/raku/ch-2.raku @@ -2,7 +2,7 @@ # Through trial and error I came up with the ~2200 figure. -my @p = reverse (1..2200).map(&pentagonal); +my @p = (2200...1).map(&pentagonal); for @p X @p { -- cgit From fe78c723122108e7f1efb36ed8983b022cbe7463 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 12:12:12 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-147/mark-anderson/raku/ch-2.raku b/challenge-147/mark-anderson/raku/ch-2.raku index 8efa8baf9a..d8a6da138d 100644 --- a/challenge-147/mark-anderson/raku/ch-2.raku +++ b/challenge-147/mark-anderson/raku/ch-2.raku @@ -20,5 +20,5 @@ sub pentagonal(\n) sub is-pentagonal(\n) { - (((sqrt(24 * n + 1)) + 1) / 6).narrow ~~ UInt; + ((sqrt(24 * n + 1) + 1) / 6).narrow ~~ UInt; } -- cgit From f4e1ecce21d107101cb0d53bf700ac9ac34cbaa8 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 12:22:36 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-1.raku | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-147/mark-anderson/raku/ch-1.raku b/challenge-147/mark-anderson/raku/ch-1.raku index 5780420a53..08ec5151c8 100644 --- a/challenge-147/mark-anderson/raku/ch-1.raku +++ b/challenge-147/mark-anderson/raku/ch-1.raku @@ -1,15 +1,15 @@ #!/usr/bin/env raku -say (1, 2, 3, { $_ + 2 } ... *).grep(&tp).head(20); +say (2, 3, { $_ + 2 } ... *).grep(&tp).head(20); multi tp(\n where *.index: 0) { - return False; + False; } multi tp(\n where * < 10) { - return n.is-prime + n.is-prime } multi tp(\n) -- cgit From d58d7f254facc6928e740b9c7d9418843c37c912 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 12:51:28 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-147/mark-anderson/raku/ch-2.raku b/challenge-147/mark-anderson/raku/ch-2.raku index d8a6da138d..b13641a58f 100644 --- a/challenge-147/mark-anderson/raku/ch-2.raku +++ b/challenge-147/mark-anderson/raku/ch-2.raku @@ -4,7 +4,7 @@ my @p = (2200...1).map(&pentagonal); -for @p X @p +for @p.combinations: 2 { if is-pentagonal(.head + .tail) and is-pentagonal(abs(.head - .tail)) { -- cgit From 3e46de7a2b20d350796f3a4a84f5eb8dbe0c5456 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 10 Jan 2022 13:03:59 +0000 Subject: initial 147 --- challenge-147/mark-anderson/raku/ch-1.raku | 2 +- challenge-147/mark-anderson/raku/ch-2.raku | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-147/mark-anderson/raku/ch-1.raku b/challenge-147/mark-anderson/raku/ch-1.raku index 08ec5151c8..e4dc7996fc 100644 --- a/challenge-147/mark-anderson/raku/ch-1.raku +++ b/challenge-147/mark-anderson/raku/ch-1.raku @@ -4,7 +4,7 @@ say (2, 3, { $_ + 2 } ... *).grep(&tp).head(20); multi tp(\n where *.index: 0) { - False; + False } multi tp(\n where * < 10) diff --git a/challenge-147/mark-anderson/raku/ch-2.raku b/challenge-147/mark-anderson/raku/ch-2.raku index b13641a58f..e81d6177a0 100644 --- a/challenge-147/mark-anderson/raku/ch-2.raku +++ b/challenge-147/mark-anderson/raku/ch-2.raku @@ -20,5 +20,5 @@ sub pentagonal(\n) sub is-pentagonal(\n) { - ((sqrt(24 * n + 1) + 1) / 6).narrow ~~ UInt; + ((sqrt(24 * n + 1) + 1) / 6).narrow ~~ UInt } -- cgit From afb6b053323475a1fe26d22c847611fac730a8b3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 14:06:41 +0100 Subject: Task 2 done --- challenge-147/luca-ferrari/raku/ch-2.p6 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 challenge-147/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-147/luca-ferrari/raku/ch-2.p6 b/challenge-147/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..daab2b97c4 --- /dev/null +++ b/challenge-147/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,31 @@ +#!raku + + sub MAIN( Int $limit = 3000 ) { + + my ( %pentagons, %inverse-pentagons ); + %pentagons{ $_ } = ( $_ * ( 3 * $_ - 1 ) / 2 ) for 1 .. $limit; + %inverse-pentagons{ %pentagons{ $_ } } = $_ for %pentagons.keys.sort; + + + + for %pentagons.keys.sort -> $index-left { + for %pentagons.keys.sort -> $index-right { + next if $index-left == $index-right; + + + my ( $sum, $diff ) = %pentagons{ $index-left } + %pentagons{ $index-right }, + abs( %pentagons{ $index-left } - %pentagons{ $index-right } ); + + # this is too slow, therefore I use an inverse hash! + # next if ! %pentagons.values.grep( * ~~ $sum ); + # next if ! %pentagons.values.grep( * ~~ $diff ); + next if %inverse-pentagons{ $diff }:!exists; + next if %inverse-pentagons{ $sum }:!exists; + + "P( $index-left ) + P( $index-right ) = { %pentagons{ $index-left } } + { %pentagons{ $index-right } } = $sum = P( { %inverse-pentagons{ $sum } } )".say; + "P( $index-left ) - P( $index-right ) = { %pentagons{ $index-left } } + { %pentagons{ $index-right } } = $diff = P( {%inverse-pentagons{ $diff } } )".say; + exit; + } + } + + } -- cgit From c446116e9f879b3815294b15b15143a832c2268c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 14:34:18 +0100 Subject: Task 1 done in PostgreSQL --- challenge-147/luca-ferrari/postgresql/ch-1.sql | 83 ++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 challenge-147/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-147/luca-ferrari/postgresql/ch-1.sql b/challenge-147/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..7e24f9ceb0 --- /dev/null +++ b/challenge-147/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,83 @@ +CREATE OR REPLACE FUNCTION +f_is_prime( n bigint ) +RETURNS bool +AS +$CODE$ +DECLARE + i int; +BEGIN + FOR i IN 2 .. ( n - 1 ) LOOP + IF n % i = 0 THEN + RETURN false; + END IF; + END LOOP; + + RETURN true; +END +$CODE$ +LANGUAGE plpgsql; + + +/** +testdb=> select * from f_generate_truncated_primes(); +f_generate_truncated_primes +----------------------------- +11 +13 +17 +23 +31 +37 +41 +43 +47 +53 +61 +67 +71 +73 +83 +97 +113 +131 +137 +167 +(20 rows) + +*/ +CREATE OR REPLACE FUNCTION +f_generate_truncated_primes( l int = 20 ) +RETURNS SETOF int +AS +$CODE$ +DECLARE + i int; + current bigint; + fnd int := 0; +BEGIN +<> + FOR current IN SELECT * FROM generate_series( 10, 999999 ) LOOP + CONTINUE WHEN current::text LIKE '%0%'; + + IF NOT f_is_prime( current ) THEN + CONTINUE MAIN_LOOP; + END IF; + + + FOR i IN 1 .. length( current::text ) LOOP + IF NOT f_is_prime( substring( current::text FROM i )::int ) THEN + CONTINUE MAIN_LOOP; + END IF; + END LOOP; + + fnd := fnd + 1; + RETURN NEXT current; + IF fnd >= l THEN + RETURN; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 30a0e70fd189d521add0046cb10452685ed615a0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 15:32:20 +0100 Subject: Task 2 done in PostgreSQL --- challenge-147/luca-ferrari/postgresql/ch-2.sql | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 challenge-147/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-147/luca-ferrari/postgresql/ch-2.sql b/challenge-147/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..61465a21c6 --- /dev/null +++ b/challenge-147/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,97 @@ +/* +testdb=> select * from f_pentagons_pairs(); +INFO: P(1020) + P(2167) = P(8602840) = 1560090 +INFO: P(1020) - P(2167) = P(5482660) = 7042750 +n1 | n2 | s | d | ps | pd +------+------+---------+---------+---------+--------- +1020 | 2167 | 1560090 | 7042750 | 8602840 | 5482660 +(1 row) + +Time: 7257,715 ms (00:07,258) + +*/ + +CREATE OR REPLACE FUNCTION +f_pentagon( n bigint ) +RETURNS bigint +AS +$CODE$ + SELECT ( n * ( 3 * n - 1 ) / 2 ); +$CODE$ +LANGUAGE sql +IMMUTABLE; + + +DROP TABLE IF EXISTS pentagons; +CREATE TABLE pentagons +( + n bigint + , p bigint GENERATED ALWAYS AS ( f_pentagon( n ) ) STORED +); + + + +INSERT INTO pentagons( n ) +SELECT generate_series( 1, 5000 ); + + + + +CREATE OR REPLACE FUNCTION +f_pentagons_pairs() +RETURNS TABLE ( n1 bigint, n2 bigint, s bigint, d bigint, ps bigint, pd bigint ) +AS $CODE$ +DECLARE + current_tuple pentagons%rowtype; + other_tuple pentagons%rowtype; + fnd int := 0; +BEGIN + + FOR current_tuple IN SELECT * FROM pentagons ORDER BY n LOOP + SELECT * + INTO other_tuple + FROM pentagons pp + WHERE EXISTS( + SELECT * + FROM pentagons ps + WHERE ps.p = current_tuple.p + pp.p + ) + AND EXISTS ( + SELECT * + FROM pentagons ps + WHERE ps.p = abs( current_tuple.p - pp.p ) + ); + + + IF FOUND THEN + SELECT current_tuple.n + , other_tuple.n + , current_tuple.p + , other_tuple.p + , current_tuple.p + other_tuple.p + , abs( current_tuple.p - other_tuple.p ) + , p1.n + , p2.n + INTO n1, n2, s, d, ps, pd + FROM pentagons p1, pentagons p2 + WHERE p1.p = current_tuple.p + other_tuple.p + AND p2.p = abs( current_tuple.p - other_tuple.p ); + + RAISE INFO 'P(%) + P(%) = P(%) = %', + n1, n2, ps, s; + + RAISE INFO 'P(%) - P(%) = P(%) = %', + n1, n2, pd, d; + + + fnd := fnd + 1; + RETURN NEXT; + RETURN; + END IF; + + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 94a16b7453f95b6898e3d5d28d2f25a554a0d061 Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 10 Jan 2022 15:47:18 +0000 Subject: Challenge 147 --- challenge-147/simon-proctor/raku/ch-1.raku | 27 +++++++++++++++++++++++++++ challenge-147/simon-proctor/raku/ch-2.raku | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 challenge-147/simon-proctor/raku/ch-1.raku create mode 100644 challenge-147/simon-proctor/raku/ch-2.raku diff --git a/challenge-147/simon-proctor/raku/ch-1.raku b/challenge-147/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..574ec4a48b --- /dev/null +++ b/challenge-147/simon-proctor/raku/ch-1.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku + +use experimental :cached; + +#| Display the first 20 base 10 left-truncatable primes +multi sub MAIN() { + .say for (1..*).grep( { is-left-truncatable($_) } )[^20]; +} + +multi sub MAIN("test") is hidden-from-USAGE { + use Test; + ok( is-left-truncatable( 9137 ) ); + ok( is-left-truncatable( 137 ) ); + ok( is-left-truncatable( 7 ) ); + ok( ! is-left-truncatable( 101 ) ); + ok( ! is-left-truncatable( 8 ) ); + ok( ! is-left-truncatable( 151 ) ); + done-testing; +} + +sub is-left-truncatable( UInt \v ) is pure is cached { + return False unless v.is-prime; + return False if v ~~ /0/; + return True if v.codes == 1; + + return is-left-truncatable( v.substr(1).Int ); +} diff --git a/challenge-147/simon-proctor/raku/ch-2.raku b/challenge-147/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..60b8421907 --- /dev/null +++ b/challenge-147/simon-proctor/raku/ch-2.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku + +my @pentagons = (1..*).map( { $_ * ((3 * $_)-1) / 2 }); + +my @vals = lazy gather { + for (0..5000) -> $i { + for (0..^$i) -> $j { + take (@pentagons[$i], @pentagons[$j], $i+$j); + } + } +}; + +@vals.race.grep( + -> (\a, \b, \m) { + my \check = Set(@pentagons[^m]); + ((a - b) (elem) check) && ((a + b) (elem) check); + } +).map( -> (\a, \b, \m) { ( a, b ) } ).first.say -- cgit From f192e61c5d3966500102b4f69cdf2cf9e6a2c268 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 17:24:47 +0100 Subject: Blog references --- challenge-147/luca-ferrari/blog-1.txt | 1 + challenge-147/luca-ferrari/blog-2.txt | 1 + challenge-147/luca-ferrari/blog-3.txt | 1 + challenge-147/luca-ferrari/blog-4.txt | 1 + 4 files changed, 4 insertions(+) create mode 100644 challenge-147/luca-ferrari/blog-1.txt create mode 100644 challenge-147/luca-ferrari/blog-2.txt create mode 100644 challenge-147/luca-ferrari/blog-3.txt create mode 100644 challenge-147/luca-ferrari/blog-4.txt diff --git a/challenge-147/luca-ferrari/blog-1.txt b/challenge-147/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..af17183edb --- /dev/null +++ b/challenge-147/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task1 diff --git a/challenge-147/luca-ferrari/blog-2.txt b/challenge-147/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..5e5d379514 --- /dev/null +++ b/challenge-147/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task2 diff --git a/challenge-147/luca-ferrari/blog-3.txt b/challenge-147/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..aa716b41b9 --- /dev/null +++ b/challenge-147/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task1pg diff --git a/challenge-147/luca-ferrari/blog-4.txt b/challenge-147/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..520b859582 --- /dev/null +++ b/challenge-147/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task2pg -- cgit From 960cf4eedd2934aba4b2188e813bcc96628fd5df Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 10 Jan 2022 18:46:16 +0000 Subject: - Added solutions by Roger Bell_West. --- stats/pwc-challenge-146.json | 643 ++++ stats/pwc-current.json | 650 +--- stats/pwc-language-breakdown-summary.json | 70 +- stats/pwc-language-breakdown.json | 5797 +++++++++++++++-------------- stats/pwc-leaders.json | 722 ++-- stats/pwc-summary-1-30.json | 22 +- stats/pwc-summary-121-150.json | 36 +- stats/pwc-summary-151-180.json | 48 +- stats/pwc-summary-181-210.json | 40 +- stats/pwc-summary-211-240.json | 38 +- stats/pwc-summary-241-270.json | 42 +- stats/pwc-summary-31-60.json | 38 +- stats/pwc-summary-61-90.json | 104 +- stats/pwc-summary-91-120.json | 50 +- stats/pwc-summary.json | 564 +-- 15 files changed, 4477 insertions(+), 4387 deletions(-) create mode 100644 stats/pwc-challenge-146.json diff --git a/stats/pwc-challenge-146.json b/stats/pwc-challenge-146.json new file mode 100644 index 0000000000..62df88114c --- /dev/null +++ b/stats/pwc-challenge-146.json @@ -0,0 +1,643 @@ +{ + "tooltip" : { + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
", + "followPointer" : 1 + }, + "title" : { + "text" : "The Weekly Challenge - 146" + }, + "subtitle" : { + "text" : "[Champions: 33] Last updated at 2022-01-10 18:34:32 GMT" + }, + "xAxis" : { + "type" : "category" + }, + "legend" : { + "enabled" : 0 + }, + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "series" : [ + { + "colorByPoint" : 1, + "data" : [ + { + "name" : "Abigail", + "drilldown" : "Abigail", + "y" : 4 + }, + { + "drilldown" : "Adam Russell", + "name" : "Adam Russell", + "y" : 3 + }, + { + "y" : 1, + "drilldown" : "Andrew Shitov", + "name" : "Andrew Shitov" + }, + { + "name" : "Andrezgz", + "drilldown" : "Andrezgz", + "y" : 2 + }, + { + "y" : 5, + "name" : "Arne Sommer", + "drilldown" : "Arne Sommer" + }, + { + "drilldown" : "Athanasius", + "name" : "Athanasius", + "y" : 4 + }, + { + "drilldown" : "Bruce Gray", + "name" : "Bruce Gray", + "y" : 5 + }, + { + "name" : "Colin Crain", + "drilldown" : "Colin Crain", + "y" : 4 + }, + { + "y" : 3, + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby" + }, + { + "drilldown" : "Duncan C. White", + "name" : "Duncan C. White", + "y" : 2 + }, + { + "drilldown" : "E. Choroba", + "name" : "E. Choroba", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Feng Chang", + "name" : "Feng Chang" + }, + { + "drilldown" : "Flavio Poletti", + "name" : "Flavio Poletti", + "y" : 6 + }, + { + "name" : "Ian Goodnight", + "drilldown" : "Ian Goodnight", + "y" : 2 + }, + { + "name" : "Jaldhar H. Vyas", + "drilldown" : "Jaldhar H. Vyas", + "y" : 5 + }, + { + "y" : 3, + "name" : "James Smith", + "drilldown" : "James Smith" + }, + { + "y" : 2, + "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek" + }, + { + "y" : 1, + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "y" : 5 + }, + { + "y" : 6, + "drilldown" : "Luca Ferrari", + "name" : "Luca Ferrari" + }, + { + "y" : 2, + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "y" : 4, + "name" : "Mark Senn", + "drilldown" : "Mark Senn" + }, + { + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh", + "y" : 2 + }, + { + "name" : "Mohammad S Anwar", + "drilldown" : "Mohammad S Anwar", + "y" : 2 + }, + { + "name" : "Niels van Dijke", + "drilldown" : "Niels van Dijke", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Pete Houston", + "name" : "Pete Houston" + }, + { + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", + "y" : 3 + }, + { + "y" : 2, + "name" : "Robert DiCicco", + "drilldown" : "Robert DiCicco" + }, + { + "y" : 5, + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West" + }, + { + "y" : 3, + "drilldown" : "Simon Green", + "name" : "Simon Green" + }, + { + "name" : "Simon Proctor", + "drilldown" : "Simon Proctor", + "y" : 2 + }, + { + "y" : 4, + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke" + }, + { + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan", + "y" : 3 + } + ], + "name" : "The Weekly Challenge - 146" + } + ], + "chart" : { + "type" : "column" + }, + "drilldown" : { + "series" : [ + { + "name" : "Abigail", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Abigail" + }, + { + "name" : "Adam Russell", + "id" : "Adam Russell", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "id" : "Andrew Shitov", + "data" : [ + [ + "Raku", + 1 + ] + ], + "name" : "Andrew Shitov" + }, + { + "id" : "Andrezgz", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Andrezgz" + }, + { + "name" : "Arne Sommer", + "id" : "Arne Sommer", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Athanasius", + "id" : "Athanasius", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ] + }, + { + "name" : "Bruce Gray", + "id" : "Bruce Gray", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "id" : "Colin Crain", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Colin Crain" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Duncan C. White", + "name" : "Duncan C. White" + }, + { + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "E. Choroba" + }, + { + "name" : "Feng Chang", + "data" : [ + [ + "Raku", + 2 +