Smarandache-Wellin primes: Difference between revisions
Content deleted Content added
Promoted to 'full' task. |
→{{header|Sidef}}: start the index from 1 |
||
(10 intermediate revisions by 7 users not shown) | |||
Line 215:
}
std::string abbreviate(
size_t len = str.size();
if (len >
return str;
}
Line 302:
19| 1087| 208614364610327343341589284471
20| 1188| 229667386663354357356628334581
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func nextprim num .
while isprim num = 0
num += 1
.
return num
.
len digs[] 10
prim = 1
while len prsw[] < 3 or len prdsw[] < 3
prim = nextprim (prim + 1)
h = prim
h[] = [ ]
while h > 0
d = h mod 10
digs[d + 1] += 1
h[] &= d
h = h div 10
.
for i = len h[] downto 1
sw = sw * 10 + h[i]
.
if isprim sw = 1
prsw[] &= sw
.
dsw = 0
for i to 10
if digs[i] = 0
h = 10
else
h = 1 + floor log10 digs[i]
h = pow 10 h
.
dsw = dsw * h + digs[i]
.
if isprim dsw = 1
prdsw[] &= dsw
.
.
print prsw[]
print prdsw[]
</syntaxhighlight>
{{out}}
<pre>
[ 2 23 2357 ]
[ 4194123321127 547233879626521 547233979727521 ]
</pre>
Line 454 ⟶ 514:
19th: index 1087 prime 208614364610327343341589284471
20th: index 1188 prime 229667386663354357356628334581
</pre>
=={{header|J}}==
<syntaxhighlight lang="j"> derive=. [: <:@#/.~ i.@10 , ]
digits=. ;@:(<@("."0@":)"0)
(#~ 1&p:) 10&#.@digits\ p: i. 10
2 23 2357
(#~ 1 p: {:"1) (# , 10x&#.@digits@derive@digits)\ p: i. 1200
32 4194123321127
72 547233879626521
73 547233979727521
134 13672766322929571043
225 3916856106393739943689
303 462696313560586013558131
309 532727113760586013758133
363 6430314317473636515467149
462 8734722823685889120488197
490 9035923128899919621189209
495 9036023329699969621389211
522 9337023533410210710923191219
538 94374237357103109113243102223
624 117416265406198131121272110263
721 141459282456260193137317129313
738 144466284461264224139325131317
790 156483290479273277162351153339
852 164518312512286294233375158359
1087 208614364610327343341589284471
1188 229667386663354357356628334581</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class SmarandacheWellinPrimes {
public static void main(String[] args) {
primes = listPrimesUpTo(12_000);
smarandacheWellinPrimes();
System.out.println();
derivedSmarandacheWellinPrimes();
}
private static void smarandacheWellinPrimes() {
int count = 0;
int index = 1;
int number = 2;
String numberString = "2";
System.out.println("The first eight Smarandache-Wellin primes are:");
while ( count < 8 ) {
if ( new BigInteger(numberString).isProbablePrime(CERTAINTY_LEVEL) ) {
count += 1;
System.out.println(String.format("%2d%s%-4d%s%-5d%s%d", count, ": index = ", index,
" last prime = ", number, " number of digits = ", numberString.length()));
}
number = primes.get(index);
numberString += number;
index += 1;
}
}
private static void derivedSmarandacheWellinPrimes() {
int count = 0;
int index = 0;
int[] digitFrequencies = new int[10];
System.out.println("The first 20 derived Smarandache-Wellin numbers which are prime:");
while ( count < 20 ) {
String prime = String.valueOf(primes.get(index));
for ( char ch : prime.toCharArray() ) {
digitFrequencies[ch - '0'] += 1;
}
index += 1;
String joined = Arrays.stream(digitFrequencies).mapToObj(String::valueOf).collect(Collectors.joining(""));
if ( new BigInteger(joined).isProbablePrime(CERTAINTY_LEVEL) ) {
count += 1;
System.out.println(String.format("%2d%s%-4d%s%s", count, ": index = ", index, " prime = ", joined));
}
}
}
private static List<Integer> listPrimesUpTo(int limit) {
final int halfLimit = ( limit + 1 ) / 2;
boolean[] composite = new boolean[halfLimit];
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
for ( int a = i + p; a < halfLimit; a += p ) {
composite[a] = true;
}
}
}
List<Integer> result = Stream.of(2).limit(1).collect(Collectors.toList());
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
result.add(p);
}
}
return result;
}
private static List<Integer> primes;
private static final int CERTAINTY_LEVEL = 15;
}
</syntaxhighlight>
{{ out }}
<pre>
The first eight Smarandache-Wellin primes are:
1: index = 1 last prime = 2 number of digits = 1
2: index = 2 last prime = 3 number of digits = 2
3: index = 4 last prime = 7 number of digits = 4
4: index = 128 last prime = 719 number of digits = 355
5: index = 174 last prime = 1033 number of digits = 499
6: index = 342 last prime = 2297 number of digits = 1171
7: index = 435 last prime = 3037 number of digits = 1543
8: index = 1429 last prime = 11927 number of digits = 5719
The first 20 derived Smarandache-Wellin numbers which are prime:
1: index = 32 prime = 4194123321127
2: index = 72 prime = 547233879626521
3: index = 73 prime = 547233979727521
4: index = 134 prime = 13672766322929571043
5: index = 225 prime = 3916856106393739943689
6: index = 303 prime = 462696313560586013558131
7: index = 309 prime = 532727113760586013758133
8: index = 363 prime = 6430314317473636515467149
9: index = 462 prime = 8734722823685889120488197
10: index = 490 prime = 9035923128899919621189209
11: index = 495 prime = 9036023329699969621389211
12: index = 522 prime = 9337023533410210710923191219
13: index = 538 prime = 94374237357103109113243102223
14: index = 624 prime = 117416265406198131121272110263
15: index = 721 prime = 141459282456260193137317129313
16: index = 738 prime = 144466284461264224139325131317
17: index = 790 prime = 156483290479273277162351153339
18: index = 852 prime = 164518312512286294233375158359
19: index = 1087 prime = 208614364610327343341589284471
20: index = 1188 prime = 229667386663354357356628334581
</pre>
Line 792 ⟶ 1,000:
19ᵗʰ: Index: 1086, 208614364610327343341589284471
20ᵗʰ: Index: 1187, 229667386663354357356628334581</pre>
=={{header|RPL}}==
The latest versions of RPL can handle large 500-digit integers, making it possible to search for the fifth SW prime. Unfortunately, even with an emulator, the primality test takes too long.
{{works with|HP|49}}
« "" 2
1 4 ROLL '''FOR''' j
SWAP OVER + SWAP NEXTPRIME
'''NEXT''' DROP
» » '<span style="color:blue">→SW</span>' STO
« →STR → swn
« { 10 } 0 CON
1 swn SIZE '''FOR''' j
swn j DUP SUB STR→ 1 + DUP2 GET 1 + PUT
'''NEXT'''
""
1 10 '''FOR''' j
OVER j GET +
'''NEXT'''
STR→ NIP
» '<span style="color:blue">DSW</span>' STO
« 0 → idx
« { }
'''WHILE''' DUP SIZE 4 < '''REPEAT'''
'''IF''' 'idx' INCR <span style="color:blue">→SW</span> DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''END'''
» » '<span style="color:blue">TASK1</span>' STO
« 0 → idx
« { }
'''WHILE''' DUP SIZE 4 < '''REPEAT'''
'''IF''' 'idx' INCR <span style="color:blue">→SW DSW</span> DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''END'''
» » '<span style="color:blue">TASK2</span>' STO
{{out}}
<pre>
2: { 2 23 2357 2357111317192329313741434753596167717379838997101103107109113127131137139149151157163167173179181191193197199211223227229233239241251257263269271277281283293307311313317331337347349353359367373379383389397401409419421431433439443449457461463467479487491499503509521523541547557563569571577587593599601607613617619631641643647653659661673677683691701709719 }
1: { 4194123321127 547233879626521 547233979727521 13672766322929571043 }
</pre>
=={{header|Ruby}}==
Line 893 ⟶ 1,141:
19th: index 1087, prime 208614364610327343341589284471
20th: index 1188, prime 229667386663354357356628334581
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">say "Smarandache-Wellen primes:"
for (var (p=2, i=1, n='', nth=1); nth <= 8; (p.next_prime!, ++i)) {
n += Str(p)
if (Num(n).is_prime){
var t = n
if (t.len > 50) {
t = (t.first(20) + ' ... ' + t.last(20) + " (#{t.len} digits)")
}
printf("%s: Index:%5d Last prime:%6d S-W: %s\n", nth, i, p, t)
++nth
}
}
say "\nSmarandache-Wellen derived primes:"
func derived(String n) -> String {
var freq = n.chars.freq
0..9 -> map { freq{_} \\ 0 }.join
}
for (var (p=2, i=1, nth=1, n=''); nth <= 20; (p.next_prime!, ++i)) {
n += Str(p)
var t = derived(n)
if (Num(t).is_prime){
if (t.len > 50) {
t = (t.first(20) + ' ... ' + t.last(20) + " (#{t.len} digits)")
}
printf("%2s: Index:%5d %s\n", nth, i, t)
++nth
}
}</syntaxhighlight>
{{out}}
<pre>
Smarandache-Wellen primes:
1: Index: 1 Last prime: 2 S-W: 2
2: Index: 2 Last prime: 3 S-W: 23
3: Index: 4 Last prime: 7 S-W: 2357
4: Index: 128 Last prime: 719 S-W: 23571113171923293137 ... 73677683691701709719 (355 digits)
5: Index: 174 Last prime: 1033 S-W: 23571113171923293137 ... 10131019102110311033 (499 digits)
6: Index: 342 Last prime: 2297 S-W: 23571113171923293137 ... 22732281228722932297 (1171 digits)
7: Index: 435 Last prime: 3037 S-W: 23571113171923293137 ... 30013011301930233037 (1543 digits)
8: Index: 1429 Last prime: 11927 S-W: 23571113171923293137 ... 11903119091192311927 (5719 digits)
Smarandache-Wellen derived primes:
1: Index: 32 4194123321127
2: Index: 72 547233879626521
3: Index: 73 547233979727521
4: Index: 134 13672766322929571043
5: Index: 225 3916856106393739943689
6: Index: 303 462696313560586013558131
7: Index: 309 532727113760586013758133
8: Index: 363 6430314317473636515467149
9: Index: 462 8734722823685889120488197
10: Index: 490 9035923128899919621189209
11: Index: 495 9036023329699969621389211
12: Index: 522 9337023533410210710923191219
13: Index: 538 94374237357103109113243102223
14: Index: 624 117416265406198131121272110263
15: Index: 721 141459282456260193137317129313
16: Index: 738 144466284461264224139325131317
17: Index: 790 156483290479273277162351153339
18: Index: 852 164518312512286294233375158359
19: Index: 1087 208614364610327343341589284471
20: Index: 1188 229667386663354357356628334581
</pre>
Line 900 ⟶ 1,216:
{{libheader|Wren-gmp}}
Need to use GMP here to find the 8th S-W prime in a reasonable time (35.5 seconds on my Core i7 machine).
<syntaxhighlight lang="
import "./gmp" for Mpz
import "./fmt"for Fmt
|