

 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. 


 
# syntax: GAWK -f SEXY_PRIMES.AWK
BEGIN {
    cutoff = 1000034
    for (i=1; i<=cutoff; i++) {
      n1 = i
      if (is_prime(n1)) {
        total_primes++
        if ((n2 = n1 + 6) > cutoff) { continue }
        if (is_prime(n2)) {
          save(2,5,n1 FS n2)
          if ((n3 = n2 + 6) > cutoff) { continue }
          if (is_prime(n3)) {
            save(3,5,n1 FS n2 FS n3)
            if ((n4 = n3 + 6) > cutoff) { continue }
            if (is_prime(n4)) {
              save(4,5,n1 FS n2 FS n3 FS n4)
              if ((n5 = n4 + 6) > cutoff) { continue }
              if (is_prime(n5)) {
                save(5,5,n1 FS n2 FS n3 FS n4 FS n5)
              }
            }
          }
        }
        if ((s[2] s[3] s[4] s[5]) !~ (n1 "")) { # check for unsexy
          save(1,10,n1)
        }
      }
    }
    printf("%d primes less than %s\n\n",total_primes,cutoff+1)
    printf("%d unsexy primes\n%s\n\n",c[1],s[1])
    printf("%d sexy prime pairs\n%s\n\n",c[2],s[2])
    printf("%d sexy prime triplets\n%s\n\n",c[3],s[3])
    printf("%d sexy prime quadruplets\n%s\n\n",c[4],s[4])
    printf("%d sexy prime quintuplets\n%s\n\n",c[5],s[5])
    exit(0)
}
function is_prime(x,  i) {
    if (x <= 1) {
      return(0)
    }
    for (i=2; i<=int(sqrt(x)); i++) {
      if (x % i == 0) {
        return(0)
      }
    }
    return(1)
}
function save(key,nbr_to_keep,str) {
    c[key]++
    str = s[key] str ", "
    if (gsub(/,/,"&",str) > nbr_to_keep) {
      str = substr(str,index(str,",")+2)
    }
    s[key] = str
}
 