
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<cstdio>
#include<iostream>
#include<vector>
#define Lim 100000001
using namespace std;
bool prime [Lim];
vector<int> v ;
void isPrime ()
{
for( int i = 2 ; i * i <= Lim ; i++)
if ( !prime [i]) for ( int j = i * i ; j <= Lim ; j += i ) prime
[j] = 1 ;
//for( int i = 2 ; i <= Lim ; i++) if ( ! prime[i] ) v.push_back( i
) ;
//cout<
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.
Which loop ?
for( int i = 2 ; i <= Lim ; i++) if ( ! prime[i] ) v.push_back( i ) ;
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com