Skip to content

GCD ​

The greatest common divisor of the arguments.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaGreatest common divisorMathWorldGreatestCommonDivisorWikidataQ131752Encyclopedia of MathematicsGreatest_common_divisornLabgreatest common divisorBritannicatopic/greatest-common-divisorFungrimGCD✓Fungrim entryWolfram LanguageGCD✓SageMathgcd([$*,])✓Rosetta CodeGreatest common divisor
GCD(a, b, …)greatest common divisor of two or more integers.
Typed signatures
TypePackageApplies
(any*) -> numbercompute-enginealways
(any*) -> numbernumber-theoryreplaces compute-enginealways

Domain: Number theory

Details
  • Also called the greatest common factor: the largest positive integer dividing every argument.
  • Paired with LCM by gcd⁡(a,b)⋅lcm⁡(a,b)=ab\gcd(a,b)\cdot\operatorname{lcm}(a,b)=ab.
  • compute-engine discards signs before computing, so gcd⁡(−a,b)=gcd⁡(a,b)\gcd(-a,b)=\gcd(a,b).
  • gcd⁡(0,n)=n\gcd(0,n)=n since every integer divides 0; with no arguments at all compute-engine returns 0, GCD's identity element.
  • Extends to rationals: gcd⁡(p1/q1,… )=gcd⁡(p1,… )/lcm⁡(q1,… )\gcd(p_1/q_1, \dots) = \gcd(p_1, \dots)/\operatorname{lcm}(q_1, \dots).

Examples

26 more kept as data, checked against the oracles
Basic #
live ↗
Ingcd⁡(12,18)\gcd(12, 18)Out66
live ↗
Ingcd⁡(24,36)\gcd(24, 36)Out1212

GCD is variadic: it accepts any number of arguments

live ↗
Ingcd⁡(20,30,45)\gcd(20, 30, 45)Out55
live ↗
Ingcd⁡(2,6,10)\gcd(2, 6, 10)Out22
Scope #

Over the rationals: the gcd of the numerators over the lcm of the denominators

live ↗
Ingcd⁡(13,25,37)\gcd(\frac{1}{3}, \frac{2}{5}, \frac{3}{7})Out1105\frac{1}{105}

Gaussian integers: the associate in the first quadrant

live ↗
Ingcd⁡(3+i,1+3i)\gcd(3+i, 1+3i)Out1+i1+i

Gaussian integers: −3+2i=i(2+3i)-3+2i=i(2+3i) and 10+15i=5(2+3i)10+15i=5(2+3i)

live ↗
Ingcd⁡(−3+2i,10+15i)\gcd(-3+2i, 10+15i)Out2+3i2+3i

Gaussian rationals: gcd⁡(15+10i, 3+2i)/lcm⁡(3,2)\gcd(15+10i,\,3+2i)/\operatorname{lcm}(3,2)

live ↗
Ingcd⁡(5+103i,32+i)\gcd(5+\frac{10}{3}i, \frac{3}{2}+i)Out12+13i\frac{1}{2}+\frac{1}{3}i

Big integers: gcd⁡(20!, 10100+3)=7\gcd(20!,\,10^{100}+3)=7, exact past where a double rounds

live ↗
Ingcd⁡(20!,10100+3)\gcd(20!, 10^{100}+3)Out77

Listable: a single list argument threads element-wise against the rest

live ↗
Ingcd⁡(12,[3,7,40])\gcd(12, \bigl\lbrack3, 7, 40\bigr\rbrack)Out[3,1,4]\bigl\lbrack3, 1, 4\bigr\rbrack
live ↗
InReduceConditions(gcd⁡(a2−1,12)<gcd⁡(a,12)∧−10<a<10,a,Z)\mathrm{ReduceConditions}(\gcd(a^2-1, 12)\lt\gcd(a, 12)∧-10\lt a\lt10, a, ℤ)OutReduceConditions(gcd⁡(12,a2−1)<gcd⁡(12,a)∧−10<a<10,a,Z)\mathrm{ReduceConditions}(\gcd(12, a^2-1)\lt\gcd(12, a)∧-10\lt a\lt10, a, ℤ)
Applications #

Reduce 12/1812/18 to lowest terms by dividing out the GCD

live ↗
In12gcd⁡(12,18)\frac{12}{\gcd(12, 18)}Out22

Greatest common divisor of two integers

live ↗
Ingcd⁡(14,105)\gcd(14, 105)Out77

When one divides the other, the gcd is the smaller

live ↗
Ingcd⁡(24,12)\gcd(24, 12)Out1212

Shared factor 3

live ↗
Ingcd⁡(6,21)\gcd(6, 21)Out33

The gcd of 6 and 3 is itself divisible by 3

live ↗
Ingcd⁡(6,3) mod 3=0\gcd(6, 3)\bmod3=0OutTrue\mathrm{True}
Properties #

Commutative: gcd⁡(a,b)=gcd⁡(b,a)\gcd(a,b)=\gcd(b,a)

live ↗
Ingcd⁡(12,18)=gcd⁡(18,12)\gcd(12, 18)=\gcd(18, 12)OutTrue\mathrm{True}

gcd⁡(a,b)⋅lcm(a,b)=ab\gcd(a,b)\cdot\mathrm{lcm}(a,b)=ab. See LCM

live ↗
Ingcd⁡(12,18)lcm⁡(12,18)=12×18\gcd(12, 18)\operatorname{lcm}(12, 18)=12×18OutTrue\mathrm{True}

gcd⁡(Fm,Fn)=Fgcd⁡(m,n)\gcd(F_m,F_n)=F_{\gcd(m,n)}: here F6=8F_6=8. See Fibonacci

live ↗
Ingcd⁡(Fibonacci(12),Fibonacci(18))\gcd(\mathrm{Fibonacci}(12), \mathrm{Fibonacci}(18))Out88

Takes any number of arguments; distinct primes give 1

live ↗
Ingcd⁡(7,11,29)\gcd(7, 11, 29)Out11

Coprime integers have gcd 1

live ↗
Ingcd⁡(5,6)=1\gcd(5, 6)=1OutTrue\mathrm{True}
Possible issues #

A single argument is returned unchanged

live ↗
Ingcd⁡(4)\gcd(4)Out44

With no arguments compute-engine returns 0, the identity element

live ↗
Ingcd⁡()\gcd()Out00

0 is absorbed: every integer divides it, so gcd⁡(0,n)=n\gcd(0,n)=n

live ↗
Ingcd⁡(0,5)\gcd(0, 5)Out55

Signs are discarded before computing the GCD

live ↗
Ingcd⁡(−12,9,57)\gcd(-12, 9, 57)Out33

Lists of one length thread element-wise, as in Wolfram: {gcd⁡(2,6),gcd⁡(4,8)}\{\gcd(2,6),\gcd(4,8)\}

live ↗
Ingcd⁡([2,4],[6,8])\gcd(\bigl\lbrack2, 4\bigr\rbrack, \bigl\lbrack6, 8\bigr\rbrack)Out[2,4]\bigl\lbrack2, 4\bigr\rbrack

gcd⁡(0,0)=0\gcd(0,0)=0 by convention

live ↗
Ingcd⁡(0,0)\gcd(0, 0)Out00

Symbolic arguments stay unevaluated

live ↗
Ingcd⁡(x,y)\gcd(x, y)Outgcd⁡(x,y)\gcd(x, y)

The result is never negative

live ↗
Ingcd⁡(−3,9)\gcd(-3, 9)Out33

An infinite argument has no divisors to take: the call stays unevaluated, as in Wolfram

live ↗
Ingcd⁡(∞,6)\gcd(∞, 6)Outgcd⁡(6,∞)\gcd(6, ∞)
About
Examples56
Checked againstWolfram Language 56 agreeSageMath 52 agree2 unevaluated1 errorSymPy 46 agree2 unevaluatedmathlib4 9 agree2 errorOscar 9 agree5 errorJulia 19 agree28 errorRust 17 agree2 error
CrosswalkWikipediaMathWorldWikidataEncyclopedia of MathematicsnLabBritannicaFungrimFungrim entry ×35Wolfram LanguageSageMath ×2SymPyOscarJuliaRustRosetta Code

Implementation

mappedwolframpackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts
mappedoscarpackages/oracle/src/mappings.ts
mappedjuliapackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts

See also: LCM, ExtendedGCD