Last active
June 27, 2020 16:25
-
-
Save Ch-sriram/59f96b46335d5041fe4cb413d6906227 to your computer and use it in GitHub Desktop.
Prime Generator (SPOJ) [O(R * X), where R = Max Range of 10^5 & X = sqrt(N)]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem link: https://www.spoj.com/problems/PRIME1/ | |
import java.util.Scanner; | |
import java.lang.Exception; | |
class Main { | |
public static boolean isPrime(final int N) { | |
if(N <= 1) return false; | |
for(int i = 2; i*i <= N; ++i) | |
if(N % i == 0) | |
return false; | |
return true; | |
} | |
public static void main (String[] args) throws Exception { | |
Scanner s = new Scanner(System.in); | |
int t = s.nextInt(); | |
while(t-- > 0) { | |
int l = s.nextInt(), r = s.nextInt(); | |
for(int i = l; i <= r; ++i) | |
if(isPrime(i)) | |
System.out.println(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment