/*
* This file uses the Erastothenes Sieve way of producing Primes
*
* Created on: Jun 4, 2012
* Author: mirtchea
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Example
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
*/
// calculate the amount of time needed to generate 10 sets of the highest prime numbers
#include <stdio.h>
#include <math.h>
#include <time.h>
int main()
{
int num; // the number of test cases
bool singleArray[100000]; // holds one range of prime numbers: TRUE if prime, FALSE otherwise
static unsigned long allArray[1000000]; // holds all of the ranges of prime numbers
unsigned long nums[10][2]; // the array of numbers that hold all the ranges
// 10 rows, 2 columns (10 sets of numbers, with two numbers each)
unsigned long s; // holds the square root of the upper end of the range
long n1, n2; // the two numbers in each range, read in 'num' number of times
int count = 0; // maintains the count of all the prime numbers to be stored into the final array
long intermediate; // used for the range of the array to track the index of the found prime numbers
//time_t start, end; /// stores the start and end time of the operation
scanf("%d", &num);
for(int i = 0; i < num; ++i) // for the number of test cases, gather the pairs of numbers
{
scanf("%lu", &n1); // the first number being scanned in
scanf("%lu", &n2); // the second number being scanned in
nums[i][0] = n1; // store the first number in the array
nums[i][1] = n2; // store the second number in the array
}
//time(&start);
for(int i = 0; i < 100000; ++i) // set all the values in the single range array to TRUE
{
singleArray[i] = true;
}
for(int i = 0; i < num; ++i) // go through all the test cases
{
s = sqrt(nums[i][1]);
for(unsigned long k = 2; k <= s; ++k) // for all the numbers ranging from 2, to the square root of the upper range
{
for (unsigned long j = nums[i][0]; j <= nums[i][1]; ++j) // for all the numbers in each test range
{
intermediate = j - nums[i][0];
if(!singleArray[intermediate]) // if the current location is already false, this means that the number is a non-prime
{
continue;
}
if((j % k == 0 && k != j) || (j == 1)) // otherwise, divide into the current number; for numbers that divide evenly, set that value to FALSE (for not being a prime number)
{
singleArray[intermediate] = false;
}
}
}
// for the single array, copy back into the main array
for(unsigned long m = nums[i][0]; m <= nums[i][1]; ++m) // for the range of numbers in one set
{
intermediate = m - nums[i][0];
if(singleArray[intermediate]) //if the number in the single array is a prime, store into the general array
{
allArray[count++] = m;
}
}
//reset all the array values to true
for(int p = 0; p < (nums[i][1] - nums[i][0]); ++p)
{
singleArray[p] = true;
}
}
// print all the prime numbers in the larger array
for(int n = 0; n < count; ++n)
{
printf("%lu\n", allArray[n]);
}
//time(&end);
//printf("This operation took: %.02f seconds to execute \n", difftime(end,start));
}