Greatest common divisor: Difference between revisions
Content added Content deleted
mNo edit summary |
No edit summary |
||
Line 4,393: | Line 4,393: | ||
end; |
end; |
||
end;</lang> |
end;</lang> |
||
=={{header|S-BASIC}}== |
|||
<lang basic> |
|||
rem - return p mod q |
|||
function mod(p, q = integer) = integer |
|||
end = p - q * (p / q) |
|||
rem - return greatest common divisor of x and y |
|||
function gcd(x, y = integer) = integer |
|||
var r, temp = integer |
|||
if x < y then |
|||
begin |
|||
temp = x |
|||
x = y |
|||
y = temp |
|||
end |
|||
r = mod(x, y) |
|||
while r <> 0 do |
|||
begin |
|||
x = y |
|||
y = r |
|||
r = mod(x, y) |
|||
end |
|||
end = y |
|||
rem - exercise the function |
|||
print "The GCD of 21 and 35 is"; gcd(21,35) |
|||
print "The GCD of 23 and 35 is"; gcd(23,35) |
|||
print "The GCD of 1071 and 1029 is"; gcd(1071, 1029) |
|||
print "The GCD of 3528 and 3780 is"; gcd(3528,3780) |
|||
end |
|||
</lang> |
|||
{{out}} |
|||
<pre>The GCD of 21 and 35 is 7 |
|||
The GCD of 23 and 35 is 1 |
|||
The GCD of 1071 and 1029 is 21 |
|||
The GCD of 3528 and 3780 is 252 |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |