

 This page uses content from Wikipedia. The original article was at Sexy_prime. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



In mathematics, sexy primes are prime numbers that differ from each other by six.

For example, the numbers 5 and 11 are both sexy primes, because 11 minus 6 is 5.

The term "sexy prime" is a pun stemming from the Latin word for six: sex.



Sexy prime pairs: Sexy prime pairs are groups of two primes that differ by 6. e.g. (5 11), (7 13), (11 17)

See sequences: OEIS:A023201 and OEIS:A046117

Sexy prime triplets: Sexy prime triplets are groups of three primes where each differs from the next by 6. e.g. (5 11 17), (7 13 19), (17 23 29)

See sequences: OEIS:A046118, OEIS:A046119 and OEIS:A046120

Sexy prime quadruplets: Sexy prime quadruplets are groups of four primes where each differs from the next by 6. e.g. (5 11 17 23), (11 17 23 29)

See sequences: OEIS:A023271, OEIS:A046122, OEIS:A046123 and OEIS:A046124

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 last 5, 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. 


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 sieve
end function
 
constant lim = 1000035,
--constant lim = 100, -- (this works too)
         limit = lim-(and_bits(lim,1)=0),   -- (limit must be odd)
         sieve = create_sieve(limit+6)  -- (+6 to check for sexiness)
 
sequence sets = repeat({},5),   -- (unsexy,pairs,trips,quads,quins)
         limits = {10,5,4,3,1},
         counts = 1&repeat(0,4) -- (2 is an unsexy prime)
integer  total = 1              -- ""
 
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 if
end for
if 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 for
printf(1,fmt,results)
