package { /** * Several functions implementing GCD, just for example. This * class does not include any logging. */ public class GCDplain { /** * Recursive version of GCD. Will not terminate if x or y is * non-positive, and are unequal. */ public static function recGcd (x:int,y:int) : int { var r : int ; if (x == y) r = x ; else if (x>y) r = recGcd(x-y,y); else r = recGcd(x,y-x); return r ; } /** * Iterative GCD. Will not terminate if x or y is * non-positive, and are unequal. */ public static function whileGcd (x:int,y:int) : int { var iter_count:int = 0 while (x != y) { iter_count++; if (x > y) x = x - y; else y = y - x ; } return x; } /** * Will check the parameters first. If one is non-positive * this function crashes; else it calls the iterative GCD. */ public static function catchGcd (x:int,y:int) : Object { var r : int ; try { if (x<=0 || y<=0) throw new Error("oops") ; // crash... else r = whileGcd(x,y) ; } catch (e:Error) { var r2 : String = "check gcd args!" ; return r2 ; } return r ; } } }