I performed an implementation of the Euclidean algorithm in Java to find the Greatest Common Divisor (GCD) of two given numbers.
For the most part, my program works fine, I checked it with several random sets of numbers, although I found in one case (that I know) that it gives the wrong output, which for the following combination of numbers:
Enter the integer a: 8965 Enter the integer b: 55
The output of the program should be 55, although this is not so. The following is displayed:
gcd = 1 Lead time: 0.005747ms.
I'm not sure why this particular combination of numbers causes a problem, since it works great for other numbers, for example, here are the results for another set of numbers:
Enter an integer a: 15000
Enter the integer b: 5325
gcd = 75
Lead time: 0.007389ms.
import java.util.Scanner; public class EuclideanAlgorithm { public static void main (String [] args) { int a, b; try(Scanner sc = new Scanner(System.in);) { System.out.print("Enter integer a: "); a = sc.nextInt(); System.out.print("Enter integer b: "); b = sc.nextInt(); } long start = System.nanoTime(); int answer = EuclideanAlgorithm(a, b); long stop = System.nanoTime(); System.out.println("gcd = " + answer); System.out.println("Execution time: " + ((stop - start) / 1e+6) + "ms."); } public EuclideanAlgorithm() {};
James source share