Problem Link - Prime Generator Practice Problem in 1000 to 1400 difficulty problems
Problem Statement:
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.
Approach:
Primality Check Function:
- The function checks if a given number
nis prime using a trial division up to \sqrt{n} - If
n≤1, it immediately returnsfalseas numbers less than or equal to1are not prime. - It iterates from
2to \sqrt{n} Ifnis divisible by any of these numbers, it returnsfalse(indicatingnis not prime). Otherwise, it returnstrue(indicatingnis prime).
Generating Primes in the Given Range:
- For each test case, iterate from
mtonand use the Prime function to check if each number in this range is prime. - If a number is prime, it is printed to the output, each on a new line.
Complexity:
- Time Complexity: The Prime function has a time complexity of O(\sqrt{n}). the code iterates over the range from
m to n. If the range size isR=n−m+1, the main loop runsO(R)times. Total time complexity is O(R . \sqrt{n}). - Space Complexity:
O(1)No extra space required.