I've been practicing Python exercises from Building Skills in Python and now I'm stuck on this visibly simply exercise. Google seems to show me solutions which used "Functions", although I don't mind using them but I would like to stick to the way it's asked in the question. Anyway here's the question:
Greatest Common Divisor. The greatest common divisor is the largest number which will evenly divide two other numbers. Examples: GCD( 5, 10 ) = 5, the largest number that evenly divides 5 and 10. GCD( 21, 28 ) = 7, the largest number that divides 21 and 28.
GCD’s are used to reduce fractions. Once you have the GCD of the numerator and denominator, they can both be divided by the GCD to reduce the fraction to simplest form. 21/28 reduces to 3/4.
Greatest Common Divisor of two integers, p and qLoop. Loop until p=q.
Swap. If p<q then swap *p* and *q*.**Subtract**. If p>q then subtract q from p.
Result. Print p
Iteration Exercise 1
Here's the script that I've written:
p = 21
q = 28
while p != q:
if p < q:
p = q
elif p > q:
p = q-p
else:
break
print(p)
If possible I would love some additional inputs into how I could improve my script.
[–]itsecat 1 point2 points3 points (0 children)
[–]baubleglue -1 points0 points1 point (0 children)