Pierpont primes: Difference between revisions

→‎{{header|Go}}: Added much quicker second version and amended preamble to the first.
(→‎{{header|Go}}: Added much quicker second version and amended preamble to the first.)
Line 81:
 
=={{header|Go}}==
{{libheader|GMP(Go wrapper)}}
Brute force but works very quickly.
===Brute force approach===
 
Despite being inherently inefficient, this still works very quickly (less than 0.4 seconds on my machine).
 
A GMP wrapper, rather than Go's math/big package, has been used for extra speed (about 3.5 times quicker).
 
However, in order to be sure that the first 250 Pierpont primes will be generated, it is necessary to choose the loop sizes to produce somewhat more than this and then sort them into order.
<lang go>package main
 
import (
"fmt"
big "mathgithub.com/bigncw/gmp"
"sort"
)
Line 164 ⟶ 171:
 
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
</pre>
 
===3-Smooth approach===
The strategy here is to generate successive [https://en.wikipedia.org/wiki/Smooth_number 3-smooth numbers], add (or subtract) one, check if prime and, if so, append to a slice of 'big' integers until the required number is reached.
 
The Pierpoint primes of the first and second kind are generated at the same time so there is no real need for parallel processing.
 
This approach is considerably faster than the first version. Although not shown below, it produces the first 250 Pierpont primes (of both kinds) in under 0.2 seconds and the first 1000 in about 7.4 seconds. However, the first 2000 takes around 100 seconds.
 
These timings are for my Celeron @1.6GHz and should therefore be much faster on a more modern machine.
 
<lang go>package main
 
import (
"fmt"
big "github.com/ncw/gmp"
"time"
)
 
var (
one = new(big.Int).SetUint64(1)
two = new(big.Int).SetUint64(2)
three = new(big.Int).SetUint64(3)
)
 
func min(i, j int) int {
if i < j {
return i
}
return j
}
 
func pierpont(n int, first bool) (p [2][]*big.Int) {
p[0] = make([]*big.Int, n)
p[1] = make([]*big.Int, n)
for i := 0; i < n; i++ {
p[0][i] = new(big.Int)
p[1][i] = new(big.Int)
}
p[0][0].Set(two)
count, count1, count2 := 0, 1, 0
var s []*big.Int
s = append(s, new(big.Int).Set(one))
i2, i3, k := 0, 0, 1
n2, n3, t := new(big.Int), new(big.Int), new(big.Int)
for count < n {
n2.Mul(s[i2], two)
n3.Mul(s[i3], three)
if n2.Cmp(n3) < 0 {
t.Set(n2)
i2++
} else {
t.Set(n3)
i3++
}
if t.Cmp(s[k-1]) > 0 {
s = append(s, new(big.Int).Set(t))
k++
t.Add(t, one)
if count1 < n && t.ProbablyPrime(10) {
p[0][count1].Set(t)
count1++
}
t.Sub(t, two)
if count2 < n && t.ProbablyPrime(10) {
p[1][count2].Set(t)
count2++
}
count = min(count1, count2)
}
}
return p
}
 
func main() {
start := time.Now()
p := pierpont(2000, true)
fmt.Println("First 50 Pierpont primes of the first kind:")
for i := 0; i < 50; i++ {
fmt.Printf("%8d ", p[0][i])
if (i-9)%10 == 0 {
fmt.Println()
}
}
fmt.Println("\nFirst 50 Pierpont primes of the second kind:")
for i := 0; i < 50; i++ {
fmt.Printf("%8d ", p[1][i])
if (i-9)%10 == 0 {
fmt.Println()
}
}
 
fmt.Println("\n250th Pierpont prime of the first kind:", p[0][249])
fmt.Println("\n250th Pierpont prime of the second kind:", p[1][249])
fmt.Println("\n1000th Pierpont prime of the first kind:", p[0][999])
fmt.Println("\n1000th Pierpont prime of the second kind:", p[1][999])
fmt.Println("\n2000th Pierpont prime of the first kind:", p[0][1999])
fmt.Println("\n2000th Pierpont prime of the second kind:", p[1][1999])
elapsed := time.Now().Sub(start)
fmt.Printf("\nTook %s\n", elapsed)
}</lang>
 
{{out}}
<pre>
First 50 Pierpont primes of the first kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
 
First 50 Pierpont primes of the second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
 
250th Pierpont prime of the first kind: 62518864539857068333550694039553
 
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
 
1000th Pierpont prime of the first kind: 69269314716439690250482558089997110961545818230232043107188537422260188701607997086273960899938499201024414931399264696270849
 
1000th Pierpont prime of the second kind: 1308088756227965581249669045506775407896673213729433892383353027814827286537163695213418982500477392209371001259166465228280492460735463423
 
2000th Pierpont prime of the first kind: 23647056334818750458979408107288138983957799805326855934519920502493109431728722178351835778368596067773810122477389192659352731519830867553659739507195398662712180250483714053474639899675114018023738461139103130959712720686117399642823861502738433
 
2000th Pierpont prime of the second kind: 1702224134662426018061116932011222570937093650174807121918750428723338890211147039320296240754205680537318845776107057915956535566573559841027244444877454493022783449689509569107393738917120492483994302725479684822283929715327187974256253064796234576415398735760543848603844607
 
Took 1m40.781726122s
</pre>
 
9,490

edits