Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

znorder(a,n): optimization for large n with many small factors. #38

Closed
trizen opened this issue Mar 17, 2023 · 1 comment
Closed

znorder(a,n): optimization for large n with many small factors. #38

trizen opened this issue Mar 17, 2023 · 1 comment

Comments

@trizen
Copy link
Contributor

trizen commented Mar 17, 2023

Currently, when n is large and has many small prime factors, znorder(a,n) is quite slow, but it can be made much faster by using the following identity:

znorder($a,$n) == lcm(map { znorder($a, powint($_->[0], $_->[1])) } factor_exp($n));

Example:

use 5.014;
use Math::Prime::Util::GMP qw(:all);
use Time::HiRes qw(gettimeofday tv_interval);

my $a = 10007;
my $n = factorial(1000);

my $t0 = [gettimeofday];
my $x  = znorder($a, $n);
say "znorder(a,n) took: ", tv_interval($t0), ' seconds';

my $t1 = [gettimeofday];
my %factors; ++$factors{$_} for factor($n);
my $y  = lcm(map { znorder($a, powint($_, $factors{$_})) } keys %factors);
say "lcm(znorder(a, p^e)) took: ", tv_interval($t1), ' seconds';

$x eq $y or die "error: x != y";

Output:

znorder(a,n) took: 11.648454 seconds
lcm(znorder(a, p^e)) took: 0.006773 seconds
@danaj
Copy link
Owner

danaj commented Jun 15, 2023

Was:

  $ make >/dev/null && perl -Iblib/lib -Iblib/arch ~/tmp/znorder-lcm.pl
  znorder(a,n) took: 6.134748 seconds
  lcm(znorder(a, p^e)) took: 0.00322 seconds

Now (code not commited yet)

  $ make >/dev/null && perl -Iblib/lib -Iblib/arch ~/tmp/znorder-lcm.pl
  znorder(a,n) took: 0.002527 seconds
  lcm(znorder(a, p^e)) took: 0.00324 seconds

@danaj danaj closed this as completed in b59e5b7 Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants