Smarandache-Wellin primes: Difference between revisions

m (julia example)
Line 457:
19ᵗʰ: Index: 1086, 208614364610327343341589284471
20ᵗʰ: Index: 1187, 229667386663354357356628334581</pre>
=={{header|Ruby}}==
The first seven Smarandache-Wellin primes are found in about 12 seconds on my system. The eighth (not shown here) takes nine minutes.
<syntaxhighlight lang="ruby" line>require 'prime'
require 'openssl' # for it's faster 'prime?' method
 
Smarandache_Wellins = Struct.new(:index, :last_prime, :sw, :derived)
 
def derived(n)
counter = Array.new(10){|i| [i, 0]}.to_h # counter is a Hash
n.digits.tally(counter).values.join.to_i
end
 
def prime?(n) = OpenSSL::BN.new(n).prime?
 
ord = {1 => "1st", 2 => "2nd", 3 => "3rd"}
ord.default_proc = proc {|_h, k| k.to_s + "th"} # _ in _h means "not used"
 
smarandache_wellinses = Enumerator.new do |y|
str = ""
Prime.each.with_index(1) do |pr, i|
str += pr.to_s
sw = str.to_i
y << Smarandache_Wellins.new(i, pr, sw, derived(sw))
end
end
 
smarandache_wellins_primes = Enumerator.new do |y|
smarandache_wellinses.rewind
loop do
s_w = smarandache_wellinses.next
y << s_w if prime?(s_w.sw)
end
end
 
sw_derived_primes = Enumerator.new do |y|
smarandache_wellinses.rewind
loop do
sw = smarandache_wellinses.next
y << sw if prime?(sw.derived)
end
end
 
n = 3
puts "The first #{n} Smarandache-Wellin primes are:"
puts smarandache_wellins_primes.first(n).map(&:sw)
puts "\nThe first #{n} Smarandache-Wellin derived primes are:"
puts sw_derived_primes.first(n).map(&:derived)
 
n = 7
puts "\nThe first #{n} Smarandache-Wellin primes are:"
smarandache_wellins_primes.first(n).each.with_index(1) do |swp, i|
puts "%s: index %4d, digits %4d, last prime %5d " % [ord[i], swp.index, swp.sw.digits.size, swp.last_prime]
end
 
n = 20
puts "\nThe first #{n} Derived Smarandache-Wellin primes are:"
sw_derived_primes.first(n).each.with_index(1) do |swdp, i|
puts "%4s: index %4d, prime %s" % [ord[i], swdp.index, swdp.derived]
end</syntaxhighlight>
{{out}}
<pre>The first 3 Smarandache-Wellin primes are:
2
23
2357
 
The first 3 Smarandache-Wellin derived primes are:
4194123321127
547233879626521
547233979727521
 
The first 7 Smarandache-Wellin primes are:
1st: index 1, digits 1, last prime 2
2nd: index 2, digits 2, last prime 3
3rd: index 4, digits 4, last prime 7
4th: index 128, digits 355, last prime 719
5th: index 174, digits 499, last prime 1033
6th: index 342, digits 1171, last prime 2297
7th: index 435, digits 1543, last prime 3037
 
The first 20 Derived Smarandache-Wellin primes are:
1st: index 32, prime 4194123321127
2nd: index 72, prime 547233879626521
3rd: index 73, prime 547233979727521
4th: index 134, prime 13672766322929571043
5th: index 225, prime 3916856106393739943689
6th: index 303, prime 462696313560586013558131
7th: index 309, prime 532727113760586013758133
8th: index 363, prime 6430314317473636515467149
9th: index 462, prime 8734722823685889120488197
10th: index 490, prime 9035923128899919621189209
11th: index 495, prime 9036023329699969621389211
12th: index 522, prime 9337023533410210710923191219
13th: index 538, prime 94374237357103109113243102223
14th: index 624, prime 117416265406198131121272110263
15th: index 721, prime 141459282456260193137317129313
16th: index 738, prime 144466284461264224139325131317
17th: index 790, prime 156483290479273277162351153339
18th: index 852, prime 164518312512286294233375158359
19th: index 1087, prime 208614364610327343341589284471
20th: index 1188, prime 229667386663354357356628334581
</pre>
 
=={{header|Wren}}==
1,149

edits