Uncommons Maths is a java library with nice math utilities and random number generators. This requires the library uncommons-maths-1.2.2.jar to be in classpath. The following example shows using Maths.greatestCommonDivisor() API.
package com.bethecoder.tutorials.uncommons; import org.uncommons.maths.Maths; public class GCDTest { /** * @param args */ public static void main(String[] args) { long a = 24; long b = 32; long c = Maths.greatestCommonDivisor(a, b); System.out.println("GCD of (" + a + ", " + b + ") = " + c); a = 1729; b = 1331; c = Maths.greatestCommonDivisor(a, b); System.out.println("GCD of (" + a + ", " + b + ") = " + c); a = 123456; b = 4684; c = Maths.greatestCommonDivisor(a, b); System.out.println("GCD of (" + a + ", " + b + ") = " + c); } }
GCD of (24, 32) = 8 GCD of (1729, 1331) = 1 GCD of (123456, 4684) = 4