//
// main.swift
// BigInt_for_C_swift
// APIテスト
//
// 5. 累乗・剰余累乗
let two = BigInt("2")
let p Int = 100
let power = two.pow(p)//2^100
print(power)// → 1267650600228229401496703205376
2^100を計算します。tweはBigInt型、pはInt型、返り値はBigInt型です。tweがBigInt型でないのは、単に意味がないという理由です。
//6. GCD/LCM(最大公約数・最小公倍数)
let d = BigInt(42)
let e = BigInt(56)
let g = BigInt.gcd(d, e) // 14
let l = BigInt.lcm(d, e) // 168
print(g, l)
最大公約数(GCD)と最小公倍数(LCM)を計算します。
//7. 素数判定(Miller–Rabin法)(Bool)
let r = BigInt("32416190071")
print(r.isProbablePrime) // true
let s = BigInt("1000005")
print(s.isProbablePrime) // false
//素数かどうかを判断します。素数ならtrue、合成数ならfalseを返します。ただしMiller–Rabin法を用いているので、多分ということです。
//8. 乱数・素数生成
let t = BigInt.random(digits: 100)// 100桁の乱数を生成します
print(t)// → 2102481647666124823991852653298001158734038282291387385003485276648110859085679305983894732944436443
let u = BigInt.random(in: BigInt(1000)...BigInt(9999))//1000~9999の範囲の乱数を発生します
print("u")// (例)→ 4533
let v = BigInt.randomProbablePrime(digits: 50)// 50桁の素数
print(v)// → (例)48843795598341390675949785287345763265173901344337
//ただしMiller–Rabin法を用いているので、多分ということです
//9. 進数変換
let n = BigInt("123456789")//十進数
print(n.hexString) // "75bcd15"
print(BigInt.fromHex("0xff")) // 255 16進数を10進数に変換します
