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. |
||
[[ |
[[File:Running_app.png|280px]] |
||
'''CODE''' |
'''CODE''' |
||
<syntaxhighlight> |
<syntaxhighlight> |
||
begin enum 1 // Object tags |
|||
_fldA |
|||
_ansA |
|||
_fldB |
|||
_ansB |
|||
_rand |
|||
end enum |
|||
void local fn BuildMacInterface //15-line GUI |
|||
window 1, @"Greatest Common Divisor", ( 0, 0, 380, 130 ), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable |
|||
textfield _fldA,,, ( 20, 89, 156, 21 ) |
|||
ControlSetAlignment( _fldA, NSTextAlignmentRight ) |
|||
ControlSetFormat( _fldA, @"0123456789", Yes, 18, 0 ) |
|||
textfield _fldB,,, ( 20, 57, 156, 24 ) |
|||
ControlSetAlignment( _fldB, NSTextAlignmentRight ) |
|||
ControlSetFormat( _fldB, @"0123456789", Yes, 18, 0 ) |
|||
textlabel _ansA, @"= ", ( 182, 91, 185, 16 ) |
|||
textlabel _ansB, @"= ", ( 182, 62, 185, 16 ) |
|||
button _rand,,,@"Random demo", ( 129, 13, 122, 32 ) |
|||
menu 1,,, @"File" : menu 1,0,, @"Close", @"w" : MenuItemSetAction(1,0,@"performClose:") |
|||
editmenu 2 |
|||
WindowMakeFirstResponder( 1, _fldA ) |
|||
end fn |
|||
local fn GCD( a as long, b as long ) as long //the requested function |
|||
while b |
|||
long c = a mod b |
|||
a = b : b = c |
|||
wend |
|||
end fn = a |
|||
void local fn DoDialog( ev as Long, tag as long ) //This makes it interactive |
|||
long a, b, c |
|||
select ev |
|||
case _textFieldDidchange //Find GCD of edit fields' contents |
|||
a = fn ControlIntegerValue( _fldA ) |
|||
b = fn ControlIntegerValue( _fldB ) |
|||
if a + b == 0 then textlabel _ansA, @"=" : textlabel _ansB, @"=" : exit fn |
|||
c = fn GCD( a, b ) |
|||
textlabel _ansA, fn stringwithformat(@"= %ld x %ld", c, a / c ) |
|||
textlabel _ansB, fn stringwithformat(@"= %ld x %ld", c, b / c ) |
|||
case _btnclick //Fill edit fields with random content, then process |
|||
select tag |
|||
case _rand |
|||
c = rnd(65536) |
|||
textfield _fldA,,str( c * rnd(65536) ) |
|||
textfield _fldB,,str( c * rnd(65536) ) |
|||
fn DoDialog( _textFieldDidchange, 0 ) |
|||
end select |
|||
case _windowWillClose : end |
|||
end select |
|||
end fn |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</syntaxhighlight> |
</syntaxhighlight> |
||
'''OUTPUT''' |
'''OUTPUT''' |
||
{{out}} |
{{out}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
</pre> |
|||