diff options
Diffstat (limited to 'challenge-147')
| -rwxr-xr-x | challenge-147/roger-bell-west/javascript/ch-1.js | 97 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/javascript/ch-2.js | 38 | ||||
| -rw-r--r-- | challenge-147/roger-bell-west/kotlin/ch-1.kt | 69 | ||||
| -rw-r--r-- | challenge-147/roger-bell-west/kotlin/ch-2.kt | 41 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/lua/ch-1.lua | 101 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/lua/ch-2.lua | 38 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/perl/ch-1.pl | 68 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-147/roger-bell-west/postscript/ch-1.ps | 128 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/python/ch-1.py | 53 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/python/ch-2.py | 39 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/raku/ch-1.p6 | 65 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/raku/ch-2.p6 | 38 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/ruby/ch-1.rb | 65 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/ruby/ch-2.rb | 38 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/rust/ch-1.rs | 70 | ||||
| -rwxr-xr-x | challenge-147/roger-bell-west/rust/ch-2.rs | 59 |
17 files changed, 1049 insertions, 0 deletions
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<l;i++) { + if (!pp.has(pc.slice(i))) { + c=false; + break; + } + } + if (c) { + out.push(parseInt(pc)); + lt++; + if (lt >= 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..2cbfbc98a8 --- /dev/null +++ b/challenge-147/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,38 @@ +#! /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 (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<Int> { + var primesh=mutableSetOf<Int>() + 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<Int> { + var out=ArrayList<Int>() + var lt=0 + val p=genprimes(500).map {it.toString()} + var pp=mutableSetOf<String>() + 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..165ab5ce2d --- /dev/null +++ b/challenge-147/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,41 @@ +import kotlin.system.exitProcess + +fun pentagon(n: Int): Int { + return n*(3*n-1)/2 +} + +fun pentpair() { + var fpent=ArrayList<Int>() + fpent.add(0) + var rpent=mutableMapOf<Int,Int>() + 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 (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..cbe31d0ed8 --- /dev/null +++ b/challenge-147/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,38 @@ +#! /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 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..1648a13d68 --- /dev/null +++ b/challenge-147/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,42 @@ +#! /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 (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..b524b7e5e5 --- /dev/null +++ b/challenge-147/roger-bell-west/python/ch-2.py @@ -0,0 +1,39 @@ +#! /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 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..d333f3337f --- /dev/null +++ b/challenge-147/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,38 @@ +#! /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 (%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..3c0de3457a --- /dev/null +++ b/challenge-147/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,38 @@ +#! /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 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<u32> { + let mut primesh: BTreeSet<u32> = 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::<Vec<u32>>() +} + +fn ltruncprimes(count: usize) -> Vec<u32> { + let mut out: Vec<u32> = Vec::new(); + let mut lt = 0; + let p = + genprimes(500).iter().map(|i| i.to_string()).collect::<Vec<String>>(); + let pp: HashSet<String> = 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::<u32>().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..044722976e --- /dev/null +++ b/challenge-147/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,59 @@ +#! /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 |
