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.
impo
Could Int be overflowing?
On Tue, Nov 8, 2011 at 7:21 PM, 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. A similar C++ program outputs the answer a
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 wrote:
> Could Int be overflowin
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 do
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 +
On 8 November 2011 23:29, mukesh tiwari 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 Haskell version.
>>
> Whic
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.
#include
#include
#include
#define Lim 10001
using namespace std;
bool prime [Lim];
vector v ;
void isPrime ()
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
wrote:
> In that loop , I am collecting all the primes in vector how ever I changed
> the c++ code and now it rese
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 wrote:
> If
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 wrote:
> I forgot to add, I'm on 32-bit GHC and the sum will overflow there, so
> I changed main:
>
> main
-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
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 c
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 op
13 matches
Mail list logo