Skip to content

Add gcd & abs #75

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

Merged
merged 2 commits into from
Jan 23, 2022
Merged

Add gcd & abs #75

merged 2 commits into from
Jan 23, 2022

Conversation

konsumlamm
Copy link
Contributor

Subsumes #66 (see #66 (comment)).

gcd is implemented using the binary GCD algorithm.

func gcd*(a, b: BigInt): BigInt =
## Returns the greatest common divisor (GCD) of `a` and `b`.
runnableExamples:
assert gcd(54.initBigInt, 24.initBigInt) == 6.initBigInt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if it's preferable that runnableExamples are fast to execute, but one funny example (to showcase multiple precision integers too) would be :

let two = 2.initBigInt
assert gcd(two.pow(54) - 1, two.pow(24) - 1) == two.pow(6) - 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it doesn't matter if runnableExamples are fast to execute, as they're only run when generating documentation. I'm not entirely convinced that this particular example is suited for the runnableExamples, but ofc we can add it to the test suite.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the test and rebase @konsumlamm ?
Same as with the modular exponentiation, we will also get more conflicts if we let the PR pending.
May you then merge this PR @narimiran ?

Copy link
Contributor

@dlesnoff dlesnoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. Might be improved in the future by other methods designed for multiple-precision arithmetic like Lehmer's (Knuth 4.5.2).

@narimiran narimiran merged commit 6d8c051 into nim-lang:master Jan 23, 2022
@konsumlamm konsumlamm deleted the gcd branch January 24, 2022 00:26
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

Successfully merging this pull request may close these issues.

3 participants