[Purdue-pm] sexy primes, etc.
Mark Senn
mark at purdue.edu
Sat Aug 24 17:27:57 PDT 2019
Perl Weekly Challenge 022, Task #1:
Write a script to print first 10 Sexy Prime Pairs. Sexy primes are prime
numbers that differ from each other by 6. For example, the numbers 5 and
11 are both sexy primes, because 11 - 5 = 6. The term “sexy prime” is a
pun stemming from the Latin word for six: sex.
See
https://engineering.purdue.edu/~mark/pwc-022-1.pdf
for my solution using a circular buffer of two elements using Perl 6.
A description of why only two elements are needed is my blog entry.
Some neat Perl 6 stuff:
# This is a loop that generates primes one at a time
((2.. Inf).grep ({.is -prime })) {
# Is the numeric variable $var is in the array @a one or more times?
# There is also "one" for occurs one time, and "none" for zero times.
if ($var == @a.any) {
if ($a %% $b) { # do following block if $a mod $b is zero
if ($a < $b < $c) { # is $a < $b and $b < $c?
is ([<=] @array) { # is @array sorted numerically
say ([+] @array) / @array.elems; # print average of @array
One think I wish Perl 6 did (Wolfram Language (Mathematica) does it):
scalar times array = array
2 * (1,2) gives (2,4) in a world I want t live in
But one can overload all built in operators depending on their types
to do different things. I think this can be done easily in Perl
but a separate definition would need to be done for + - * / etc.
-mark
More information about the Purdue-pm
mailing list