I wrote these two algorithm in C++.
Sieve of Atkin :
#include <new>
#include <cmath>
#include <iostream>
#include <exception>
#include <sys/time.h>
typedef unsigned long long ulong;
using namespace std;
class SieveOfAtkin
{
private :
ulong Limit;
static bool *IsPrime;
public :
SieveOfAtkin() { }
SieveOfAtkin(ulong Limit) : Limit(Limit)
{
try
{
IsPrime = new bool[Limit];
}
catch(bad_alloc&)
{
cerr << " Memory allocation unsuccessful." << endl;
}
}
static long NoOfPrime;
void PrimeGen();
void Display();
};
bool *SieveOfAtkin :: IsPrime;
long SieveOfAtkin :: NoOfPrime = 0;
void SieveOfAtkin :: PrimeGen()
{
ulong SqrtLimit = (ulong)ceil(sqrt((double)Limit));
for (ulong x = 1; x <= SqrtLimit; x++)
{
for (ulong y = 1; y <= SqrtLimit; y++)
{
ulong n = 4 * x * x + y * y;
if (n <= Limit && (n % 12 == 1 || n % 12 == 5))
{
IsPrime[n] ^= true;
}
n = 3 * x * x + y * y;
if (n <= Limit && n % 12 == 7)
{
IsPrime[n] ^= true;
}
n = 3 * x * x - y * y;
if (x > y && n <= Limit && n % 12 == 11)
{
IsPrime[n] ^= true;
}
}
}
for (ulong n = 5; n <= SqrtLimit; n++)
{
if (IsPrime[n])
{
ulong s = n * n;
for (ulong k = s; k <= Limit; k += s)
{
IsPrime[k] = false;
}
}
}
IsPrime[2] = true;
IsPrime[3] = true;
}
void SieveOfAtkin :: Display()
{
int Counter = 0;
for (ulong i = 2; i <= Limit; i++)
{
if (IsPrime[i])
{
/*cout << i << "\t";
Counter++;
if (Counter == 19)
{
cout << endl;
Counter = 0;
}*/
NoOfPrime++;
}
}
cout << endl << endl;
}
unsigned GetTickCount()
{
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0)
{
return 0;
}
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
int main ()
{
unsigned StartTime = GetTickCount();
SieveOfAtkin Object = SieveOfAtkin(10000000LLU);
Object.PrimeGen();
Object.Display();
cout << " Number of Prime : " << Object.NoOfPrime << endl;
cout << " Execution time : " << (GetTickCount() - StartTime) << " ms." << endl << endl;
return 0;
}
Output :
tapas@My-Child:~/Prime Number Program/Atkin$ g++ Atkin.cpp -o Atkin.o
tapas@My-Child:~/Prime Number Program/Atkin$ ./Atkin.o
Number of Prime : 664579
Execution time : 1370 ms.
And Sieve of Eratosthenes :
#include <new>
#include <list>
#include <cmath>
#include <iostream>
#include <exception>
#include <sys/time.h>
typedef unsigned long long ulong;
using namespace std;
class SieveOfEratosthenes
{
private :
ulong Limit;
static bool *IsPrime;
public :
SieveOfEratosthenes() { }
SieveOfEratosthenes(ulong Limit) : Limit(Limit)
{
try
{
IsPrime = new bool[Limit + 1];
}
catch(bad_alloc&)
{
cerr << " Memory allocation unsuccessful." << endl;
}
}
static long NoOfPrime;
void PrimeGen();
void Display();
};
bool *SieveOfEratosthenes :: IsPrime;
long SieveOfEratosthenes :: NoOfPrime = 0;
void SieveOfEratosthenes :: PrimeGen()
{
for (ulong i = 2; i*i <= Limit; i++)
{
if (!IsPrime[i])
{
for (ulong j = i; i*j <= Limit; j++)
{
IsPrime[i*j] = true;
}
}
}
}
void SieveOfEratosthenes :: Display()
{
int Counter = 0;
for (ulong i = 2; i <= Limit; i++)
{
if (!IsPrime[i])
{
/*cout << i << "\t";
Counter++;
if (Counter == 19)
{
cout << endl;
Counter = 0;
}*/
NoOfPrime++;
}
}
cout << endl << endl;
}
unsigned GetTickCount()
{
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0)
{
return 0;
}
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
int main ()
{
unsigned StartTime = GetTickCount();
SieveOfEratosthenes Object = SieveOfEratosthenes(10000000LLU);
Object.PrimeGen();
Object.Display();
cout << " Number of Prime : " << Object.NoOfPrime << endl;
cout << " Execution time : " << (GetTickCount() - StartTime) << " ms." << endl << endl;
return 0;
}
Output :
tapas@My-Child:~/Prime Number Program/Eratosthenes$ g++ Eratosthenes.cpp -o Eratosthenes.o
tapas@My-Child:~/Prime Number Program/Eratosthenes$ ./Eratosthenes.o
Number of Prime : 664579
Execution time : 696 ms.
So which one is faster? I can not understand what is going on. Is there any problem in my code? Please help.

New Topic/Question
Reply




MultiQuote






|