Skip to content

Commit

Permalink
Minor optimization in Number subfactorial(): cache log(tau) and compu…
Browse files Browse the repository at this point in the history
…te log(n) only once.
  • Loading branch information
trizen committed Jul 19, 2021
1 parent 6667622 commit 555398f
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7544,9 +7544,12 @@ package Sidef::Types::Number::Number {
my @roots;

if (HAS_PRIME_UTIL) {
Math::Prime::Util::forsetproduct(sub {
push @roots, Math::Prime::Util::GMP::chinese(@_);
}, @congruences);
Math::Prime::Util::forsetproduct(
sub {
push @roots, Math::Prime::Util::GMP::chinese(@_);
},
@congruences
);
}
else {
require Algorithm::Loops;
Expand Down Expand Up @@ -8205,7 +8208,7 @@ package Sidef::Types::Number::Number {

*RamanujanSum = \&ramanujan_sum;

sub subfactorial {
sub subfactorial { # OEIS: A000166
my ($x, $y) = @_;

my $m = _any2ui($$x) // goto &nan;
Expand All @@ -8221,8 +8224,10 @@ package Sidef::Types::Number::Number {

if ($n >= 40000) {

my $tau = 6.28318530717958647692528676655900576839433879875;
my $prec = 4 + CORE::int(($n * CORE::log($n) + CORE::log($tau * $n) / 2 - $n) / CORE::log(2));
state $logtau = CORE::log(6.28318530717958647692528676655900576839433879875);

my $logn = CORE::log($n);
my $prec = 4 + CORE::int(($n * $logn + ($logn + $logtau) / 2 - $n) / CORE::log(2));

Math::GMPz::Rmpz_fac_ui($z, $n);

Expand Down Expand Up @@ -18672,7 +18677,7 @@ package Sidef::Types::Number::Number {
}

Math::GMPz::Rmpz_sgn($n) >= 0
or return Sidef::Types::Bool::Bool::FALSE;
or return Sidef::Types::Bool::Bool::FALSE;

# Optimization for bases <= 62
if (!defined($k) or Math::GMPz::Rmpz_cmp_ui($k, 62) <= 0) {
Expand Down Expand Up @@ -18711,7 +18716,7 @@ package Sidef::Types::Number::Number {
$n = _any2mpz($$n) // goto &nan;

Math::GMPz::Rmpz_sgn($n) >= 0
or goto &nan;
or goto &nan;

my @d;

Expand Down

0 comments on commit 555398f

Please sign in to comment.