这是我关于haskell的第二个问题,我认为我的算法节奏不是很差,它在c++和python中给出了更快的结果,haskell有一些问题,它不能给我10^25 (也许它会给我,但我不会等待那么多),这个问题问我这个值,请从现在开始指导我修复这个家伙。
##Euler 169##
giveRes 0 _= 1
giveRes 1 _= 1
giveRes a x = if search a x
then send a x
else let res = if rem a 2 == 0
then (giveRes (quot a 2) x)
+
(giveRes (quot a 2-1) x)
else giveRes (quot a 2) x
in snd (head ((a,res):x))
search _ [] = False
search a (x:xs) = if a == fst x then True else search a xs
send a (x:xs) = if a == fst x then snd x else send a xs
代码很长,因为它是我的记忆系统,可以缩短时间,但在haskell中效率不高
f 0 _ = 1
f a = if rem a 2 == 0
then giveRes (quot a 2) + giveRes (quot a 2-1)
else giveRes (quot a 2)
是代码第一种形式
发布于 2013-05-15 09:44:56
snd (head ((a,res):x)) --> res
。另一个建议:将你的公共子表达式(quot
)类型signatures.[haskell] memo
,那么您应该会得到很多很好的results.search
),然后使用一个partial function for lookup (send
)。相反,可以考虑使用容器或无序容器库中的Map
或HashMap
。https://stackoverflow.com/questions/16553594
复制相似问题