1

我在 Haskell 中有以下代码。我想对给定的数字 n 重复 30 次费马素性检验,但问题是它总是返回 False……我试图解决问题,但我总是得到错误的答案……有什么想法吗?

import System.Random
import System.IO.Unsafe

takeARandomNum n=unsafePerformIO (getStdRandom (randomR (2,n)))

fermatTestA :: (Int, Int) -> Bool
fermatTestA (n, a) =((a^(n-1) `mod` n)==1)

solve :: (Int, Int) -> Bool
solve (n, 1) = fermatTestA (n, takeARandomNum (n-2))
solve (n, maxTest)
    | fermatTestA (n, takeARandomNum (n-2)) = (solve (n, (maxTest-1)))
    | otherwise = False

fermatTest :: Int ->Bool
fermatTest n = solve (n, 30)
4

1 回答 1

5

你的整数被截断了。Haskell 中的“Int”不是大整数类型。将所有“Int”类型声明更改为“Integer”,您的代码应该可以工作。

于 2013-06-10T19:59:36.713 回答