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?
Note: This is a cleverer (and theoretically faster) solution than prob041-heyajulia.raku.
This example showcases backwards Seq
s, the topic variable $_
and implicitly calling a method on it, blocks,
breaking out of a loop with last
, the fact that empty arrays are falsey, the topicalizer given
, and the hyper
method call operator ».
.
More
https://projecteuler.net/problem=41
Features used
Backwards
Seq
s - https://docs.raku.org/routine/%2E%2E%2E$_
and implicitly calling a method on it - https://docs.raku.org/syntax/%24_Blocks - https://docs.raku.org/syntax/blocks
Breaking out of a loop with
last
- https://docs.raku.org/syntax/lastEmpty arrays are falsey
The topicalizer
given
- https://docs.raku.org/syntax/givenHyper method call operator
».
. - https://docs.raku.org/language/operators#methodop_%C2%BB%2E_/_methodop_%3E%3E%2E
The answer is 7652413.
for 9...1 {
{ say .max; last; } if $_ given permutations(1..$_)».join.grep(*.is-prime);
}