my code is doing the correct work but not in hacker blocks
Code not running
lower= int(input()) upper = int(input()) for num in range(lower,upper+1): if num>1: for i in range(2,num): if num%i==0: break else: print(num)
In the code you are not considering the number of test cases
can you help with this pls … m not understanding how to do it then …
For this question you will have to look up the concept of Sieve of Eratosthenes. You wont be able to solve this question using any other concept , since this question is made for languages such as C/C++/java . You would face TLE problem in this question since python is a slow language .
can you please tell me the code for this ?
public static void sieveOfEratosthenes(int ll, int ul) {
boolean prime[] = new boolean[ul + 1];
Arrays.fill(prime, true);
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= ul; p++) {
if (prime[p] == true) {
for (int i = p * 2; i <= ul; i += p)
prime[i] = false;
}
}
for (int i = ll; i <= ul; i++) {
if (prime[i] == true)
System.out.print(i + " ");
}
}
Here is the code in java.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
please help me with the python code . i’m a beginner in coding … not did java or c++ yet .secondly i didnt understand that code too
def SieveOfEratosthenes(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
for p in range(n + 1):
if prime[p]:
print p
This is the python program for Sieve Of Eratosthenes,which is the fastest way to produce prime numbers.
this code also not working plus i didnt understand this code too
- Create a list of consecutive integers from 2 to n : (2, 3, 4, …, n ).
- Initially, let p equal 2, the first prime number.
- Starting from p 2, count up in increments of p and mark each of these numbers greater than or equal to p2 itself in the list. These numbers will be p(p+1) , p(p+2) , p(p+3) , etc…
- Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3
When the algorithm terminates, all the numbers in the list that are not marked are prime.
For video explanation of this topic you can take free trial of Algo++ course . There you will find a video by Prateek bhaiya explaining this concept under the free trial section.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.