Greatest common divisor: Difference between revisions

m
→‎{{header|REXX}}: compressed the source slightly, added more comments. -- ~~~~
(Improved D entry)
m (→‎{{header|REXX}}: compressed the source slightly, added more comments. -- ~~~~)
Line 1,803:
=={{header|REXX}}==
<lang rexx>
/*REXX program to find GCD (greatest common divisor) of two integers. */
 
say 'the GCD of 7 and 21 is' gcd( 7,21)
Line 1,809:
say 'the GCD of 15 and 10 is' gcd(15,10)
say 'the GCD of 51 and 99 is' gcd(51,99)
 
exit
 
/*─────────────────────────────────────GCD subroutine───────────────────*/
gcd: procedure; parse arg x,y /*find greatest common divisor. */
if x=0 | y=0 then return 0 /*test a couple of special cases.*/
x=abs(x); y=abs(y) /*make X & Y /*insure X number isnumbers positive.*/
 
do until _==0
gcd: procedure; parse arg x,y /*find greatest common divisor.*/
if x=0 | y _=0x//y; then returnx=y; 0 y=_
end
x=abs(x) /*insure X number is positive.*/
y=abs(y) /*insure Y number is positive.*/
 
do until _==0
_=x//y
x=y
y=_
end
 
return x
</lang>
Output:
<pre style="height:30ex20ex;overflow:scroll">
the GCD of 7 and 21 is 7
the GCD of 4 and 7 is 1