-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathoptimizedprime.java
More file actions
39 lines (35 loc) · 1.07 KB
/
optimizedprime.java
File metadata and controls
39 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Hey Everyone,
I'm Om just started Algorithms topic in java wanted to share that i have found a great way to find total prime no. between given two no.
It saves much time from bruteforce Solution Hope this helps!
Thanks!
Om Duragkar
GITHUB Account : https://github.com/omduragkar
*/
// The Most efficient way to find optimized solution of total primes between two given no. it reduces time complexity from O(n^2) to O(logn)
import java.util.*;
public class optimizedprime{
public static void main(String[] args) {
Scanner scn = new Scanner (System.in);
int l = scn.nextInt();
int h = scn.nextInt();
for(int i=l;i<=h;i++)
{
int j=2;
boolean isprime=true;
while(j*j<=i)
{
if(i%j==0)
{
isprime= false;
break;
}
j++;
}
if(isprime)
{
System.out.println(i);
}
}
}
}