One-two primes: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: code formatting) |
(Created Nim solution.) |
||
Line 361: | Line 361: | ||
1900: (1 x 1889) 122212212211 |
1900: (1 x 1889) 122212212211 |
||
2000: (1 x 1989) 122121121211 |
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> |
</pre> |
||