Skip to content

Commit

Permalink
- Added a pre-test in Number is_squarefree(n) for relatively large …
Browse files Browse the repository at this point in the history
…`n`.

This helps when `n` has a small (<10^6) square divisor.

Example:

	say is_squarefree((381**59 - 1)/(381-1))    # takes less than 1ms
  • Loading branch information
trizen committed Oct 5, 2019
1 parent 3de1b0b commit 176860b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11498,8 +11498,16 @@ package Sidef::Types::Number::Number {

sub is_squarefree {
my ($x) = @_;
__is_int__($$x)
&& Math::Prime::Util::GMP::moebius(_big2uistr($x) // return Sidef::Types::Bool::Bool::FALSE)
__is_int__($$x) || return Sidef::Types::Bool::Bool::FALSE;

my $z = _any2mpz($$x) // return Sidef::Types::Bool::Bool::FALSE;

if (Math::GMPz::Rmpz_sizeinbase($z, 2) > 100) {
state $lim = __PACKAGE__->_set_uint(1e6);
$x->is_prob_squarefree($lim) || return Sidef::Types::Bool::Bool::FALSE;
}

Math::Prime::Util::GMP::moebius(_big2uistr($z) // return Sidef::Types::Bool::Bool::FALSE)
? Sidef::Types::Bool::Bool::TRUE
: Sidef::Types::Bool::Bool::FALSE;
}
Expand Down

0 comments on commit 176860b

Please sign in to comment.