Can this Python code also be written in Java code? :
def gcd(a, b):
# Return greatest common divisor using Euclid's Algorithm.
while b:
a, b = b, a % b
return a
print (gcd(210, 45))
This is what I've so far in Java code:
private static int gcd(int p, int q) {
// Return greatest common divisor using Euclid's Algorithm.
int temp;
while (q != 0) {
temp = q;
q = p % q;
p = temp;
}
return p;
}
System.out.println(gcd(210, 45));
As you can see the Java code uses 3 variables whereas the Python code only uses 2 variables. I also want to use only 2 variables in the Java code and I want to keep the while loop and I don't want to use recursion.
Also why does Java need 1 more variable than the Python code? Except for the fact that the Python code uses a tuple.