QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Greatest Common Divisor

Calculates the greatest common divisor (gcd) of two numbers

  • math
  • number
  • greatest-common-devisor
  • gcd
  • euclidean-algorithm
  • division
  • number-theory
  • algebra

Install

npx shadcn@latest add https://quicksnip.dev/r/greatest-common-divisor.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function gcd(a, b) {
    while (b !== 0) {
        let temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

Usage

int a = 16;
int b = 12;
System.out.println(gcd(a, b)); // 4