-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgcd.tea
24 lines (21 loc) · 992 Bytes
/
gcd.tea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/opt/TeaAgeSolutions/TeaScript/bin/TeaScript
/*
* SPDX-FileCopyrightText: Copyright (C) 2023 Florian Thake, <contact |at| tea-age.solutions>.
* SPDX-License-Identifier: MIT
* see included file MIT_LICENSE.txt
*/
// computes the gcd with a loop
// note: arg1,arg2 is the legacy form of passing arguments, args the new form. If none is there, 1 is used as default.
// Here is both present because of the long history (this was one of the first example file) and backward compatibility,
// (It still can be executed back to (unreleased) version 0.6.0)
def x1 := if( is_defined arg1 ) { +arg1 } else if( is_defined args ) { _strtonum( _tuple_val( args, 0 ) ) } else { 1 }
def x2 := if( is_defined arg2 ) { +arg2 } else if( is_defined args and argN >= 2 ) { _strtonum( _tuple_val( args, 1 ) ) } else { 1 }
def gcd := repeat {
if( x1 == x2 ) {
stop with x1
} else if( x1 > x2 ) {
x1 := x1 - x2
} else /* x2 > x1 */ {
x2 := x2 - x1
}
}