[Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread mukesh tiwari
Hello all Being a Haskell enthusiastic , first I tried to solve this problem in Haskell but it running for almost 10 minutes on my computer but not getting the answer. A similar C++ program outputs the answer almost instant so could some one please tell me how to improve this Haskell program.

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Lyndon Maydwell
Could Int be overflowing? On Tue, Nov 8, 2011 at 7:21 PM, mukesh tiwari mukeshtiwari.ii...@gmail.com wrote: Hello all Being a Haskell enthusiastic , first I tried to solve this problem in Haskell but it running for almost 10 minutes on my computer but not getting the answer. A similar C++

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread mukesh tiwari
I am not sure about Int overflow. There is no case of Int overflow in prime , pList and divPrime function however lets assuming Int overflow in main but then still answer should be outputted. Regards Mukesh Tiwari On Tue, Nov 8, 2011 at 5:08 PM, Lyndon Maydwell maydw...@gmail.com wrote: Could

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Ivan Lazar Miljenovic
May I suggest you try a non-ST solution first (e.g. using Data.IntMap) first (assuming an auxiliary data-structure is required)? Also, I'm not sure if the logic in the two versions is the same: I'm not sure about how you handle the boolean aspect in C++, but you have a third for-loop there that

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread mukesh tiwari
Logic is same. The idea is generate the primes less than 10^8. Now from each prime , subtract 1 ( when d is 1 then d + n / d = n + 1 should be prime ) and check for all the divisor. If all divisor are prime then return True else False divPrime n = all ( \d - if mod n d == 0 then pList ! ( d +

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Ivan Lazar Miljenovic
On 8 November 2011 23:29, mukesh tiwari mukeshtiwari.ii...@gmail.com wrote: Also, I'm not sure if the logic in the two versions is the same: I'm not sure about how you handle the boolean aspect in C++, but you have a third for-loop there that doesn't seem to correspond to anything in the

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread mukesh tiwari
In that loop , I am collecting all the primes in vector how ever I changed the c++ code and now it resembles to Haskell code. This code still gives the answer within a second. #includecstdio #includeiostream #includevector #define Lim 10001 using namespace std; bool prime [Lim]; vectorint

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Ryan Yates
If I compile with optimizations: ghc --make -O3 primes.hs I get an answer that is off by one from the C++ program in a few seconds. On Tue, Nov 8, 2011 at 7:46 AM, mukesh tiwari mukeshtiwari.ii...@gmail.com wrote: In that loop , I  am collecting all the primes in vector how ever I changed

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Ryan Yates
I forgot to add, I'm on 32-bit GHC and the sum will overflow there, so I changed main: main = putStrLn . show . sum $ ([ if and [ pList ! i , divPrime . pred $ i ] then (fromIntegral $ pred i) else 0 | i - [ 2 .. 10 ^ 8 ] ] :: [Integer]) On Tue, Nov 8, 2011 at 8:19 AM, Ryan Yates

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread mukesh tiwari
Thank you Ryan . I never compiled my program with -O3 option before and now i can feel the power of compiler optimisation. Regards Mukesh Tiwari On Tue, Nov 8, 2011 at 6:50 PM, Ryan Yates fryguy...@gmail.com wrote: I forgot to add, I'm on 32-bit GHC and the sum will overflow there, so I

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Silvio Frischknecht
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 11/08/2011 02:19 PM, Ryan Yates wrote: If I compile with optimizations: ghc --make -O3 primes.hs I get an answer that is off by one from the C++ program in a few seconds. nice one. Though i wonder. The problem seems to be that without

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Daniel Fischer
On Tuesday 08 November 2011, 12:21:14, mukesh tiwari wrote: Hello all Being a Haskell enthusiastic , first I tried to solve this problem in Haskell but it running for almost 10 minutes on my computer but not getting the answer. Hmm, finishes in 13.36 seconds here, without any changes. Of

Re: [Haskell-cafe] Project Euler Problem 357 in Haskell

2011-11-08 Thread Daniel Fischer
On Tuesday 08 November 2011, 14:54:18, Silvio Frischknecht wrote: On 11/08/2011 02:19 PM, Ryan Yates wrote: If I compile with optimizations: ghc --make -O3 primes.hs So far, -O3 is not different from -O2 (-On gives you -O2 for n 2). *Never* compile code you want to use without