I'm trying to make a prime number generator using arrays. I started with a 1D array but the max limit on my computer was 250000. So I thought maybe I should make a 2d or more array to go to the next line once this limit is reached to avoid problems on my PC. But I have no idea how to do it. Any help would be welcome.
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<time.h>
#define LIMIT 250000ul
//prime number generator using arrays
void prime_generator(){
unsigned long long n;
printf("How many primes do you want generated : ");
scanf("%llu", &n);
unsigned long long p[LIMIT];
unsigned long long i, j=2;
unsigned long long num;
bool isPrime;
unsigned long long primes_per_line, space_column;
printf("How many primes do you want displayed per line? : ");
scanf("%llu", &primes_per_line);
printf("Enter space : ");
scanf("%llu", &space_column);
p[0]=2, p[1]=3;//first two primes
unsigned long long count = 2;
clock_t begin = clock();
for(num = 5;count < n; num=num+2){//only test for odd number
isPrime = true;
for(i = 1;i < j && p[i] <= sqrt(num) ;i++){
if(num%p[i]==0){
isPrime = false;
break;//break out of i loop
}
}
if(isPrime == true){
++count;
p[j] = num; //new prime found
++j;
}
}
clock_t end = clock();
double time_spent = (double) (end-begin)/CLOCKS_PER_SEC;
for(i = 0; i < n; i++){
if(i!=0 && i % primes_per_line == 0) printf("\n");
printf("%*llu", space_column, p[i]);
}
printf("\nCrunching time is : %.12f", time_spent);
}
int main(void){
prime_generator();
return 0;
}