数学で扱う関数です。

冪乗、modPow、平方根、最大公約数、最小公倍数、階乗、完全平方か判断、Miller–Rabin法を用意してあります。

 

override func viewDidLoad() {

        super.viewDidLoad()

        //2の3乗を返す

        let a = BigInt.pow(2, 3)//exp は非負整数)。

        print(a)//→ 8

        

        //modPow(b, e, m) = (a^e) mod m を返す

        let b: BigInt = 7

        let e: BigInt = 128

        let m: BigInt = 13

        let result = BigInt.modPow(b, e, m)

        print("7^128 mod 13 = \(result)")

        

        //√cを返す

        let c: BigInt = 64

        let d = BigInt.sqrt(of: c)

        print(d)

        

        //f と g の最大公約数を返す

        let f : BigInt = 128

        let g : BigInt = 12

        let h = BigInt.gcd(f, g)

        print(h)

        

        //iとjの最小公倍数を返す

        let i : BigInt = 128

        let j : BigInt = 12

        let k = BigInt.lcm(i, j)

        print(k)

        

        //n!(階乗)を返す

        let l: Int = 12

        let n = BigInt.factorial(l)

        print(n)

        

        //完全平方か

        let o : BigInt = 49

        let p: Bool = BigInt.isPerfectSquare(o)

        print(p)

        

        //Miller–Rabin法でnが素数らしいかどうかを判定

        let q: BigInt = 127

        let r: Bool = BigInt.isPerfectSquare(q)

        print(r)

    }