算額あれこれ

算額問題をコンピュータで解きます

Julia: プログラムを速くするための定石--その10

ヴィーフェリッヒ素数
http://commutative.world.coocan.jp/blog5/2020/11/post-182.html

関数化と Primes パッケージの isprime() を使うことによりほぼ 15 倍速となった。
元のプログラムと比較して欲しい。

using Primes
function Wiefelich()
    z = BigInt(1)
    k = BigInt(2)
    for p = 3:100000
        isprime(big(p)) && k^(p - 1) % (p^2) == 1 && print("$p ")
    end
end

@time Wiefelich()

1093 3511   0.069396 seconds (267.17 k allocations: 59.338 MiB, 3.79% gc time)