require "bignum" local fmt = require "fmt" local one = bigint.one local two = bigint.two local function fermat(n) return (two ^ (two ^ n)) + one end local fns = {} print("The first 10 fermat numbers are:") for i = 0, 9 do fns[i + 1] = fermat(i) print($"F{fmt.sub(i)} = {fns[i + 1]}") end print("\nFactors of the first 9 Fermat numbers:") mpz.init() for i = 0, 8 do io.write($"F{fmt.sub(i)} = ") local factors if i < 7 then factors = bigint.factors(fns[i + 1]) else factors = mpz.factors(fns[i + 1]) end fmt.lwrite(factors) if #factors == 1 then print(" (prime)") elseif !factors[1]:isprobableprime(10) then print(" (second factor is composite)") else print() end end local f = bigint.pollard(fns[10]) print($"\nThe smallest factor of F₉ is: {f}")