Smarandache-Wellin primes: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (→{{header|Perl}}: added derived primes) |
(Created Nim solution.) |
||
Line 500: | Line 500: | ||
SmarandacheWellin() |
SmarandacheWellin() |
||
</syntaxhighlight>{{out}} Same as C and Go versions. |
</syntaxhighlight>{{out}} Same as C and Go versions. |
||
=={{header|Nim}}== |
|||
{{libheader|Nim-Integers}} |
|||
<syntaxhighlight lang="Nim">import std/[bitops, math, strformat, strutils, sugar, tables] |
|||
import integers |
|||
const N = 12_000 |
|||
let primes = @[2] & collect(for n in countup(3, N, 2): |
|||
if n.isPrime: n) |
|||
func compressed(str: string; size: int): string = |
|||
## Return a compressed value for long strings of digits. |
|||
if str.len <= 2 * size: str |
|||
else: &"{str[0..<size]}...{str[^size..^1]} ({str.len} digits)" |
|||
iterator swNumbers(): tuple[value: Integer; lastPrime: int] = |
|||
## Yield the SW-Numbers and the last prime added. |
|||
var swString = "" |
|||
for p in primes: |
|||
swString.addInt p |
|||
yield (newInteger(swString), p) |
|||
iterator derivedSwNumbers(): Integer = |
|||
## Yield the derived SW-Numbers. |
|||
for (n, _) in swNumbers(): |
|||
var dSwString = "" |
|||
let counts = toCountTable($n) |
|||
for d in '0'..'9': |
|||
dSwString.add $counts[d] |
|||
yield newInteger(dSwString.strip(leading = true, trailing = false, {'0'})) |
|||
echo "Known eight Smarandache-Wellin numbers which are prime:" |
|||
var idx, count = 0 |
|||
for (n, p)in swNumbers(): |
|||
inc idx |
|||
if n.isPrime: |
|||
inc count |
|||
echo &"{count}: index = {idx:<4} last prime = {p:<5} value = {compressed($n, 20)}" |
|||
if count == 8: break |
|||
echo "\nFirst 20 derived S-W numbers which are prime:" |
|||
idx = 0 |
|||
count = 0 |
|||
for n in derivedSwNumbers(): |
|||
inc idx |
|||
if n.isPrime: |
|||
inc count |
|||
echo &"{count:2}: index = {idx:<4} value = {n}" |
|||
if count == 20: break |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Known eight Smarandache-Wellin numbers which are prime: |
|||
1: index = 1 last prime = 2 value = 2 |
|||
2: index = 2 last prime = 3 value = 23 |
|||
3: index = 4 last prime = 7 value = 2357 |
|||
4: index = 128 last prime = 719 value = 23571113171923293137...73677683691701709719 (355 digits) |
|||
5: index = 174 last prime = 1033 value = 23571113171923293137...10131019102110311033 (499 digits) |
|||
6: index = 342 last prime = 2297 value = 23571113171923293137...22732281228722932297 (1171 digits) |
|||
7: index = 435 last prime = 3037 value = 23571113171923293137...30013011301930233037 (1543 digits) |
|||
8: index = 1429 last prime = 11927 value = 23571113171923293137...11903119091192311927 (5719 digits) |
|||
First 20 derived S-W numbers which are prime: |
|||
1: index = 32 value = 4194123321127 |
|||
2: index = 72 value = 547233879626521 |
|||
3: index = 73 value = 547233979727521 |
|||
4: index = 134 value = 13672766322929571043 |
|||
5: index = 225 value = 3916856106393739943689 |
|||
6: index = 303 value = 462696313560586013558131 |
|||
7: index = 309 value = 532727113760586013758133 |
|||
8: index = 363 value = 6430314317473636515467149 |
|||
9: index = 462 value = 8734722823685889120488197 |
|||
10: index = 490 value = 9035923128899919621189209 |
|||
11: index = 495 value = 9036023329699969621389211 |
|||
12: index = 522 value = 9337023533410210710923191219 |
|||
13: index = 538 value = 94374237357103109113243102223 |
|||
14: index = 624 value = 117416265406198131121272110263 |
|||
15: index = 721 value = 141459282456260193137317129313 |
|||
16: index = 738 value = 144466284461264224139325131317 |
|||
17: index = 790 value = 156483290479273277162351153339 |
|||
18: index = 852 value = 164518312512286294233375158359 |
|||
19: index = 1087 value = 208614364610327343341589284471 |
|||
20: index = 1188 value = 229667386663354357356628334581 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |