Sexy primes: Difference between revisions

m (→‎{{header|zkl}}: handle the case where the tail of the sexy prime pair is past N)
Line 268:
Number of unsexy primes less than 1,000,000: 48,626
Last 10 unsexy primes less than 1,000,000: (999809 999853 999863 999883 999907 999917 999931 999961 999979 999983)</pre>
 
=={{header|Python}}==
<lang python>LIMIT = 1_000_000
def primes2(limit=LIMIT):
if limit < 2: return []
if limit < 3: return [2]
lmtbf = (limit - 3) // 2
buf = [True] * (lmtbf + 1)
for i in range((int(limit ** 0.5) - 3) // 2 + 1):
if buf[i]:
p = i + i + 3
s = p * (i + 1) + i
buf[s::p] = [False] * ((lmtbf - s) // p + 1)
return [2] + [i + i + 3 for i, v in enumerate(buf) if v]
 
primes = primes2(LIMIT)
primeset = set(primes)
#%%
s = [[] for x in range(4)]
unsexy = []
for p in primes:
if p + 6 in primeset:
s[0].append((p, p+6))
else:
if p - 6 not in primeset:
unsexy.append(p)
continue
if p + 12 in primeset:
s[1].append((p, p+6, p+12))
else:
continue
if p + 18 in primeset:
s[2].append((p, p+6, p+12, p+18))
else:
continue
if p + 24 in primeset:
s[3].append((p, p+6, p+12, p+18, p+24))
#%%
print('"SEXY" PRIME GROUPINGS:')
for sexy, name in zip(s, 'pairs triplets quadruplets quintuplets'.split()):
print(f' {len(sexy)} {name} ending with ...')
for sx in sexy[-5:]:
print(' ',sx)
 
print(f'\nThere are {len(unsexy)} unsexy primes ending with ...')
for usx in unsexy[-10:]:
print(' ',usx)</lang>
 
{{out}}
<pre>"SEXY" PRIME GROUPINGS:
16386 pairs ending with ...
(999371, 999377)
(999431, 999437)
(999721, 999727)
(999763, 999769)
(999953, 999959)
2900 triplets ending with ...
(997427, 997433, 997439)
(997541, 997547, 997553)
(998071, 998077, 998083)
(998617, 998623, 998629)
(998737, 998743, 998749)
325 quadruplets ending with ...
(977351, 977357, 977363, 977369)
(983771, 983777, 983783, 983789)
(986131, 986137, 986143, 986149)
(990371, 990377, 990383, 990389)
(997091, 997097, 997103, 997109)
1 quintuplets ending with ...
(5, 11, 17, 23, 29)
 
There are 48626 unsexy primes ending with ...
999809
999853
999863
999883
999907
999917
999931
999961
999979
999983</pre>
 
=={{header|REXX}}==
Anonymous user