Semiprime: Difference between revisions

→‎{{header|Perl}}: Switch to ntheory, modify the shortcut example a little.
(→‎{{header|Perl}}: Switch to ntheory, modify the shortcut example a little.)
Line 528:
 
=={{header|Perl}}==
{{libheader|ntheory}}
<tt>factor</tt> in scalar context gives the number of factors (like <tt>bigomega</tt> in Pari/GP and <tt>PrimeOmega</tt> in Mathematica).
<lang perl>use Math::Prime::Utilntheory "factor";
print join(" ", grep { scalar factor($_) == 2 } 1..100),"\n";
print join(" ", grep { scalar factor($_) == 2 } 1675..1681),"\n";
Line 538 ⟶ 539:
4 1679 1234567 900660121</pre>
 
Following Pari/GP's example, for inputs over 10^10 or so, we can save some time by usinglooking trialfor divisionsmall factors first:
<lang perl>myuse @_small_primesntheory =qw/factor @{primes(100)}is_prime trial_factor/;
sub issemi {
my $n = shift;
forif ((my $@p (@_small_primes) { return= !!is_primetrial_factor($n/$p,500)) unless> $n1) % $p; }{
return 0 if @p > 2;
return !!is_prime($p[1]) if @p == 2;
}
2 == factor($n);
}</lang>
Anonymous user