Here you can find the source of gcd(int num1, int num2)
Parameter | Description |
---|---|
num1 | The first number |
num2 | The second number |
public static int gcd(int num1, int num2)
//package com.java2s; //License from project: LGPL public class Main { /**/* www.j av a 2 s . c o m*/ * Returns the greatest common divisor of two integers. * * @param num1 The first number * @param num2 The second number * @return The greatest common divisor */ public static int gcd(int num1, int num2) { if (num1 < 0) num1 = -num1; if (num2 < 0) num2 = -num2; return privGCD(num1, num2); } /** * Returns the greatest common divisor of two non-negative integers. * * @param num1 The first number; must be non-negative * @param num2 The second number; must be non-negative * @return The greatest common divisor */ private static int privGCD(int num1, int num2) { if (num1 == 0 && num2 == 0) return 1; if (num1 == 0) return num2; if (num2 == 0) return num1; return num1 > num2 ? privGCD(num1 % num2, num2) : privGCD(num1, num2 % num1); } }