One-two primes: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) m →{{header|Perl}}: code formatting |
Added Sidef |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 361:
1900: (1 x 1889) 122212212211
2000: (1 x 1989) 122121121211
</pre>
=={{header|Nim}}==
{{libheader|Nim-Integers}}
Based on the Python code in the OEIS A036229 entry.
<syntaxhighlight lang="Nim">import std/[strformat, strutils]
import integers
let
One = newInteger(1)
Ten = newInteger(10)
proc a036229(n: Positive): Integer =
var k = Ten^n div 9
var r = One shl n - 1
var m = newInteger(0)
while m <= r:
let t = k + newInteger(`$`(m, 2))
if t.isprime: return t
inc m
quit &"No {n}-digit prime found with only digits 1 or 2.", QuitFailure
func compressed(n: Integer): string =
let s = $n
let idx = s.find('2')
result = &"(1 × {idx}) " & s[idx..^1]
for n in 1..20:
echo &"{n:4}: {a036229(n)}"
echo()
for n in countup(100, 2000, 100):
echo &"{n:4}: {compressed(a036229(n))}"
</syntaxhighlight>
{{out}}
<pre> 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 × 92) 21112211
200: (1 × 192) 21112211
300: (1 × 288) 211121112221
400: (1 × 390) 2111122121
500: (1 × 488) 221222111111
600: (1 × 590) 2112222221
700: (1 × 689) 21111111111
800: (1 × 787) 2122222221111
900: (1 × 891) 222221221
1000: (1 × 988) 222122111121
1100: (1 × 1087) 2112111121111
1200: (1 × 1191) 211222211
1300: (1 × 1289) 22121221121
1400: (1 × 1388) 222211222121
1500: (1 × 1489) 21112121121
1600: (1 × 1587) 2121222122111
1700: (1 × 1688) 212121211121
1800: (1 × 1791) 221211121
1900: (1 × 1889) 22212212211
2000: (1 × 1989) 22121121211
</pre>
Line 604 ⟶ 680:
show_oeis36229()
</syntaxhighlight>{{out}} same as Wren, etc.
=={{header|Quackery}}==
<code>1-2-prime</code> returns the first qualifying prime of the specified number of digits, or <code>-1</code> if no qualifying prime found.
<code>prime</code> is defined at [[Miller–Rabin primality test#Quackery]].
<syntaxhighlight lang="Quackery"> [ -1 swap
dup temp put
bit times
[ [] i^
temp share times
[ dup 1 &
rot join swap
1 >> ]
swap witheach
[ 1+ swap 10 * + ]
dup prime iff
[ nip conclude ]
done
drop ]
temp release ] is 1-2-prime ( n --> n )
[] 20 times [ i^ 1+ 1-2-prime join ]
witheach [ echo cr ]</syntaxhighlight>
{{out}}
<pre>2
11
211
2111
12211
111121
1111211
11221211
111112121
1111111121
11111121121
111111211111
1111111121221
11111111112221
111111112111121
1111111112122111
11111111111112121
111111111111112111
1111111111111111111
11111111111111212121
</pre>
=={{header|Raku}}==
Line 808 ⟶ 933:
100: (8 x 91) 998998889
200: (8 x 190) 9888898989</pre>
=={{header|RPL}}==
Candidate numbers are generated lexically by the <code>UP12</code> recursive function to speed up execution: the first 20 terms are found in 1 minute 39 seconds.
{{works with|HP|50g}}
≪ ""
1 ROT '''START''' "1" + '''NEXT'''
≫ ‘<span style=color:blue>REPUNIT</span>’ STO <span style=color:grey>@ ''(n → "11..1")''</span>
≪
'''IF''' DUP '''THEN'''
DUP2 DUP SUB NUM 99 SWAP - CHR REPL
LASTARG ROT DROP
'''IF''' "1" == '''THEN''' 1 - <span style=color:blue>UP12</span> <span style=color:grey>@ factor the carry</span>
'''ELSE''' DROP '''END'''
'''ELSE''' DROP "1" SWAP + '''END'''
≫ ‘<span style=color:blue>UP12</span>’ STO <span style=color:grey>@ ''("121..21" n → "122..21")''</span>
≪ { } "2"
'''WHILE''' OVER SIZE 20 < '''REPEAT'''
DUP STR→
'''IF''' DUP ISPRIME? '''THEN'''
ROT SWAP + SWAP
SIZE 1 + <span style=color:blue>REPUNIT</span>
'''ELSE'''
DROP DUP SIZE <span style=color:blue>UP12</span>
'''END'''
'''END''' DROP
≫ ‘<span style=color:blue>TASK</span>’ STO
{{out}}
<pre>
1: {2 11 211 2111 12211 111121 1111211 11221211 111112121 1111111121 11111121121 111111211111 1111111121221 11111111112221 111111112111121 1111111112122111 11111111111112121 111111111111112111 1111111111111111111 11111111111111212121}
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">say "Smallest n digit prime using only 1 and 2:"
for k in (0..20) {
['1','2'].variations_with_repetition(k, {|*a|
var p = Num(a.join)
if (p.is_prime) {
printf("%4d. %s\n", k, p)
break
}
})
}
for k in (100..2000 `by` 100) {
['1','2'].variations_with_repetition(k-1, {|*a|
var p = Num(a.join + '1')
if (p.is_prime) {
var t = Str(p)
var i = t.index('2')
printf("%4d. (1 x %s) %s\n", k, i, t.substr(i))
break
}
})
}</syntaxhighlight>
{{out}}
<pre>
Smallest n digit prime using only 1 and 2:
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
300. (1 x 288) 211121112221
400. (1 x 390) 2111122121
500. (1 x 488) 221222111111
600. (1 x 590) 2112222221
700. (1 x 689) 21111111111
800. (1 x 787) 2122222221111
900. (1 x 891) 222221221
1000. (1 x 988) 222122111121
1100. (1 x 1087) 2112111121111
1200. (1 x 1191) 211222211
1300. (1 x 1289) 22121221121
1400. (1 x 1388) 222211222121
1500. (1 x 1489) 21112121121
1600. (1 x 1587) 2121222122111
1700. (1 x 1688) 212121211121
1800. (1 x 1791) 221211121
1900. (1 x 1889) 22212212211
2000. (1 x 1989) 22121121211
</pre>
=={{header|Wren}}==
Line 815 ⟶ 1,041:
===Task specific===
This is based on the Python code in the OEIS entry. Run time about 52 seconds.
<syntaxhighlight lang="
import "./fmt" for Fmt
import "./iterate" for Stepped
Line 891 ⟶ 1,117:
64-bit integers are needed to find the first 20 terms so we need to use the above module here.
<syntaxhighlight lang="
import "./fmt" for Conv, Fmt
Line 915 ⟶ 1,141:
===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="
import "./fmt" for Fmt
import "./iterate" for Stepped
|