Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. /**
  2. CalcGCD finds the Greatest Common Divisor g of two numbers a and b
  3. using Euclid's algorithm
  4. and then uses that to find the Least Common Multiple as a*b/g
  5. It also counts the number of steps required by Euclid's algorithm.
  6. */
  7. public class CalcGCD{
  8. public static void main(String[] args){
  9. System.out.println("Enter two whole numbers, one per line: ");
  10. long num1 = TextIO.getlnLong();
  11. long num2 = TextIO.getlnLong();
  12.  
  13. long n = num1;
  14. long m = num2;
  15. while (m>0){
  16. long tmp = n%m;
  17. n=m;
  18. m=tmp;
  19. }
  20. System.out.printf("The GCD of %d and %d is %d%n",num1,num2,n);
  21.  
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement