Greatest common divisor: Difference between revisions

Content added Content deleted
imported>Skywalk
(Purebasic code was wrong.)
Line 1,632: Line 1,632:
==={{header|PureBasic}}===
==={{header|PureBasic}}===
====Iterative====
====Iterative====
<syntaxhighlight lang="purebasic">Procedure GCD(x, y)
<syntaxhighlight lang="purebasic">
Import "" ;msvcrt.lib
Protected r
AbsI(Quad.q) As "_abs64"
While y <> 0
AbsL(Long.l) As "labs"
r = x % y
EndImport
x = y
Procedure.i GCD(u.i, v.i)
y = r
Protected.i t
While v <> 0
t = v
v = u % v
u = t
Wend
Wend
ProcedureReturn y
ProcedureReturn AbsI(u) ; Avoid float conversion with Abs(u).
EndProcedure</syntaxhighlight>
EndProcedure
Debug GCD(18, 12) ; 6

Debug GCD(1071, 1029) ; 21
====Recursive====
Debug GCD(3528, -3780) ; 252
<syntaxhighlight lang="purebasic">Procedure GCD(x, y)
</syntaxhighlight>
Protected r
r = x % y
If (r > 0)
y = GCD(y, r)
EndIf
ProcedureReturn y
EndProcedure</syntaxhighlight>


==={{header|QuickBASIC}}===
==={{header|QuickBASIC}}===