RosettaCodeData/Task/Sexy-primes/Python/sexy-primes-2.py
2023-07-01 13:44:08 -04:00

20 lines
1.5 KiB
Python

#Functional Sexy Primes. Nigel Galloway: October 5th., 2018
from itertools import *
z=primes()
n=frozenset(takewhile(lambda x: x<1000035,z))
ni=sorted(list(filter(lambda g: n.__contains__(g+6) ,n)))
print ("There are",len(ni),"sexy prime pairs all components of which are less than 1,000,035. The last 5 are:")
for g in islice(ni,max(len(ni)-5,0),len(ni)): print(format("(%d,%d) " % (g,g+6)))
nig=list(filter(lambda g: n.__contains__(g+12) ,ni))
print ("There are",len(nig),"sexy prime triplets all components of which are less than 1,000,035. The last 5 are:")
for g in islice(nig,max(len(nig)-5,0),len(nig)): print(format("(%d,%d,%d) " % (g,g+6,g+12)))
nige=list(filter(lambda g: n.__contains__(g+18) ,nig))
print ("There are",len(nige),"sexy prime quadruplets all components of which are less than 1,000,035. The last 5 are:")
for g in islice(nige,max(len(nige)-5,0),len(nige)): print(format("(%d,%d,%d,%d) " % (g,g+6,g+12,g+18)))
nigel=list(filter(lambda g: n.__contains__(g+24) ,nige))
print ("There are",len(nigel),"sexy prime quintuplets all components of which are less than 1,000,035. The last 5 are:")
for g in islice(nigel,max(len(nigel)-5,0),len(nigel)): print(format("(%d,%d,%d,%d,%d) " % (g,g+6,g+12,g+18,g+24)))
un=frozenset(takewhile(lambda x: x<1000050,z)).union(n)
unsexy=sorted(list(filter(lambda g: not un.__contains__(g+6) and not un.__contains__(g-6),n)))
print ("There are",len(unsexy),"unsexy primes less than 1,000,035. The last 10 are:")
for g in islice(unsexy,max(len(unsexy)-10,0),len(unsexy)): print(g)