Smarandache-Wellin primes: Difference between revisions

Added Go
m (→‎{{header|Raku}}: Include ordinals, show more derived SW primes)
(Added Go)
Line 30:
<br>
 
=={{header|Go}}==
{{trans|Wren}}
===Basic===
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"rcu"
"strconv"
"strings"
)
 
func main() {
primes := rcu.Primes(400)
sw := ""
var swp []int
count := 0
i := 0
for count < 3 {
sw += strconv.Itoa(primes[i])
n, _ := strconv.Atoi(sw)
if rcu.IsPrime(n) {
swp = append(swp, n)
count++
}
i++
}
fmt.Println("The first 3 Smarandache-Wellin primes are:")
fmt.Printf("%v\n", swp)
 
freqs := make([]int, 10)
var dswp []int
count = 0
i = 0
for count < 3 {
p := strconv.Itoa(primes[i])
for _, d := range p {
n, _ := strconv.Atoi(string(d))
freqs[n]++
}
dsw := ""
for _, freq := range freqs {
dsw += strconv.Itoa(freq)
}
dsw = strings.TrimLeft(dsw, "0")
dn, _ := strconv.Atoi(dsw)
if rcu.IsPrime(dn) {
dswp = append(dswp, dn)
count++
}
i++
}
fmt.Println("\nThe first 3 Derived Smarandache-Wellin primes are:")
fmt.Printf("%v\n", dswp)
}</syntaxhighlight>
 
{{out}}
<pre>
The first 3 Smarandache-Wellin primes are:
[2 23 2357]
 
The first 3 Derived Smarandache-Wellin primes are:
[4194123321127 547233879626521 547233979727521]
</pre>
 
===Stretch===
{{libheader|GMP(Go wrapper)}}
We need to use the above GMP wrapper to match Wren's time of about 35.5 seconds as Go's native big.Int type is far slower.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
big "github.com/ncw/gmp"
"rcu"
"strconv"
)
 
func main() {
primes := rcu.Primes(12000)
sw := ""
count := 0
i := 0
n := new(big.Int)
fmt.Println("The 4th to the 8th Smarandache-Wellin primes are:")
for count < 8 {
sw += strconv.Itoa(primes[i])
n.SetString(sw, 10)
if n.ProbablyPrime(15) {
count++
if count > 3 {
fmt.Printf("%dth: index %4d digits %4d last prime %5d\n", count, i+1,
len(sw), primes[i])
}
}
i++
}
}</syntaxhighlight>
 
{{out}}
<pre>
The 4th to the 8th Smarandache-Wellin primes are:
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
8th: index 1429 digits 5719 last prime 11927
</pre>
 
=={{header|Raku}}==
9,476

edits