I have written this simple code to implement Eratosthenes algorithm to compute the prime numbers and type them in some ordered way. The problem is that it doesn't work for numbers larger than 1020. Can somebody tells me the reason of this? When I run it, Eclipse can not launch the exe file and stops computing the numbers. The same code written in Java works well for numbers under one billion, though.
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int N;
cin >> N;
int* a = new int[N];
int i , j, k, cnt = 0;
for(a[1] = 0, i = 2; i <= N; i++) a[i] = 1;
for(i = 2; i <= N/2; i++)
for(j = 2; j <= N/i; j++)
a[i*j] = 0;
for(i = 1; i <= N; i++)
if(a[i]) {
cout<< i ;
int lengthi = (int)floor(log10((float)i));
int lengthN = (int)floor(log10((float)N)) + 1;
for(k = 0; k < lengthN - lengthi + 1; k++)
cout<<' ';
cnt++;
if(cnt%10==0) cout<<'\n';
}
delete [] a;
return 0;
}