Python - Print All Prime Numbers In Any Interval

preview_player
Показать описание
in this video I show you how to create a function that prints out all prime numbers in any given interval
Рекомендации по теме
Комментарии
Автор

l = int(input("low bound: "))
u = int(input("upper bound: "))

for num in range(l, u+1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)

wrttech
Автор

As a beginner in coding that else statement outside for loop felt genius

naveenkumarg
Автор

Does the second for loop "for i in range(2, num):" never check the number itself so that we can break out of the loop? Example num =100 will only ever check up to 99. So that the for loop is never satisfied?

brendanthorne
Автор

Great tutorial. Just a suggestion below
Instead of "for i in range(2, num)",
"for i in range(2, (num//2 + 1))" would have better time complexity isn't it? Because for any i which is greater than num/2, num won't divide by i

houseofelectronics
Автор

If finding prime number between 2 and 200, Does this function work? Because 2 divided by 2 will break and will not print 2, but 2 is a prime number.

snehap
Автор

How can we modify this so that given a positive integer x, it computes the fraction
f(x) of prime numbers less than or equal to x, i.e., f(x) = y/x, where y is the the number of prime
numbers less than or equal to x

telsaabi
Автор

how to write this code for, prime numbers between 100 & 200, and output as 5 numbers in 1 line and then next line, a little tricky question...

cokfarm
Автор

wonderful, i really appreciate that. Yet i am wondering why the else can go outside the for loop but not on the same vertical line with 'if'. What does it mean sir, hope you can answer me~~

dsvuuyq
Автор

sir can you write it without using a break command

jadeboom
Автор

Just curious: why didn't you create a function?

driesdep
Автор

If we know 2 isn't a factor of n, wouldn't it be better to avoid running this code over rest of the even numbers in our integer range (since even numbers are all factors of 2)?

ayearninghope
Автор

how to print only first prime number in this condition. i.e output must be only single number

bharadhwajsinguluri
Автор

How to get all that output in single line

chadvukondiifirstuu
Автор

When i was doing this in the range between 1 to is not including in the list when the output was dont know why it was not including....pls give me the solution sir

bhavanipullepu
Автор

In pydroid it doesnt work but in pycharm it works i must have a stupid IDE

FC
Автор

For some reason, I copied this exactly into IDLE and it is only printing "2". Nothing else

bryceberry