Special neighbor primes: Difference between revisions

Added Go
(→‎{{header|Wren}}: Removed a superfluous condition.)
(Added Go)
Line 4:
 
<br><br>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<lang go>package main
 
import (
"fmt"
"rcu"
)
 
const MAX = 1e7 - 1
 
var primes = rcu.Primes(MAX)
 
func specialNP(limit int, showAll bool) {
if showAll {
fmt.Println("Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:")
}
count := 0
for i := 1; i < len(primes); i++ {
p2 := primes[i]
if p2 >= limit {
break
}
p1 := primes[i-1]
p3 := p1 + p2 - 1
if rcu.IsPrime(p3) {
if showAll {
fmt.Printf("(%2d, %2d) => %3d\n", p1, p2, p3)
}
count++
}
}
ccount := rcu.Commatize(count)
climit := rcu.Commatize(limit)
fmt.Printf("\nFound %s special neighbor primes under %s.\n", ccount, climit)
}
 
func main() {
specialNP(100, true)
var pow = 1000
for i := 3; i < 8; i++ {
specialNP(pow, false)
pow *= 10
}
}</lang>
 
{{out}}
<pre>
Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:
( 3, 5) => 7
( 5, 7) => 11
( 7, 11) => 17
(11, 13) => 23
(13, 17) => 29
(19, 23) => 41
(29, 31) => 59
(31, 37) => 67
(41, 43) => 83
(43, 47) => 89
(61, 67) => 127
(67, 71) => 137
(73, 79) => 151
 
Found 13 special neighbor primes under 100.
 
Found 71 special neighbor primes under 1,000.
 
Found 367 special neighbor primes under 10,000.
 
Found 2,165 special neighbor primes under 100,000.
 
Found 14,526 special neighbor primes under 1,000,000.
 
Found 103,611 special neighbor primes under 10,000,000.
</pre>
 
=={{header|REXX}}==
9,488

edits