Prime Generator Codechef Solution |Problem Code: PRIME1

Hello coders, today we are going to solve Prime Generator Codechef Solution |Problem Code: PRIME1.

Prime Generator Codechef Solution
Prime Generator Codechef Solution

Problem

Ram wants to generate some prime numbers for his cryptosystem. Help him, please! Your task is to generate all prime numbers between two given numbers.

Input

The first line contains t, the number of test cases (less then or equal to 10). Followed by t lines which contain 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. Separate the answers for each test case by an empty line.

Example

Input:
2
1 10
3 5
Output:
2
3
5
7
3
5

Prime Generator CodeChef Solution in CPP

#include <iostream>
using namespace std;
bool isPrime(long long int n)
{
    if(n <=1)
        return false;
    for(int i =2; i<n; i++)
    {
        if(n % i == 0)
            return false;
    }
    return true;
}
void generate_primes()
{
    long long int a, b;
    cin >> a >> b;
    if(a <=1 || b<=1)
        return;
    for(int i =a+1 ; i<b; i++)
    {
        if(isPrime(i))
            cout<<i<<endl;
    }
    cout<<endl;
}
int main()
{
    long long int test_cases;
    cin >> test_cases;
    while(test_cases--)
    {
        generate_primes();
    }
    return 0;
}

Disclaimer: The above Problem (Prime Generator) is generated by CodeChef but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring