Is there something wrong with the CODE [Prime Generator]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
void primeGenerate(int start, int end){
    int i;
    int startMod10;
    int squart;
    if(start == 1) start++;
    if(start == 2) {printf("%d\n",start); start++;}
    if(start == 3) {printf("%d\n",start); start++;}
    if(start == 4) start++;
    if(start == 5) {printf("%d\n",start); start++;}
    if(start == 6) start++;
    if(start == 7) {printf("%d\n",start); start++;}
    if(start == 8) start++;
    if(start == 9) start++;
    while(start!=end){
            startMod10=start%10;
            if((startMod10!=1)&&(startMod10!=3)&&(startMod10!=7)&&(startMod10!=9))
                {start++; continue;}
            squart=sqrt(start)+1;
            for(i=3; i<=squart; i=i+2){
                    if(start%i==0)
                        {break;}
            }
            if(i>=squart) printf("%d\n",start);
            start++;
    }
}
main()
{
        primeGenerate(1,1000000);
        printf("\n");
}


I coded this algo last hour and I don't know is it really works or not. How to be assure that is really Generating real prime number. As far as i check manually it was prime. i need to Generate prime 1-1000000000 where difference is 1000000 so it is a good method to get primes isn't it.
Last edited on
Looks great to me. I'm running it now, the results should show:
There are 50,847,534 primes between 1 and 1,000,000,000.
oh great i i .... dont know how much prime there are in 1 and 1,000,000,000 but after your post i just Google it and got there are 50,847,534 prime so it means my code works. But do you have any idea to run it faster for a huge number like 1,000,000,000.
As far as i get the fastest method of generating prime is Sieve of Atkin and i implement it from the Pseudocode provided on Wikipedia but that code work from 1 to X and because none of array can handle more than 1,000,000 element it fail with the number 1,000,000,000.
You can declare the array like this:
1
2
int array[500000000]; 
int array2[500000000]; 


Which would give an array size of 1,000,000,000 if you just treat that as one continuous array. Check you have enough memory to do this though.

1,000,000,000 * sizeof(int) = 4,000,000,000 ~ 4 GB
Topic archived. No new replies allowed.