Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- CalcGCD finds the Greatest Common Divisor g of two numbers a and b
- using Euclid's algorithm
- and then uses that to find the Least Common Multiple as a*b/g
- It also counts the number of steps required by Euclid's algorithm.
- */
- public class CalcGCD{
- public static void main(String[] args){
- System.out.println("Enter two whole numbers, one per line: ");
- long num1 = TextIO.getlnLong();
- long num2 = TextIO.getlnLong();
- long n = num1;
- long m = num2;
- while (m>0){
- long tmp = n%m;
- n=m;
- m=tmp;
- }
- System.out.printf("The GCD of %d and %d is %d%n",num1,num2,n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement