Sexy prime quintuplets: Sexy prime quintuplets are groups of five primes with a common difference of 6. One of the terms must be divisible by 5, because 5 and 6 are relatively prime. Thus, the only possible sexy prime quintuplet is (5 11 17 23 29)

Task

For each of pairs, triplets, quadruplets and quintuplets, Find and display the count of each group type of sexy primes less than one million thirty-five (1,000,035).

Display at most the last5, less than one million thirty-five, of each sexy prime group type.

Find and display the count of the unsexy primes less than one million thirty-five.

Find and display the last 10 unsexy primes less than one million thirty-five.

Note that 1000033 SHOULD NOT be counted in the pair count. It is sexy, but not in a pair within the limit. However, it also SHOULD NOT be listed in the unsexy primes since it is sexy.

There are 48627 unsexy primes less than 1,000,035. The last 10 are:
999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003
There are 16386 sexy prime pairs all components of which are less than 1,000,035. The last 5 are:
(999371,999377) (999431,999437) (999721,999727) (999763,999769) (999953,999959)
There are 2900 sexy prime triplets all components of which are less than 1,000,035. The last 5 are:
(997427,997433,997439) (997541,997547,997553) (998071,998077,998083) (998617,998623,998629) (998737,998743,998749)
There are 325 sexy prime quadruplets all components of which are less than 1,000,035. The last 5 are:
(977351,977357,977363,977369) (983771,983777,983783,983789) (986131.986137,986143,986149) (990371,990377,990383,990389) (997091,997097,997103,997109)
There are 1 sexy prime quintuplets all components of which are less than 1,000,035. The last 5 are:
(5,11,17,23,29)

The ntheory module includes a function to do very efficient sieving for prime clusters. Even though we are doing repeated work for this task, it is still faster than the previous code. The helper subroutines and output code remain identical, as does the generated output.

The cluster sieve becomes more efficient as the number of terms increases. See for example OEIS Prime 11-tuplets.

function create_sieve(integer limit) sequence sieve = repeat(true,limit) sieve[1] = false for i=4 to limit by 2 do sieve[i] = false end for for p=3 to floor(sqrt(limit)) by 2 do integer p2 = p*p if sieve[p2] then for k=p2 to limit by p*2 do sieve[k] = false end for end if end for return sieveend function

for i=limit to 3 by -2 do -- (this loop skips 2) if sieve[i] then total += 1 if sieve[i+6]=false and (i-6<0 or sieve[i-6]=false) then counts[1] += 1 -- unsexy if length(sets[1])<limits[1] then sets[1] = prepend(sets[1],i) end if else sequence set = {i} for j=i-6 to 3 by -6 do if j<=0 or sieve[j]=false then exit end if set = prepend(set,j) integer l = length(set) if length(sets[l])<limits[l] then sets[l] = prepend(sets[l],set) end if counts[l] += 1 end for end if end ifend forif length(sets[1])<limits[1] then sets[1] = prepend(sets[1],2) -- (as 2 skipped above)end if

constant fmt = """Of %,d primes less than %,d there are:%,d unsexy primes, the last %d being %s%,d pairs, the last %d being %s%,d triplets, the last %d being %s%,d quadruplets, the last %d being %s%,d quintuplet, the last %d being %s"""sequence results = {total,lim, 0,0,"", 0,0,"", 0,0,"", 0,0,"", 0,0,""}for i=1 to 5 do results[i*3..i*3+2] = {counts[i],length(sets[i]),sprint(sets[i])}end forprintf(1,fmt,results)

Output:

Of 78,500 primes less than 1,000,035 there are:
48,627 unsexy primes, the last 10 being {999853,999863,999883,999907,999917,999931,999961,999979,999983,1000003}
16,386 pairs, the last 5 being {{999371,999377},{999431,999437},{999721,999727},{999763,999769},{999953,999959}}
2,900 triplets, the last 4 being {{997541,997547,997553},{998071,998077,998083},{998617,998623,998629},{998737,998743,998749}}
325 quadruplets, the last 3 being {{986131,986137,986143,986149},{990371,990377,990383,990389},{997091,997097,997103,997109}}
1 quintuplet, the last 1 being {{5,11,17,23,29}}

#Functional Sexy Primes. Nigel Galloway: October 5th., 2018fromitertoolsimport *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)andnot 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)

Output:

There are 16386 sexy prime pairs all components of which are less than 1,000,035. The last 5 are:
(999371,999377)
(999431,999437)
(999721,999727)
(999763,999769)
(999953,999959)
There are 2900 sexy prime triplets all components of which are less than 1,000,035. The last 5 are:
(997427,997433,997439)
(997541,997547,997553)
(998071,998077,998083)
(998617,998623,998629)
(998737,998743,998749)
There are 325 sexy prime quadruplets all components of which are less than 1,000,035. The last 5 are:
(977351,977357,977363,977369)
(983771,983777,983783,983789)
(986131,986137,986143,986149)
(990371,990377,990383,990389)
(997091,997097,997103,997109)
There are 1 sexy prime quintuplets all components of which are less than 1,000,035. The last 5 are:
(5,11,17,23,29)
There are 48627 unsexy primes less than 1,000,035. The last 10 are:
999853
999863
999883
999907
999917
999931
999961
999979
999983
1000003

fcn msg(N,s,ps,n,g){ n=n.min(ps.len()); // if the number of primes is less than n gs:=ps[-n,*].apply('wrap(n){ [0..g*6,6].apply('+(n)) }) .pump(String,T("concat", ","),"(%s) ".fmt); println("Number of %s less than %,d is %,d".fmt(s,N,ps.len())); println("The last %d %s:\n %s\n".fmt(n, (n>1 and "are" or "is"), gs));}

Output:

Number of sexy prime pairs less than 1,000,035 is 16,386
The last 5 are:
(999371,999377) (999431,999437) (999721,999727) (999763,999769) (999953,999959)
Number of sexy triplet primes less than 1,000,035 is 2,900
The last 5 are:
(997427,997433,997439) (997541,997547,997553) (998071,998077,998083) (998617,998623,998629) (998737,998743,998749)
Number of sexy quadruplet primes less than 1,000,035 is 325
The last 5 are:
(977351,977357,977363,977369) (983771,983777,983783,983789) (986131,986137,986143,986149) (990371,990377,990383,990389) (997091,997097,997103,997109)
Number of sexy quintuplet primes less than 1,000,035 is 1
The last 1 is:
(5,11,17,23,29)
Number of unsexy primes less than 1,000,035 is 48,627
The last 10 are:
(999853) (999863) (999883) (999907) (999917) (999931) (999961) (999979) (999983) (1000003)