Prime numbers whose neighboring pairs are tetraprimes: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: Wasn't quite right before - hence problem with Wren alignment.)
(→‎{{header|Go}}: Updated in line with Wren version of which it's a translation - about 5 times quicker than before.)
Line 454: Line 454:
)
)


const LIMIT = int(1e7)
func hasDups(pf []int) bool {
var primes = rcu.Primes(LIMIT)
le := len(pf)

if le == 1 {
func isTetraPrime(n int) bool {
return false
}
count := 0;
for i := 1; i < le; i++ {
prevFact := 1
if pf[i] == pf[i-1] {
for _, p := range primes {
return true
if p * p <= n {
for n % p == 0 {
if count == 4 || p == prevFact {
return false
}
count++
n /= p
prevFact = p
}
} else {
break
}
}
}
}
return false
if n > 1 {
if count == 4 || n == prevFact {
}
return false

func contains(pf []int, value int) bool {
for i := 0; i < len(pf); i++ {
if pf[i] == value {
return true
}
}
count++
}
}
return false
return count == 4
}
}

// Note that 'gaps' will only contain even numbers here.
// Note that 'gaps' will only contain even numbers here.
func median(gaps []int) int {
func median(gaps []int) int {
Line 487: Line 494:


func main() {
func main() {
const LIMIT = int(1e7)
primes := rcu.Primes(LIMIT)
highest5 := primes[sort.SearchInts(primes, int(1e5))-1]
highest5 := primes[sort.SearchInts(primes, int(1e5))-1]
highest6 := primes[sort.SearchInts(primes, int(1e6))-1]
highest6 := primes[sort.SearchInts(primes, int(1e6))-1]
Line 496: Line 501:
j := 100_000
j := 100_000
for _, p := range primes {
for _, p := range primes {
// process even numbers first as likely to have most factors
pf1 := rcu.PrimeFactors(p - 2)
cond1 := len(pf1) == 4 && !hasDups(pf1)
if isTetraPrime(p-1) && isTetraPrime(p-2) {

pf2 := rcu.PrimeFactors(p - 1)
cond2 := len(pf2) == 4 && !hasDups(pf2)

pf3 := rcu.PrimeFactors(p + 1)
cond3 := len(pf3) == 4 && !hasDups(pf3)

pf4 := rcu.PrimeFactors(p + 2)
cond4 := len(pf4) == 4 && !hasDups(pf4)

if cond1 && cond2 {
tetras1 = append(tetras1, p)
tetras1 = append(tetras1, p)
if contains(pf1, 7) || contains(pf2, 7) {
if (p-1)%7 == 0 || (p-2)%7 == 0 {
sevens1++
sevens1++
}
}
}
}

if cond3 && cond4 {
if isTetraPrime(p+1) && isTetraPrime(p+2) {
tetras2 = append(tetras2, p)
tetras2 = append(tetras2, p)
if contains(pf3, 7) || contains(pf4, 7) {
if (p+1)%7 == 0 || (p+2)%7 == 0 {
sevens2++
sevens2++
}
}
}
}

if p == highest5 || p == highest6 || p == highest7 {
if p == highest5 || p == highest6 || p == highest7 {
for i := 0; i < 2; i++ {
for i := 0; i < 2; i++ {