【发布时间】:2017-12-05 16:33:04
【问题描述】:
我已经实现了以下两个函数来确定 n 是否为 fermat 素数(如果为真则返回 n,否则返回 -1),但它始终返回 -1,不知道为什么(gc 是函数taht计算gcd)
fermatPT :: Int -> Int
fermatPT n = fermatPT' n list
where
list = [a | a <- [1..n-1]]
-- | heper function
fermatPT' :: Int -> [Int] -> Int
fermatPT' n l | gc (n, head l) == 1 && fermatTest n (head l) = fermatPT' n (tail l)
| null l = n
| otherwise = -1
where
fermatTest n a = mod (a^(n-1)) n == 1
【问题讨论】:
-
为什么不
True或False?我真的不明白为什么你把东西编码为Ints,如果这些是真值 -
或者如果您需要返回数字,只需 n 和 Nothing
-
list有点毫无意义;你可以写fermatPT n = fermatPT' n [1..n-1]。 -
如果它总是返回 -1,那么
gc可能有问题导致它永远不会返回 1。
标签: haskell math primality-test