以下代码尝试解决https://projecteuler.net/problem=87
import Math.NumberTheory.Primes.Sieve -- from package arithmoi
gen::[Integer]->Integer->[Integer]-- time complexity : n! , n = numberofprimes
gen pmlist limit=filter (\x->x<=limit) prelist
where prelist=do
let ls1=filter (\(x::Integer)->(x^2)<=limit) pmlist
a<-ls1
let ls2=filter (\(x::Integer)->(x^3)<=limit) ls1
b<-ls2
c<-filter (\(x::Integer)->(x^4)<=limit) ls2
-- guard $ (b>=a)&&(c>=b)
let res=(a^2+b^3+c^4)
return res
main=do
print "euler proj" -- other's answer:1097343 , my : 1139575
print $ length $ ep87 limit_1
-- print $ ep87
primesUnder i = takeWhile (\x->x<=i) primes -- library function
limit_1::Integer
limit_1=50000000
limit_2=1000000000
limit_3=10000000000
ep87 lmt = gen (primesUnder lmt) lmt
另一个答案:1097343,但这段代码片段给出了1139575,可能是罪魁祸首?
答案 0 :(得分:0)
您的方法会生成重复项:
ghci> ep87 150
[28,93,47,112,145,33,98,52,117,150,49,114,68,133,73,138,92,145]
^^^ ^^^