Sexy primes

From Rosetta Code
Revision as of 15:32, 29 September 2018 by Thundergnat (talk | contribs) (New draft task and Perl 6 example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Sexy primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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 5 is 6.

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 (1,000,000).
  • Display the last 5 (or all if there are fewer), less than one million, of each sexy prime group type.
  • Find and display the first 25 unsexy primes.


Perl 6

Works with: Rakudo version 2018.08

<lang perl6>use Math::Primesieve; my $sieve = Math::Primesieve.new;

my $max = 1_000_000; my %primes = $sieve.primes($max + 36) X=> 1;

my $primes = %primes.keys.categorize: { .&sexy }

for <pair 2 triplet 3 quadruplet 4 quintuplet 5> -> $sexy, $cnt {

   say "Number of sexy prime {$sexy}s less than {comma $max}: ", +$primes{$sexy};
   say "   Last 5 sexy prime {$sexy}s less than {comma $max}: ",
     join ' ', $primes{$sexy}.sort(+*).tail(5).grep(*.defined).map:
     { "({ $_ «+« (0,6 … 24)[^$cnt] })" }
   say ;

}

say "First 25 unsexy primes less than {comma $max}: ", $primes<unsexy>.sort(+*)[^25];

sub sexy ($i) {

   (
       (so all(%primes{$i «+« (6,12,18,24)})) ?? 'quintuplet' !! Nil,
       (so all(%primes{$i «+« (6,12,18)   })) ?? 'quadruplet' !! Nil,
       (so all(%primes{$i «+« (6,12)      })) ?? 'triplet'    !! Nil,
       (so all(%primes{$i  +   6          })) ?? 'pair'       !! 'unsexy'
   ).grep: *.defined

}

sub comma { $^i.flip.comb(3).join(',').flip }</lang>

Output:
Number of sexy prime pairs less than 1,000,000: 16386
   Last 5 sexy prime pairs less than 1,000,000: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959)

Number of sexy prime triplets less than 1,000,000: 2900
   Last 5 sexy prime triplets less than 1,000,000: (997427 997433 997439) (997541 997547 997553) (998071 998077 998083) (998617 998623 998629) (998737 998743 998749)

Number of sexy prime quadruplets less than 1,000,000: 325
   Last 5 sexy prime quadruplets less than 1,000,000: (977351 977357 977363 977369) (983771 983777 983783 983789) (986131 986137 986143 986149) (990371 990377 990383 990389) (997091 997097 997103 997109)

Number of sexy prime quintuplets less than 1,000,000: 1
   Last 5 sexy prime quintuplets less than 1,000,000: (5 11 17 23 29)

First 25 unsexy primes less than 1,000,000: (2 3 19 29 43 59 71 79 89 109 113 127 137 139 149 163 179 181 197 199 211 229 239 241 269)