Home Java SE Tips

Java Network
 Java Forums Java Blog

Most Visited Tips
Top Rated Tips
Statistics
 Registered Users: 4118 Java SE Tips: 614 Java ME Tips: 202 Java EE Tips: 183 Other API Tips: 779 Java Applications: 298 Java Libraries: 209 Java Games: 16 Book Reviews:

 Finding Greatest Common Divisor recursively
User Rating: / 224
PoorBest

In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers.

The greatest common divisor of a and b is written as gcd(a, b), or sometimes simply as (a, b). For example, gcd(12, 18) = 6, gcd(−4, 14) = 2 and gcd(5, 0) = 5. Two numbers are called coprime or relatively prime if their greatest common divisor equals 1. For example, 9 and 28 are relatively prime.

The code below shows how to implement gcd function recursively.

 ``` /**  * Return the greatest common divisor  */    public static long gcd(long a, long b) {      if (b==0)       return a;    else      return gcd(b, a % b);  } ```

### Related Tips

Page 1 of 0 ( 0 comments )

Name (required)

E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)

Comment Enable HTML code : Yes No