Greatest common divisor: Difference between revisions

Content added Content deleted
(Added Gambas)
No edit summary
Line 1,562: Line 1,562:


HandleEvents</syntaxhighlight>
HandleEvents</syntaxhighlight>

==={{header|Gambas}}===
====Iterative solution====
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim a As Long = 111111111111111
Dim b As Long = 11111
Print "\nGCD("; a; ", "; b; ") = "; gcd(a, b)
Print "\nGCD("; a; ", 111) = "; gcd(a, 111)
End

Function gcd(x As Long, y As Long) As Long

Dim t As Long
While y
t = y
y = x Mod y
x = t
Wend
Return x

End Function</syntaxhighlight>
{{out}}
<pre>GCD(111111111111111, 11111) = 11111
GCD(111111111111111, 111) = 111</pre>

====Recursive solution====
<syntaxhighlight lang="vbnet">Function gcdp(a As Long, b As Long) As Long
If b = 0 Then Return a
Return gcdp(b, a Mod b)
End Function
Function gcd(a As Long, b As Long) As Long
Return gcdp(Abs(a), Abs(b))
End Function</syntaxhighlight>


==={{header|GFA Basic}}===
==={{header|GFA Basic}}===
Line 3,467: Line 3,430:


=={{header|FutureBASIC}}==
=={{header|FutureBASIC}}==
This is a nearly-trivial 6-line function, so we've dressed it up a bit to show how easily FutureBASIC builds a full, interactive application. In FB, when you complete your code and hit RUN, the built app can open in as little as 3 seconds.
This is a nearly-trivial 6-line function, so we've dressed it up a bit to show how easily FutureBASIC builds a full, interactive application. In FB, when you complete your code and hit RUN, the built app can open in as little as 3 seconds.

[[New_app_in_finder.png ]]
[[File:New_app_in_finder.png|200px]]

It also appears in the dock, where you can run it again with a single click.
It also appears in the dock, where you can run it again with a single click.

[[New_app_in_finder.png]]
[[File:New_app_in_Mac_dock.png|45px]]

The running app looks like this—we added a button to generate random examples to
The running app looks like this—we added a button to generate random examples to
process, and an interactive design that instantly responds to each change in an input field.
process, and an interactive design that instantly responds to each change in an input field.


[[Running app.png]]
[[File:Running_app.png|280px]]


'''CODE'''
'''CODE'''
<syntaxhighlight>
<syntaxhighlight>


begin enum 1 // Object tags
begin enum 1 // Object tags
_fldA
_fldA
_ansA
_ansA
_fldB
_fldB
_ansB
_ansB
_rand
_rand
end enum
end enum


void local fn BuildMacInterface //15-line GUI
void local fn BuildMacInterface //15-line GUI
window 1, @"Greatest Common Divisor", ( 0, 0, 380, 130 ), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
window 1, @"Greatest Common Divisor", ( 0, 0, 380, 130 ), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
textfield _fldA,,, ( 20, 89, 156, 21 )
textfield _fldA,,, ( 20, 89, 156, 21 )
ControlSetAlignment( _fldA, NSTextAlignmentRight )
ControlSetAlignment( _fldA, NSTextAlignmentRight )
ControlSetFormat( _fldA, @"0123456789", Yes, 18, 0 )
ControlSetFormat( _fldA, @"0123456789", Yes, 18, 0 )
textfield _fldB,,, ( 20, 57, 156, 24 )
textfield _fldB,,, ( 20, 57, 156, 24 )
ControlSetAlignment( _fldB, NSTextAlignmentRight )
ControlSetAlignment( _fldB, NSTextAlignmentRight )
ControlSetFormat( _fldB, @"0123456789", Yes, 18, 0 )
ControlSetFormat( _fldB, @"0123456789", Yes, 18, 0 )
textlabel _ansA, @"= ", ( 182, 91, 185, 16 )
textlabel _ansA, @"= ", ( 182, 91, 185, 16 )
textlabel _ansB, @"= ", ( 182, 62, 185, 16 )
textlabel _ansB, @"= ", ( 182, 62, 185, 16 )
button _rand,,,@"Random demo", ( 129, 13, 122, 32 )
button _rand,,,@"Random demo", ( 129, 13, 122, 32 )
menu 1,,, @"File" : menu 1,0,, @"Close", @"w" : MenuItemSetAction(1,0,@"performClose:")
menu 1,,, @"File" : menu 1,0,, @"Close", @"w" : MenuItemSetAction(1,0,@"performClose:")
editmenu 2
editmenu 2
WindowMakeFirstResponder( 1, _fldA )
WindowMakeFirstResponder( 1, _fldA )
end fn
end fn


local fn GCD( a as long, b as long ) as long //the requested function
local fn GCD( a as long, b as long ) as long //the requested function
while b
while b
long c = a mod b
long c = a mod b
a = b : b = c
a = b : b = c
wend
wend
end fn = a
end fn = a


void local fn DoDialog( ev as Long, tag as long ) //This makes it interactive
void local fn DoDialog( ev as Long, tag as long ) //This makes it interactive
long a, b, c
long a, b, c
select ev
select ev
case _textFieldDidchange //Find GCD of edit fields' contents
case _textFieldDidchange //Find GCD of edit fields' contents
a = fn ControlIntegerValue( _fldA )
a = fn ControlIntegerValue( _fldA )
b = fn ControlIntegerValue( _fldB )
b = fn ControlIntegerValue( _fldB )
if a + b == 0 then textlabel _ansA, @"=" : textlabel _ansB, @"=" : exit fn
if a + b == 0 then textlabel _ansA, @"=" : textlabel _ansB, @"=" : exit fn
c = fn GCD( a, b )
c = fn GCD( a, b )
textlabel _ansA, fn stringwithformat(@"= %ld x %ld", c, a / c )
textlabel _ansA, fn stringwithformat(@"= %ld x %ld", c, a / c )
textlabel _ansB, fn stringwithformat(@"= %ld x %ld", c, b / c )
textlabel _ansB, fn stringwithformat(@"= %ld x %ld", c, b / c )
case _btnclick //Fill edit fields with random content, then process
case _btnclick //Fill edit fields with random content, then process
select tag
select tag
case _rand
case _rand
c = rnd(65536)
c = rnd(65536)
textfield _fldA,,str( c * rnd(65536) )
textfield _fldA,,str( c * rnd(65536) )
textfield _fldB,,str( c * rnd(65536) )
textfield _fldB,,str( c * rnd(65536) )
fn DoDialog( _textFieldDidchange, 0 )
fn DoDialog( _textFieldDidchange, 0 )
end select
end select
case _windowWillClose : end
case _windowWillClose : end
end select
end select
end fn
end fn

fn BuildMacInterface
on dialog fn doDialog
handleevents


fn BuildMacInterface
on dialog fn doDialog
handleevents
</syntaxhighlight>
</syntaxhighlight>


'''OUTPUT'''
'''OUTPUT'''
{{out}}
{{out}}
[[File:GCD_of_814997010 and 4644003.png|300px]] [[File:GCD_of_1234567890 and 9876543210.png|300px]] [[File:GCD_of_51015 and 15051.png|300px]] [[File:GCD_of_1881 and 8118.png|300px]] [[File:GCD_of_42426466 and 2445968527.png|300px]] [[File:GCD_of_123 and empty field.png|300px]]
<pre>
[[GCD of 814997010 and 4644003.png]] [[GCD of 1234567890 and 9876543210.png]] [[GCD of 51015 and 15051.png]] [[GCD of 1881 and 8118.png]] [[GCD of 42426466 and 2445968527.png ]] [[GCD of 123 and empty field.png]]
</pre>