Java gcd gcdUsingRecursion(long a, long b)

Here you can find the source of gcdUsingRecursion(long a, long b)

Description

Calculate greatest common divisor of two numbers using recursion.

License

Apache License

Parameter

Parameter Description
a Long integer
b Long integer

Return

greatest common divisor of a and b

Declaration

public static long gcdUsingRecursion(long a, long b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w  ww.  j  a v  a2s  . c  o  m
     * Calculate greatest common divisor of two numbers using recursion.
     * <p>
     * Time complexity O(log(a+b))
     * <br>
     * @param a Long integer
     * @param b Long integer
     * @return greatest common divisor of a and b
     */
    public static long gcdUsingRecursion(long a, long b) {
        a = Math.abs(a);
        b = Math.abs(b);
        return a == 0 ? b : gcdUsingRecursion(b % a, a);
    }
}

Related

  1. gcdMultiple(int[] nums)
  2. gcdPositive(int a, int b)
  3. gcdPositive(int... args)
  4. gcdRec(int a, int b)
  5. gcdUsingEuclides(long x, long y)