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

Mixed-algorithm GCD (potential optimization) #296

Open
Rudxain opened this issue Apr 25, 2024 · 1 comment
Open

Mixed-algorithm GCD (potential optimization) #296

Rudxain opened this issue Apr 25, 2024 · 1 comment

Comments

@Rudxain
Copy link

Rudxain commented Apr 25, 2024

Similarly to how multiplication uses different algorithms depending on some conditions, GCD could be optimized by using more than 1 algorithm. It should look something like this:
https://github.com/Rudxain/EsoMath.js/blob/8c896b3804b40b8c0307a0a32c10264ff82a52b5/src/mod/factors.js#L52-L178

Note

Here's an alt link to the proof that GCDs of Mersennes are Mersennes themselves: https://math.stackexchange.com/a/7561 . Both links have different proofs.

Yes, I know, my code is an abomination.

Honestly, I don't even know if that JS impl is 100% correct , or if it's always faster than a simple Stein. Some explicit special-case branches (the guard clauses) may be unnecessary, as one (or more) of these 3 algos may implicitly handle them efficiently.

However, it uses Lehmer's algorithm, which is proven to be faster than Stein's for huge ints. IIRC, the Euclidean algorithm is also faster than Stein's, but only for small ints (~128bits?).

I'm willing to open a PR for this, but I need to "get familiar" with the codebase

@cuviper
Copy link
Member

cuviper commented Apr 25, 2024

It's definitely something we could experiment with!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants