Greatest common divisor: Difference between revisions
Content added Content deleted
m (→{{header|K}}: Fix highlight) |
(New entry for Greatest Common Divisor in FutureBASIC, demonstrating ease of building a full, interactive application in FB.) |
||
Line 3,428: | Line 3,428: | ||
_gcd( abs(a), abs(b) )</syntaxhighlight> |
_gcd( abs(a), abs(b) )</syntaxhighlight> |
||
=={{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. |
|||
[[New_app_in_finder.png ]] |
|||
It also appears in the dock, where you can run it again with a single click. |
|||
[[New_app_in_finder.png]] |
|||
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. |
|||
[[Running app.png]] |
|||
'''CODE''' |
|||
<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 |
|||
fn BuildMacInterface |
|||
on dialog fn doDialog |
|||
handleevents |
|||
</syntaxhighlight> |
|||
'''OUTPUT''' |
|||
{{out}} |
|||
<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> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |