Twin primes: Difference between revisions

m (→‎{{header|REXX}}: simplified the GENP function.)
Line 463:
Under 10,000,000 there are 58,980 pairs of twin primes.
Under 100,000,000 there are 440,312 pairs of twin primes.
</pre>
==Tuple Generalization==
<p>We can generalize pairs to reflect any tuple of integer diffences between the first prime
and the successive primes: see http://www.rosettacode.org/wiki/Successive_prime_differences.
</p><p>
If we ignore the first difference from the index prime with itself (always 0), we can express a
prime pair as a diffence tuple of (2,), and a prime quadruplet such as [11, 3, 17, 19] as the
tuple starting with 11 of type (2, 6, 8).
</p>
<lang julia>using Primes
 
tuplefitsat(k, tup, arr) = all(i -> arr[k + i] - arr[k] == tup[i], 1:length(tup))
 
function countprimetuples(tup, n)
arr = [i for i in 2:n if isprime(i)]
return count(k -> tuplefitsat(k, tup, arr), 1:length(arr) - length(tup))
end
 
println("Count of prime pairs from 1 to 1 million: ", countprimetuples((2,), 1000000))
 
println("Count of a form of prime quads from 1 to 1 million: ",
countprimetuples((2, 6, 8), 1000000))
 
println("Count of a form of prime octets from 1 to 1 million: ",
countprimetuples((2, 6, 12, 14, 20, 24, 26), 1000000))
</lang>{{out}}
<pre>
Count of prime pairs from 1 to 1 million: 8169
Count of a form of prime quads from 1 to 1 million: 166
Count of a form of prime octets from 1 to 1 million: 3
</pre>
 
4,105

edits