【发布时间】:2019-02-28 17:05:00
【问题描述】:
我目前正在尝试在 Haskell 中实现 Atkin 筛
在Wikipedia article on the Sieve of Atkin 的第 3 步中,我需要找到多个方程的整数解的数量。
但是我对第一个方程的解 (4x² + y² = n, x > 0, y > 0 其中 n 是正整数列表中的一个条目)在使用任何 n 的查询时会产生一个无限循环。
到目前为止,这是我解决这部分问题的代码:
eq1 :: Integer -> Integer
eq1 n = eq1_ n []
eq1_ :: Integer -> [(Integer, Integer)] -> Integer
eq1_ n list | (x > 0) && (y > 0) && (n == 4*(x^2) + (y^2)) && (notElem ((x,y)) list) = eq1_ n ([(x, y)] ++ list)
| otherwise = toInteger (length list)
where
x = floor (sqrt (fromIntegral ((n - y^2) `div` 4)))
y = floor (sqrt (fromIntegral (n - 4*(x^2))))
WinGHCi 可以很好地加载它,但是当我查询例如eq1 0 它只是停留在无限循环中,必须在产生答案之前中断。我怀疑它在x 和y 的两个分配之间循环。
如何防止这种情况发生?这甚至可能吗?
编辑:意识到无限循环必须在哪里。
【问题讨论】:
-
如果你已经解决了你的问题,你认为你可以自己回答吗?
标签: haskell equation-solving sieve-of-atkin