diff options
| author | Roger Bell_West <Firedrake@users.noreply.github.com> | 2022-01-03 11:16:59 +0000 |
|---|---|---|
| committer | Roger Bell_West <Firedrake@users.noreply.github.com> | 2022-01-03 11:16:59 +0000 |
| commit | c19a6fd03a75b06b22b3590345f48e3998e1143d (patch) | |
| tree | 53a0559dae1d3c3119c75733d5010e5f010b308d | |
| parent | 41f8dae6667e8b4215b9f2507d7a2e14236890b9 (diff) | |
| download | perlweeklychallenge-club-c19a6fd03a75b06b22b3590345f48e3998e1143d.tar.gz perlweeklychallenge-club-c19a6fd03a75b06b22b3590345f48e3998e1143d.tar.bz2 perlweeklychallenge-club-c19a6fd03a75b06b22b3590345f48e3998e1143d.zip | |
Solutions for challenge #146
18 files changed, 1036 insertions, 0 deletions
diff --git a/challenge-146/roger-bell-west/javascript/ch-1.js b/challenge-146/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..a663da78f2 --- /dev/null +++ b/challenge-146/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,50 @@ +#! /usr/bin/node + +function genprimes(mx) { + let primesh=new Set([2,3]); + for (let i = 6; i <= mx; i += 6) { + for (let j = i-1; j <= i+1; j += 2) { + if (j <= mx) { + primesh.add(j); + } + } + } + let q=[2,3,5,7]; + let p=q.shift(); + let mr=Math.floor(Math.sqrt(mx)); + while (p <= mr) { + if (primesh.has(p)) { + let i=p*p + for (let i=p*p; i <= mx; i += p) { + primesh.delete(i); + } + } + if (q.length < 2) { + q.push(q[q.length-1]+4); + q.push(q[q.length-1]+2); + } + p=q.shift(); + } + let primes=[...primesh]; + primes.sort(function(a,b) { + return a-b; + }); + return primes; +} + +function nthprime(n) { + let m=15; + if (n >= 6) { + m=Math.floor(1+n*Math.log(n*Math.log(n))); + } + let p=genprimes(m); + return p[n-1]; +} + +if (nthprime(10001) === 104743) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-146/roger-bell-west/javascript/ch-2.js b/challenge-146/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..b9708b6b0c --- /dev/null +++ b/challenge-146/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,87 @@ +#! /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 fraction() { + let r = Object.create(fraction.methods); + r.n = 1; + r.d = 1; + return r; +} + +fraction.methods = { + get_parent() { + let nn=this.n; + let dd=this.d; + if (nn < dd) { + dd -= nn + } else { + nn -= dd + } + let f=fraction(); + f.n=nn; + f.d=dd; + return f; + }, + + stringify() { + return `${this.n}/${this.d}`; + }, + + set_from_string(s) { + let q=s.split("/"); + this.n=q[0]; + this.d=q[1]; + return this; + } +} + +function p_gp(s) { + let f=fraction().set_from_string(s); + let out=[] + for (let i = 1; i <= 2; i++) { + f=f.get_parent(); + out.push(f.stringify()); + } + return out; +} + +if (deepEqual(p_gp("3/5"),[ "3/2", "1/2" ])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(p_gp("4/3"),[ "1/3", "1/2" ])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-146/roger-bell-west/kotlin/ch-1.kt b/challenge-146/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..b1dd18b623 --- /dev/null +++ b/challenge-146/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,51 @@ +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 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 nthprime(n: Int): Int { + var m=15 + if (n >= 6) { + val nn=n.toDouble() + m=1+(nn*ln(nn*ln(nn))).toInt() + } + val primes=genprimes(m) + return primes[n-1] +} + +fun main() { + if (nthprime(10001) == 104743) { + println("Pass") + } else { + println("FAIL") + } +} diff --git a/challenge-146/roger-bell-west/kotlin/ch-2.kt b/challenge-146/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..dfe68a7d71 --- /dev/null +++ b/challenge-146/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,51 @@ +class Fraction(var n: Int = 1,var d: Int = 1) { + + fun get_parent(): Fraction { + var nn=n + var dd=d + if (nn < dd) { + dd -= nn + } else { + nn -= dd + } + return Fraction(nn,dd) + } + + fun stringify(): String { + return "%d/%d".format(n,d) + } + + fun set_from_string(s: String): Fraction { + val q=s.split("/") + n=q[0].toInt() + d=q[1].toInt() + return this + } +} + +fun p_gp(s: String): ArrayList<String> { + var f=Fraction().set_from_string(s) + var out=ArrayList<String>() + for (i in 1..2) { + f=f.get_parent() + out.add(f.stringify()) + } + return out +} + +fun main() { + if(p_gp("3/5") == listOf("3/2","1/2")) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if(p_gp("4/3") == listOf("1/3","1/2")) { + print("Pass") + } else { + print("FAIL") + } + println("") + +} diff --git a/challenge-146/roger-bell-west/lua/ch-1.lua b/challenge-146/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..565b5b6b79 --- /dev/null +++ b/challenge-146/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,52 @@ +#! /usr/bin/lua + +function genprimes(mx) + primesh = {} + for i = 2, 3 do + primesh[i] = true + end + for i = 6, mx, 6 do + for j = i-1, i+1, 2 do + if j <= mx then + primesh[j]=true + end + end + end + q={2,3,5,7} + p=table.remove(q,1) + 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 + primes = {} + for k,v in pairs(primesh) do + table.insert(primes,k) + end + table.sort(primes) + return primes +end + +function nthprime(n) + m=15 + if n >= 6 then + m=1+n*math.log(n*math.log(n)) + end + primes=genprimes(m) + return primes[n] +end + +if nthprime(10001) == 104743 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-146/roger-bell-west/lua/ch-2.lua b/challenge-146/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..3084c3a124 --- /dev/null +++ b/challenge-146/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,86 @@ +#! /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 + +Fraction = {n = 1, b = 1} + +function Fraction:new (o) + o = o or {} + self.__index = self + setmetatable(o,self) + return o +end + +function Fraction:get_parent () + p=Fraction:new() + p.n = self.n + p.d = self.d + if p.n < p.d then + p.d = p.d - p.n + else + p.n = p.n - p.d + end + return p +end + +function Fraction:stringify () + return string.format("%d/%d",self.n,self.d) +end + +function Fraction:set_from_string (s) + for n, d in string.gmatch(s,"(%d+)/(%d+)") do + self.n=tonumber(n) + self.d=tonumber(d) + end + return self +end + +function p_gp(s) + f=Fraction:new():set_from_string(s) + out={} + for i = 1,2 do + f=f:get_parent() + table.insert(out,f:stringify()) + end + return out +end + +if recursive_compare(p_gp("3/5"),{"3/2","1/2"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(p_gp("4/3"),{"1/3","1/2"}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-146/roger-bell-west/perl/ch-1.pl b/challenge-146/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..3ceafc0c22 --- /dev/null +++ b/challenge-146/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,52 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +is(nthprime(10001),104743,'example 1'); + +sub nthprime { + my $n=shift; + my $m=15; + if ($n >= 6) { + $m=int(1+$n*log($n*log($n))); + } + my $primes=genprimes($m); + return $primes->[$n-1]; +} + +sub genprimes { + my $mx=shift; + my @primes; + { + my %primesh=map {$_ => 1} (2,3); + for (my $i=6;$i <= $mx; $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-146/roger-bell-west/perl/ch-2.pl b/challenge-146/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..54ac67dc6a --- /dev/null +++ b/challenge-146/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,62 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is_deeply(p_gp('3/5'),['3/2','1/2'],'example 1'); +is_deeply(p_gp('4/3'),['1/3','1/2'],'example 2'); + +sub p_gp { + my $s=shift; + my $f=Local::Fraction->new($s); + my @out; + foreach (1..2) { + $f=$f->get_parent(); + push @out,$f->stringify(); + } + return \@out; +} + +package Local::Fraction; + +sub new { + my $class=shift; + my $self={ + n => 1, + d => 1, + }; + bless $self,$class; + if (scalar @_ > 0) { + $self->set_from_string($_[0]); + } + return $self; +} + +sub get_parent { + my $self=shift; + my $p=Local::Fraction->new(); + $p->{n}=$self->{n}; + $p->{d}=$self->{d}; + if ($p->{n} < $p->{d}) { + $p->{d} -= $p->{n}; + } else { + $p->{n} -= $p->{d}; + } + return $p; +} + +sub stringify { + my $self=shift; + return join('/',$self->{n},$self->{d}); +} + +sub set_from_string { + my $self=shift; + my $s=shift; + if ($s =~ /^([0-9]+)\/([0-9]+)$/) { + $self->{n}=$1; + $self->{d}=$2; + } +} diff --git a/challenge-146/roger-bell-west/postscript/ch-1.ps b/challenge-146/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..9ea73cac8a --- /dev/null +++ b/challenge-146/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,77 @@ +%!PS + +% Warning, this is untested - it runs off the end of Ghostscript's ability +% to handle memory allocation happily. + +/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 + +/genprimes { + /mx exch def + /primesh mx dict def + 2 1 3 { + primesh exch 1 put + } for + 6 6 mx { + dup 1 sub exch 1 add 2 exch { + primesh exch 1 put + } 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 + +/nthprime { + /n exch def + /m 15 def + n 6 ge { + /m n ln n mul ln n mul 1 add cvi def + } if + /pr m genprimes def + pr n 1 sub get +} bind def + +10001 nthprime 104743 eq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-146/roger-bell-west/postscript/ch-2.ps b/challenge-146/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..c3fec3b250 --- /dev/null +++ b/challenge-146/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,74 @@ +%!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 + +/spush { % (ab) c -> (abc) + 3 dict begin + /t exch 0 get def + /a exch def + a length 1 add string /b exch def + a b copy pop + b b length 1 sub t put + b + end +} bind def + +/i2s { + dup log cvi 1 add string cvs +} bind def + +/stringify { + exch i2s 0 string exch spush + (/) spush + exch i2s spush +} bind def + +/get_parent { + 2 copy + gt { + exch 1 index sub exch + } { + 1 index sub + } ifelse +} bind def + +/p_gp { + /out 0 array def + (/) search { + cvi + exch pop + exch cvi + 1 1 2 { + pop get_parent 2 copy stringify out exch apush /out exch def + } for + out + } { + pop out + } ifelse +} bind def + +(3/5) p_gp [ (3/2) (1/2) ] aeq { (Pass) } { (FAIL) } ifelse print ( ) print +(4/3) p_gp [ (1/3) (1/2) ] aeq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-146/roger-bell-west/python/ch-1.py b/challenge-146/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..60d95f8a3c --- /dev/null +++ b/challenge-146/roger-bell-west/python/ch-1.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 + +import unittest + +from math import sqrt,log,floor +from collections import deque + +def genprimes(mx): + primesh=set(range(2,4)) + for i in range(6,mx+1,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 nthprime(n): + m=15 + if n >= 6: + m=floor(1+n*log(n*log(n))) + primes=genprimes(m) + return primes[n-1] + +class TestNthprime(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(nthprime(10001),104743,'example 1') + +unittest.main() diff --git a/challenge-146/roger-bell-west/python/ch-2.py b/challenge-146/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..91afe2ce09 --- /dev/null +++ b/challenge-146/roger-bell-west/python/ch-2.py @@ -0,0 +1,45 @@ +#! /usr/bin/python3 + +import unittest + +class Fraction(object): + def __init__(self): + self.n = 1 + self.d = 1 + + def get_parent(self): + p=Fraction() + p.n = self.n + p.d = self.d + if p.n < p.d: + p.d -= p.n + else: + p.n -= p.d + return p + + def stringify(self): + return str(self.n) + "/" + str(self.d) + + def set_from_string(self,s): + q=s.split("/") + self.n=int(q[0]) + self.d=int(q[1]) + return self + +def p_gp(s): + f=Fraction().set_from_string(s) + out=[] + for i in range(2): + f=f.get_parent() + out.append(f.stringify()) + return out + +class TestP_Gp(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(p_gp("3/5"),["3/2","1/2"],'example 1') + + def test_ex2(self): + self.assertEqual(p_gp("4/3"),["1/3","1/2"],'example 2') + +unittest.main() diff --git a/challenge-146/roger-bell-west/raku/ch-1.p6 b/challenge-146/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..280bb736a8 --- /dev/null +++ b/challenge-146/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,48 @@ +#! /usr/bin/perl6 + +use Test; +plan 1; + +is(nthprime(10001),104743,'example 1'); + +sub nthprime($n) { + my $m=15; + if ($n >= 6) { + $m=floor(1+$n*log($n*log($n))); + } + my @primes=genprimes($m); + return @primes[$n-1]; +} + +sub genprimes($mx) { + my @primes; + { + my $primesh=(2,3).SetHash; + loop (my $i=6;$i <= $mx; $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-146/roger-bell-west/raku/ch-2.p6 b/challenge-146/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..72df8f433e --- /dev/null +++ b/challenge-146/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,47 @@ +#! /usr/bin/perl6 + +use Test; +plan 2; + +is-deeply(p_gp('3/5'),['3/2','1/2'],'example 1'); +is-deeply(p_gp('4/3'),['1/3','1/2'],'example 2'); + +class LocalFraction { + has $.n is rw = 1; + has $.d is rw = 1; + + method get_parent { + my $p=LocalFraction.new(); + $p.n=self.n; + $p.d=self.d; + if ($p.n < $p.d) { + $p.d -= $p.n; + } else { + $p.n -= $p.d; + } + return $p; + } + + method stringify { + return join('/',self.n,self.d); + } + + method set_from_string($s) { + if ($s ~~ /^(<[0..9]>+)\/(<[0..9]>+)$/) { + self.n=$0; + self.d=$1; + } + return self + } + +} + +sub p_gp($s) { + my $f=LocalFraction.new().set_from_string($s); + my @out; + for (1..2) { + $f=$f.get_parent(); + push @out,$f.stringify(); + } + return @out; +} diff --git a/challenge-146/roger-bell-west/ruby/ch-1.rb b/challenge-146/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..ce54ada806 --- /dev/null +++ b/challenge-146/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,49 @@ +#! /usr/bin/ruby + +require 'set' + +def genprimes(mx) + primesh=Set.new([2,3]) + (6..mx).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 nthprime(n) + m=15 + if n >= 6 then + m=1+n*Math.log(n*Math.log(n)).floor + end + primes=genprimes(m) + return primes[n-1] +end + +require 'test/unit' + +class TestNthprime < Test::Unit::TestCase + + def test_ex1 + assert_equal(104743,nthprime(10001)) + end + +end diff --git a/challenge-146/roger-bell-west/ruby/ch-2.rb b/challenge-146/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..b4efaa5065 --- /dev/null +++ b/challenge-146/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,58 @@ +#! /usr/bin/ruby + +require 'set' + +class Fraction + attr_accessor :n, :d + def initialize + @n=1 + @d=1 + end + + def get_parent + p=Fraction.new + p.n=@n + p.d=@d + if p.n < p.d then + p.d -= p.n + else + p.n -= p.d + end + return p + end + + def stringify + return "#{@n}/#{@d}" + end + + def set_from_string(s) + q=s.split('/') + @n=q[0].to_i + @d=q[1].to_i + return self + end +end + +def p_gp(s) + f=Fraction.new.set_from_string(s) + out=[] + 1.upto(2) do + f=f.get_parent + out.push(f.stringify) + end + return out +end + +require 'test/unit' + +class TestP_Gp < Test::Unit::TestCase + + def test_ex1 + assert_equal(["3/2","1/2"],p_gp("3/5")) + end + + def test_ex2 + assert_equal(["1/3","1/2"],p_gp("4/3")) + end + +end diff --git a/challenge-146/roger-bell-west/rust/ch-1.rs b/challenge-146/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..a2cd6cd30a --- /dev/null +++ b/challenge-146/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,50 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::{HashSet, VecDeque}; +use std::iter::FromIterator; + +#[test] +fn test_ex1() { + assert_eq!(nthprime(10001), 104743); +} + +fn genprimes(mx: u32) -> Vec<u32> { + let mut primesh: HashSet<u32> = HashSet::from_iter(2..=3); + for i in (6..=mx).step_by(6) { + for j in [i - 1, i + 1] { + if j < mx { + primesh.insert(j); + } + } + } + let mut q = VecDeque::from([2, 3, 5, 7]); + let mut p = q.pop_front().unwrap(); + let mr = (mx as f64).sqrt() as 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(); + } + let mut primes = primesh.iter().map(|i| *i).collect::<Vec<u32>>(); + primes.sort(); + primes +} + +fn nthprime(n: usize) -> u32 { + let mut m = 15; + if n >= 6 { + let nn = n as f64; + m = (1f64 + (nn.ln() * nn).ln() * nn) as u32; + } + let primes = genprimes(m); + return primes[n - 1]; +} diff --git a/challenge-146/roger-bell-west/rust/ch-2.rs b/challenge-146/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..378548d5c0 --- /dev/null +++ b/challenge-146/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,56 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(p_gp("3/5"), vec!["3/2", "1/2"]); +} + +#[test] +fn test_ex2() { + assert_eq!(p_gp("4/3"), vec!["1/3", "1/2"]); +} + +#[derive(Clone, Copy)] +struct Fraction { + pub n: u32, + pub d: u32, +} + +impl Fraction { + pub fn new() -> Fraction { + Fraction { n: 1, d: 1 } + } + + pub fn |
