2007年5月6日日曜日

最大公約数と最大公倍数を求める

// 最大公約数(GCD)と最小公倍数(LCM)を求めるユーザー定義関数

#module GetGCDandLCM
#defcfunc _gcd int high, int low, local tmp
    tmp = high \ low
    if tmp {
        return _gcd(low, tmp)
    } else {
        return low
    }

#defcfunc gcd int i1, int i2
    if i1 > i2 {
        return _gcd(i1, i2)
    } else {
        return _gcd(i2, i1)
    }

#defcfunc lcm int i1, int i2
    return i1 * i2 / gcd(i1, i2)
#global

// 以下サンプル
#runtime "hsp3cl"
    mes "最大公約数と最小公倍数を求める整数を2つ入力してください..."
    repeat 2
        repeat
            input tmp, , 1
            if int(tmp) > 0 : break
            mes "1以上の整数値を入力してください..."
        loop
        num(cnt) = int(tmp)
    loop

    mes "GCD(" + num(0) + ", " + num(1) + ") = " + gcd(num(0), num(1))
    mes "LCM(" + num(0) + ", " + num(1) + ") = " + lcm(num(0), num(1))
    stop

0 件のコメント: