Welcome to Day 17 of the **Perl 6 One-Liner Advent Calendar**! Today, weâ€™ll have two one-liners, both generating some prime numbers.

## Part 1

First, let us solve Problem 7 of Project Euler, where you need to print the 10001^{st} number (having the first being 2).

Perl 6 is good at prime numbers, as it has a built-in method of the `Int` class, `is-prime`.

There are a few ways of generating prime numbers. For one-liners, the best is the simplest, but the least efficient, method that tests every number.

say ((1..*).grep: *.is-prime)[10000]

It takes about half-a-minute to compute the result, but the code is quite short. Someday, weâ€™ll solve the task using the so-called sieve of Eratosthenes, which should be much faster, but will probably require more lines of code.

## Part 2

In the second part of this advent post, let us play golf and solve the corresponding problem on the code-golf.io site. We need to print all prime numbers below 100.

My solution, which needs 22 characters, is the following:

.is-prime&&.say for ^â…

There is no shorter solution in Perl 6, while in J, they managed to have only 11 characters. In Perl 6, eight characters are consumed by the method name already. I believe, to win all golf contests, you need a special language with very short names (which J is) and a set of built-in routines to generate lists of prime, or Fibonacci, or any other numeric sequence. It should also strongly utilise Unicode character space.

In our Perl 6 example, there is also a Unicode character, `â…`. This not a simple C, the third letter from the Latin alphabet, but a Unicode character ROMAN NUMERAL ONE HUNDRED (which is originally the third letter of the Latin alphabet, of course). Using this symbol let us save two characters in the solution.

The `&&` trick is possible because Perl does not execute the second part of the Boolean expression if the first operand is False. Notice that you cannot use a single `&` here. The full non-optimised version of the code would need additional spaces and would look like this:

.say if .is-prime for ^100

And thatâ€™s the end of todayâ€™s Perl 6 journey, see you tomorrow!

## 2 thoughts on “ðŸŽ„ 17/25. Playing with prime numbers in Perl 6”