Find largest left truncatable prime in a given base: Difference between revisions

Added Go
(Added Go)
Line 649:
Count: 97667 44905 44904 44904 44904 44904
CPU time: 111.2656
</pre>
 
=={{header|Go}}==
{{trans|C}}
<lang go>package main
 
import (
"fmt"
"math/big"
)
 
var smallPrimes = [...]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
 
const maxStack = 128
 
var (
tens, values [maxStack]big.Int
bigTemp, answer = new(big.Int), new(big.Int)
base, seenDepth int
)
 
func addDigit(i int) {
for d := 1; d < base; d++ {
values[i].Set(&values[i-1])
bigTemp.SetUint64(uint64(d))
bigTemp.Mul(bigTemp, &tens[i])
values[i].Add(&values[i], bigTemp)
if !values[i].ProbablyPrime(1) {
continue
}
if i > seenDepth || (i == seenDepth && values[i].Cmp(answer) == 1) {
if !values[i].ProbablyPrime(50) {
continue
}
answer.Set(&values[i])
seenDepth = i
}
addDigit(i + 1)
}
}
 
func doBase() {
answer.SetUint64(0)
tens[0].SetUint64(1)
bigTemp.SetUint64(uint64(base))
seenDepth = 0
for i := 1; i < maxStack; i++ {
tens[i].Mul(&tens[i-1], bigTemp)
}
for i := 0; smallPrimes[i] < base; i++ {
values[0].SetUint64(uint64(smallPrimes[i]))
addDigit(1)
}
fmt.Printf("%2d: %s\n", base, answer.String())
}
 
func main() {
for base = 3; base <= 17; base++ {
doBase()
}
}</lang>
 
{{out}}
<pre>
3: 23
4: 4091
5: 7817
6: 4836525320399
7: 817337
8: 14005650767869
9: 1676456897
10: 357686312646216567629137
11: 2276005673
12: 13092430647736190817303130065827539
13: 812751503
14: 615419590422100474355767356763
15: 34068645705927662447286191
16: 1088303707153521644968345559987
17: 13563641583101
</pre>
 
9,482

edits