Q:

Logic Help: Calculating primes of a range of numbers with multiple threads

I've been tasked to calculate prime numbers using multiple threads. The user provides a range of numbers, and how many threads to use to find the prime numbers. 
So for example, the range is 2 to 100 inclusive and we want to use 3 threads.
What I want to happen is the following:
Thread 1 found  2 3 5 7 11 13 17 19 23 29 31
Thread 2 found  37 41 43 47 53 59 61 67
Thread 3 found  71 73 79 83 89 97

Each thread is going to need to be supplied a start range and an end range. I have determined that a data structure would be the most appropriate for this, such as:
typedef struct Data{
    int start;
    int end;
}Data; 

I can't figure out how to create these sub-ranges. I know I'll have to divide the max end range by how many threads we want, and getting the first threads range is easy but I'm lost after that. This seems like a simple issue that I am making entirely too complicated.
This is where I'm at:
int start, end, nthreads, div;

start = atoi(argv[1]);
end = atoi (argv[2]);
nthreads = atoi (argv[3]);
div = end/nthreads;

for (int i = 0; i < nthreads; i++){
    if (i == 0){
        //first iteration, start is start and end is the div
        data.start = start;
        data.end = div;
    }
    else{
        //this is where I am lost
        data.start = ??
        data.end = div*i;
    }

    //create the thread with our data
    //calculations is handled in threadCreate()
    pthread_create(&tid, NULL, threadCreate, (void*)data);
}

A:

From your code, you want to remove the if (i==0) and simply set this:
data.start = start + div * i
data.end = start + div * (i + 1) - 1;

Be cautious though, you may want to redefine div as (end-start) / nbthreads.
You will then quickly realize that some ranges are harder to find prime numbers.
I would recommend using OpenMP which is made exactly for this kind of usage. This would simplify your threading by automatically splitting your for loop. This would allow you to easily use a dynamic task assignation scheme which will automatically balance thread assignation of easier and harder ranges in order to end the computation faster.

