3
我正在創建一個函數來分解haskell中的任何給定數字。所以我創造了這個:爲什麼我的素因子分解函數在結果中附加1?
primes :: [Integer]
primes = 2:(sieve [3,5..])
where
sieve (p:xs) = p : sieve [x |x <- xs, x `mod` ((p+1) `div` 2) > 0]
factorize 2 = [2]
factorize x
| divisible x = w:(factorize (x `div` w))
| otherwise = [x]
where
divisible :: Integer -> Bool
divisible y = [x |x <- (2:[3,5..y]), y `mod` x == 0] /= []
firstfactor :: Integer -> [Integer] -> Integer
firstfactor a (x:xs)
| a `ifdiv` x = x
| otherwise = firstfactor a xs
firstfactor a _ = a
ifdiv x y = mod x y == 0
w = firstfactor x primes
功能工作正常,但追加1到列表的末尾,例如factorize 80
會給這個名單:[2,2,2,2,5,1]
我的問題是,爲什麼會出現這種情況?
強制性鏈接到「Erathostenes篩」論文:http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf – Cactus 2014-12-19 01:36:44