Pandigital Prime
AUTHOR
Julia (GitHub: heyajulia)
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists?
What's interesting here?
This example showcases Raku's support for functional programming, handy built-in functions like permutations
,
effortless parallelism with race
, and how flexible Raku is when it comes to type conversion. Notic how we join each
permutation into a string and check if it's prime without having to convert it to a number ourselves.
More
https://projecteuler.net/problem=41
Features used
Ranges - https://docs.raku.org/type/Range
permutations
- https://docs.raku.org/routine/permutationsTerse syntax for a hash with numeric values:
(:16degree, :2048batch) == {degree =
16, batch => 2048}>
The answer is 7652413.
(2..9)
.map({ permutations(1..$^end).Slip })
.race(:16degree, :2048batch)
.map(*.join)
.grep(*.is-prime)
.max
.say;