One-two primes: Difference between revisions

→‎{{header|Wren}}: Added a generalized version.
(→‎{{header|Wren}}: Fixed precedence bug in Wren-cli version - results unaffected.)
(→‎{{header|Wren}}: Added a generalized version.)
Line 714:
{{libheader|Wren-fmt}}
{{libheader|Wren-iterate}}
===Task specific===
This is based on the Python code in the OEIS entry. Run time about 52 seconds.
<syntaxhighlight lang="ecmascript">import "./gmp" for Mpz
Line 811 ⟶ 812:
<pre>
As GMP version for first 20.
</pre>
 
===Generalized===
Slower than Raku at about 15.2 seconds though acceptable given that Wren is having to do a lot of string manipulation here.
<syntaxhighlight lang="ecmascript">import "./gmp" for Mpz
import "./fmt" for Fmt
import "./iterate" for Stepped
 
var firstPrime = Fn.new { |n, d|
var k = Mpz.ten.pow(n).sub(Mpz.one).div(Mpz.nine)
var r = Mpz.one.lsh(n).sub(Mpz.one)
var m = Mpz.zero
while (m <= r) {
var t = k + Mpz.fromStr(m.toString(2))
var s = t.toString
if (d[0] != 2) {
if (d[0] != 1) s = s.replace("1", d[0].toString)
if (d[1] != 2) s = s.replace("2", d[1].toString)
} else {
s = s.replace("1", "x").replace("2", d[1].toString).replace("x", "2")
}
if (s[0] == "0") s = "1" + s[1..-1]
t.setStr(s)
if (t.probPrime(15) > 0) return t
m.inc
}
return Mpz.zero
}
 
var digits = [
[0, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8],
[1, 9], [2, 3], [2, 7], [2, 9], [3, 4], [3, 5], [3, 7], [3, 8],
[4, 7], [4, 9], [5, 7], [5, 9], [6, 7], [7, 8], [7, 9], [8, 9]
]
for (d in digits) {
Fmt.print("Smallest n digit prime using only $d and $d (or '0' if none exists)", d[0], d[1])
for (n in 1..20) Fmt.print("$3d: $i", n, firstPrime.call(n, d))
for (n in Stepped.new(100..200, 100)) {
var t = firstPrime.call(n, d)
var ts = t.toString
if (d[0] != 0) {
var ix = ts.indexOf(d[1].toString)
Fmt.print("$3d: ($d x $3d) $s", n, d[0], ix, ts[ix..-1])
} else {
var ix = ts[1..-1].indexOf(d[1].toString)
Fmt.print("$3d: $d (0 x $3d) $s", n, d[1], ix, ts[ix..-1])
}
}
System.print()
}</syntaxhighlight>
 
{{out}}
<pre>
Smallest n digit prime using only 0 and 1 (or '0' if none exists)
1: 0
2: 11
3: 101
4: 0
5: 10111
6: 101111
7: 1011001
8: 10010101
9: 100100111
10: 1000001011
11: 10000001101
12: 100000001111
13: 1000000111001
14: 10000000001011
15: 100000000100101
16: 1000000000011101
17: 10000000000001101
18: 100000000000100111
19: 1000000000000010011
20: 10000000000001100101
100: 1 (0 x 93) 0101101
200: 1 (0 x 189) 01110101011
 
Smallest n digit prime using only 1 and 2 (or '0' if none exists)
1: 2
2: 11
3: 211
4: 2111
5: 12211
6: 111121
7: 1111211
8: 11221211
9: 111112121
10: 1111111121
11: 11111121121
12: 111111211111
13: 1111111121221
14: 11111111112221
15: 111111112111121
16: 1111111112122111
17: 11111111111112121
18: 111111111111112111
19: 1111111111111111111
20: 11111111111111212121
100: (1 x 92) 21112211
200: (1 x 192) 21112211
 
Smallest n digit prime using only 1 and 3 (or '0' if none exists)
1: 3
2: 11
3: 113
4: 3313
5: 11113
6: 113111
7: 1111333
8: 11111131
9: 111111113
10: 1111113313
11: 11111111113
12: 111111133333
13: 1111111111333
14: 11111111113133
15: 111111111113113
16: 1111111111313131
17: 11111111111131333
18: 111111111111111131
19: 1111111111111111111
20: 11111111111111111131
100: (1 x 94) 331131
200: (1 x 190) 3311311111
 
....
 
Smallest n digit prime using only 7 and 9 (or '0' if none exists)
1: 7
2: 79
3: 797
4: 0
5: 77797
6: 777977
7: 7777997
8: 77779799
9: 777777799
10: 7777779799
11: 77777779979
12: 777777779777
13: 7777777779977
14: 77777777777977
15: 777777777779797
16: 7777777777797799
17: 77777777777777797
18: 777777777777797977
19: 7777777777777777997
20: 77777777777777779997
100: (7 x 93) 9979979
200: (7 x 192) 99777779
 
Smallest n digit prime using only 8 and 9 (or '0' if none exists)
1: 0
2: 89
3: 0
4: 8999
5: 89899
6: 888989
7: 8888989
8: 88888999
9: 888898889
10: 8888888989
11: 88888888999
12: 888888898999
13: 8888888999899
14: 88888888888889
15: 888888888898999
16: 8888888888989999
17: 88888888888888889
18: 888888888888898889
19: 8888888888888888989
20: 88888888888889888989
100: (8 x 91) 998998889
200: (8 x 190) 9888898989
</pre>
9,488

edits