Wherein we learn to never, ever stop screaming

#### THE WEEKLY CHALLENGE – PERL & RAKU #170 — Task 1

“**The oldest and strongest emotion of mankind is fear, and the oldest and strongest kind of fear is fear of the unknown.**“

**— H. P. Lovecraft**

## Primorial Numbers

**Submitted by:** Mohammad S Anwar

Write a script to generate the first 10 Primorial Numbers.

Primorial numbers are those formed by multiplying successive prime numbers.

For example,

```
P(0) = 1 (1)
P(1) = 2 (1x2)
P(2) = 6 (1x2×3)
P(3) = 30 (1x2×3×5)
P(4) = 210 (1x2×3×5×7)
```

#### Discussion

We first came across the sequence of “primorials” was in PWC155-1, when we used them in the construction of the Fortunate Numbers. In this simpler task, we are asked to just generate the primorials themselves. But to those unacquainted with the idea of what’s going on here, we’re getting ahead of ourselves. First we’re going to need some background.

Harvey Dubner loved prime numbers with a passion few of us will every achieve. In his pursuit of ever-larger prime numbers he also explored many *classifications* of primes to accomplish his goals, searching for relationships and patterns beneath the surface, such a twin primes or Fibonacci primes, to name just two. Considering both the interrelationships between the Fibonacci numbers — the properties that emerge from their construction — and the wide-open field of relationships between the prime numbers themselves, it was only a matter of time before the two were combined, in a manner Dubner dubbed the primorials.

So how did that work?

The primorials, then, are analogous to the factorials, in that they share the same generalized recurrence relation: given some reference sequence, to find the next number in the calculated sequence we multiply the previous by the next value in the reference.

**gen**(*n*) = **gen**(*n*-1) x **ref**(*n*)

In the factorials, the reference is the the positive whole numbers, which correspond directly to the indices, *n*.

**F**(*n*) = **F**(*n*-1) x *n*

In the primorials, however, the reference is the sequence of primes numbers,

**π** = (2, 3, 5, 7, 11, 13, 17, 19, 23, …)

Which gives us

**P**(*n*) = **P**(*n*-1) x **π**(*n*)

Suffice to say all sorts of hijinx ensue.

#### METHOD

##### PERL 5 SOLUTION

Somewhere between writing PWC 155 and now, I rewrote my prime generating routine to return an iterator closure that when called always returns the next value from its own private, growing array of prime numbers. So instead of resurfacing the previous version we’ll use that one instead. It’s pretty cool.

Once we have that, though, constructing a loop to construct a few primorials is straightforward.

```
~/Code/PWC/170-1-primorial-fear.pl
---------------------------------------------------------------
1# = 2
2# = 6
3# = 30
4# = 210
5# = 2310
6# = 30030
7# = 510510
8# = 9699690
9# = 223092870
10# = 6469693230
```

Here is the code:

```
use warnings;
use strict;
use utf8;
use feature ":5.26";
use feature qw(signatures);
no warnings 'experimental::signatures';
use constant { COUNT => 10 };
my $p = 1; ## multiplicative identity
my $iter = prime_generator();
for (1..COUNT) {
$p *= $iter->();
say sprintf "%3d# = %-d", $_, $p;
}
sub prime_generator {
## returns an iterator closure that always delivers the next prime
state @primes;
return sub {
if ( @primes < 2 ) {
push @primes, @primes == 0 ? 2 : 3;
return $primes[-1];
}
my $candidate = $primes[-1] ;
CANDIDATE: while ( $candidate += 2 ) {
my $sqrt_candidate = sqrt( $candidate );
for my $test ( @primes ) {
next CANDIDATE if $candidate % $test == 0;
last if $test > $sqrt_candidate;
}
push @primes, $candidate;
return $candidate;
}
}
}
```

##### Raku Solution

*The Perl Weekly Challenge, that idyllic glade wherein we stumble upon the holes for these sweet descents, is now known as *

**The Weekly Challenge – Perl and Raku**

*It is the creation of the lovely Mohammad Sajid Anwar and a veritable swarm of contributors from all over the world, who gather, as might be expected, weekly online to solve puzzles. Everyone is encouraged to visit, learn and contribute at *