Clever!
I had missed checking "is-prime" straight away! Checked some large
primes, and my function hung badly. Thank you!
Is grep running on all "2 ..^ $n/2" values, and then picking the first,
or is that somehow lazily evaluated? I assume the former, or you'd have
no need to halve $n. But
Frederick P. Brooks, from the additional material in the 1995
edition of "The Mythical Man-Month" (1975):
"Much more is known today about software engineering than
was known in 1975. Which of the assertions in the original
1975 edition have been supported by data and experience?
Here's my new go to prime-factors function :
sub prime-factors( Int $n is copy where * > 1 ) {
gather {
while ( $n > 1 ) {
if ( $n.is-prime ) {
take $n;
last;
}
my $f = (2..^$n/2).grep($n %% *).first(*.is-prime);