Quad-power prime seeds: Difference between revisions
Content added Content deleted
(→{{header|C}}: Eliminated a variable.) |
(Added Go) |
||
Line 305: | Line 305: | ||
Sleep</syntaxhighlight> |
Sleep</syntaxhighlight> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|GMP(Go wrapper)}} |
|||
{{libheader|Go-rcu}} |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"fmt" |
|||
big "github.com/ncw/gmp" |
|||
"rcu" |
|||
) |
|||
var p, p2 *big.Int |
|||
func isQuadPowerPrimeSeed(n uint64) bool { |
|||
nn := new(big.Int).SetUint64(n) |
|||
p.Set(nn) |
|||
k := new(big.Int).SetUint64(n + 1) |
|||
p2.Add(p, k) |
|||
if !p2.ProbablyPrime(15) { |
|||
return false |
|||
} |
|||
for i := 0; i < 3; i++ { |
|||
p.Mul(p, nn) |
|||
p2.Set(p) |
|||
p2.Add(p2, k) |
|||
if !p2.ProbablyPrime(15) { |
|||
return false |
|||
} |
|||
} |
|||
return true |
|||
} |
|||
func ord(c int) string { |
|||
m := c % 100 |
|||
if m > 4 && m <= 20 { |
|||
return "th" |
|||
} |
|||
m %= 10 |
|||
switch m { |
|||
case 1: |
|||
return "st" |
|||
case 2: |
|||
return "nd" |
|||
case 3: |
|||
return "rd" |
|||
default: |
|||
return "th" |
|||
} |
|||
} |
|||
func main() { |
|||
p = new(big.Int) |
|||
p2 = new(big.Int) |
|||
c := 0 |
|||
m := 1 |
|||
n := uint64(1) |
|||
fmt.Println("First fifty quad-power prime seeds:") |
|||
for ; c < 50; n++ { |
|||
if isQuadPowerPrimeSeed(n) { |
|||
fmt.Printf("%7s ", rcu.Commatize(int(n))) |
|||
c++ |
|||
if c%10 == 0 { |
|||
fmt.Println() |
|||
} |
|||
} |
|||
} |
|||
fmt.Println("\nFirst quad-power prime seed greater than:") |
|||
for { |
|||
if isQuadPowerPrimeSeed(n) { |
|||
c++ |
|||
if n > 1000000*uint64(m) { |
|||
ns := rcu.Commatize(int(n)) |
|||
fmt.Printf(" %2d million is the %d%s: %10s\n", m, c, ord(c), ns) |
|||
m++ |
|||
if m == 11 { |
|||
break |
|||
} |
|||
} |
|||
} |
|||
n++ |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First fifty quad-power prime seeds: |
|||
1 2 5 6 69 131 426 1,665 2,129 2,696 |
|||
5,250 7,929 9,689 13,545 14,154 14,286 16,434 19,760 25,739 27,809 |
|||
29,631 36,821 41,819 46,619 48,321 59,030 60,500 61,955 62,321 73,610 |
|||
77,685 79,646 80,535 82,655 85,251 86,996 91,014 96,566 97,739 105,939 |
|||
108,240 108,681 119,754 122,436 123,164 126,489 140,636 150,480 153,179 163,070 |
|||
First quad-power prime seed greater than: |
|||
1 million is the 141st: 1,009,286 |
|||
2 million is the 234th: 2,015,496 |
|||
3 million is the 319th: 3,005,316 |
|||
4 million is the 383rd: 4,004,726 |
|||
5 million is the 452nd: 5,023,880 |
|||
6 million is the 514th: 6,000,554 |
|||
7 million is the 567th: 7,047,129 |
|||
8 million is the 601st: 8,005,710 |
|||
9 million is the 645th: 9,055,151 |
|||
10 million is the 701st: 10,023,600 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |