Skip to content

Commit

Permalink
- Optimizations in Number lpf(n) when n is very large, by using o…
Browse files Browse the repository at this point in the history
…ur fast `trial_factor(n,k)` method.

Example:

	say lpf(fib(50402326))		#=> 37 (takes only 0.39s -- before it took over 19s)

Also faster for:

	for k in (3..1000) {
 	   say (k! / (k-2)! -> fib.lpf)
	}

The above code now takes only 1.3s to run. Before it took over 10s.
  • Loading branch information
trizen committed Aug 16, 2019
1 parent 420b0cf commit 66200dc
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9601,12 +9601,23 @@ package Sidef::Types::Number::Number {
return bless \$z;
}

foreach my $p (2, 3, 5, 7, 11) {
foreach my $p (2, 3, 5) {
if (Math::GMPz::Rmpz_divisible_ui_p($z, $p)) {
return __PACKAGE__->_set_uint($p);
}
}

state $bounds = [map { __PACKAGE__->_set_uint($_) } (1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8)];

foreach my $k (@$bounds) {

my $factors = $_[0]->trial_factor($k);

if (@$factors > 1) {
return $factors->[0];
}
}

my $nstr = (
Math::GMPz::Rmpz_fits_ulong_p($z)
? Math::GMPz::Rmpz_get_ui($z)
Expand All @@ -9617,15 +9628,6 @@ package Sidef::Types::Number::Number {
return bless \$z;
}

foreach my $k (1e4, 1e6, 1e7) {

my @f = Math::Prime::Util::GMP::trial_factor($nstr, $k);

if (@f > 1) {
return __PACKAGE__->_set_uint($f[0]);
}
}

my @f = Math::Prime::Util::GMP::factor($nstr);

__PACKAGE__->_set_str('int', $f[0]);
Expand Down

0 comments on commit 66200dc

Please sign in to comment.