Java Utililty Methods gcd

List of utility methods to do gcd

  1. HOME
  2. Java
  3. g
  4. gcd

Description

The list of methods to do gcd are organized into topic(s).

Method

double gcd(double a, double b)
gcd
double epsilon = 1.0 / (1L << 32);
while (a > epsilon) {
 if (a < b) {
 double swap = a;
 a = b;
 b = swap;
 a = a % b;
...
double GCD(double a, double b)
GCD
return b == 0 ? a : GCD(b, a % b);
int gcd(final int a, final int b)
Determines the greatest common divisor of two integers.
if (b == 0) {
 return a;
} else {
 return gcd(b, a % b);
int gcd(final int p, final int q)

Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations.

int u = p;
int v = q;
if ((u == 0) || (v == 0)) {
 if ((u == Integer.MIN_VALUE) || (v == Integer.MIN_VALUE)) {
 return 0;
 return (Math.abs(u) + Math.abs(v));
if (u > 0) {
 u = -u;
if (v > 0) {
 v = -v;
int k = 0;
while ((u & 1) == 0 && (v & 1) == 0 && k < 31) { 
 u /= 2;
 v /= 2;
 k++; 
if (k == 31) {
 return 0;
int t = ((u & 1) == 1) ? v : -(u / 2);
do {
 while ((t & 1) == 0) { 
 t /= 2; 
 if (t > 0) {
 u = -t;
 } else {
 v = t;
 t = (v - u) / 2;
} while (t != 0);
return -u * (1 << k); 
int gcd(int a, int b)
gcd
while (b != 0) {
 int h = a % b;
 a = b;
 b = h;
return a;
int gcd(int a, int b)
gcd
return b == 0 ? a : gcd(b, a % b);
int gcd(int a, int b)
gcd
if (a > b) {
 int tmp = a;
 a = b;
 b = tmp;
if (a == 0) {
 return 0;
if (b % a == 0) {
 return a;
return gcd(b % a, a);
int gcd(int a, int b)
Returns the greatest common divisor between a and b.
return (b == 0) ? a : gcd(b, a % b);
int gcd(int a, int b)
Returns the Greatest Common Division using a recursive algorithm based on the Euclides theorem.
int min = Math.min(a, b);
int max = Math.max(a, b);
int r = max % min;
if (r == 0) {
 return min;
} else {
 return gcd(min, r);
int gcd(int a, int b)
Computes the greatest common divisor (GCD) of two integers.
a = Math.abs(a);
b = Math.abs(b);
while (a > 0) {
 int c = b % a;
 b = a;
 a = c;
return b;
...


AltStyle によって変換されたページ (->オリジナル) /