ExtendedGCD
The GCD of the arguments together with Bézout coefficients x₁, x₂, … such that a₁·x₁ + a₂·x₂ + … = GCD(a₁, a₂, …).
Extended Euclidean algorithmWikidataQ1362750Wolfram LanguageExtendedGCD!Rosetta CodeModular inverseExtendedGCD(a, b) together with Bézout coefficients satisfying .xgcd(a, b)Oscarcollect(gcdx(ZZ(a), ZZ(b)))Juliacollect(gcdx(ZZ(a), ZZ(b)))Rustextended_gcd(a, b)ExtendedGCD(a, b, c, …) of any number of arguments, together with one Bézout coefficient per argument, folded pairwise from the two-argument case.Typed signatures
| Type | Package | Applies |
|---|---|---|
(integer, integer) -> tuple<integer, integer, integer> | compute-engine | always |
(number, number*) -> tuple | number-theoryreplaces compute-engine | always |
Details
- Implements the extended Euclidean algorithm, the standard way to compute modular inverses. See PowerMod.
- The coefficients are not unique; the algorithm returns one particular solution, by folding the two-argument case pairwise across the arguments left to right.
- When , the coefficients reduce to .
Examples
30 more kept as data, checked against the oraclesScope #
About
| Examples | 43 |
|---|---|
| Checked against | Wolfram Language 3 agree40 differSageMath 13 agree26 differSymPy 30 agree9 differmathlib4 11 agree1 differ2 errorOscar 11 agreeJulia 12 agree26 errorRust 12 agree1 differ1 errormpmath 1 agree |
| Crosswalk | WikipediaWikidataWolfram LanguageSageMathOscarJuliaRustRosetta Code |
Implementation
oscarpackages/oracle/src/mappings.tswolframpackages/oracle/src/mappings.tsShape difference, not a value one: Wolfram's ExtendedGCD returns {g, {s, t}}, nested; ours returns the flat Tuple(g, s, t) compute-engine's own Tuple head calls for. The Bezout coefficients also aren't unique, so an occasional case lands on another valid (s, t) pair than Wolfram's Euclid would pick; ours still satisfies sa + tb = g.
sympypackages/oracle/src/mappings.tsSymPy's gcdex returns (s, t, g), a different order from our (g, s, t); the template only reorders the three. The Bezout pair is whichever the Euclidean algorithm reaches, and its sign convention for a negative argument is SymPy's own.
sympypackages/oracle/src/mappings.tsSymPy's gcdex takes two arguments and has no n-ary form; folding it pairwise would test our fold against itself, so the three-argument call is left unmapped.
sagepackages/oracle/src/mappings.tsjuliapackages/oracle/src/mappings.tsrustpackages/oracle/src/mappings.tsBézout coefficients are the algorithm's choice among many, so a pair can differ from ours.
mathlib4packages/oracle/src/mappings.tsInt.gcd with its Bezout coefficients Int.gcdA and Int.gcdB, as a flat list [g, s, t] (a Lean tuple prints nested); the pair is whichever the Euclidean algorithm reaches, which need not be the one ours picks.
See also: GCD