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)
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?
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
@@wrttech422 I'm a super beginner but wouldn't this version of the program be faster? No need to check i past the square root of num since 6*7 = 7*6 and the square root is where the numbers flip side No need to check even numbers since we know they won't be prime numbers (except for 2, which can be fixed by printing 2 before everything else) There must be a way to pass numbers ending in 5 since we know they won't be prime numbers as well but I've only been programming for a week so idk how to do it import math print(2) for n in range(1, 1000, 2): if n > 1: for i in range(2,(math.ceil(math.sqrt(n))+1)): if n%i == 0: break else: print(num)
Yes, the range function is non inclusive meaning it will go up to but not including the upper bounds you specify. Therefore, the for loop wont execute at all for the number 2, and it will go directly to the else statement and print out 2.
Think of it this way. In a for/else statement, the else statement executes if and only if the for loop executes all its iterations without hitting a break statement. when the line reads... for i in range(2,2): the number of iterations it must complete is 0. it completes 0 iterations and skips the break statement. no break = else condition
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
this returns it in decimal form: x = int(input("x: ")) y = 0 for num in range(1,x+1): if num > 1: for i in range(2,num): if num % i == 0: break else: y += 1 print(y/x) this returns it in fractional form: x = int(input("x: ")) y = 0 for num in range(1,x+1): if num > 1: for i in range(2,num): if num % i == 0: break else: y += 1 print(str(y) + "/" + str(x))
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~~
The else clause is attached to the for loop, not the if statement. In python, there is something called a for else statement. The else condition in a for else statement executes if the for loop executes in its entirety without hitting a break statement. run both example codes below example 1: in the code below, the else clause will execute because the break statement is not hit for i in range(10): if i == 11: break else: print("break statement was not encountered. Else condition executed") example 2: in the code below, the else clause will not execute because the break statement was encountered. for i in range(10): if i == 4: break else: print("else condition") print("else condition will not execute because break was encountered")
Just break out of the loop once you print the first prime number: code below 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) break
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, end = " ")
This will print all prime numbers in the interval as well as printing the count at the end. Copy and paste the code into your IDE/text editor: l = int(input("low bound: ")) u = int(input("upper bound: ")) count = 0 for num in range(l,u+1): if num > 1: for i in range(2,num): if num % i == 0: break else: count += 1 print(num) print("number of prime numbers: {}".format(count))
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)?
Write a python program that asks the user for a number and determines whether it is prime or not. The idea to solve this problem is to iterate through all the numbers starting from 2 to (N/2) using a for loop and for every number check if it divides N. If we find any number that divides, we return false. If we did not find any number between 2 and N/2 which divides N then it means that N is prime and we will return True. Sir, can you explain to me how I can do it? thanks🙏
When i was doing this in the range between 1 to 10.....2 is not including in the list when the output was displayed......I dont know why it was not including....pls give me the solution sir
just copy and paste the code below. Its the code from the video 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)
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)
thankyou
dear sir , i want the largest prime number can u explain
As a beginner in coding that else statement outside for loop felt genius
You can also use a flag e.g is_prime=True or False to track all the prime numbers. for/else clause is pythonic.
God tier explanation. Thanks!
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?
Very helpful video for me.Thanks sir
❤❤❤keep continue sir
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
yes, you are correct
@@wrttech422 I'm a super beginner but wouldn't this version of the program be faster?
No need to check i past the square root of num since 6*7 = 7*6 and the square root is where the numbers flip side
No need to check even numbers since we know they won't be prime numbers (except for 2, which can be fixed by printing 2 before everything else)
There must be a way to pass numbers ending in 5 since we know they won't be prime numbers as well but I've only been programming for a week so idk how to do it
import math
print(2)
for n in range(1, 1000, 2):
if n > 1:
for i in range(2,(math.ceil(math.sqrt(n))+1)):
if n%i == 0:
break
else:
print(num)
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.
Yes, the range function is non inclusive meaning it will go up to but not including the upper bounds you specify. Therefore, the for loop wont execute at all for the number 2, and it will go directly to the else statement and print out 2.
Think of it this way. In a for/else statement, the else statement executes if and only if the for loop executes all its iterations without hitting a break statement.
when the line reads...
for i in range(2,2): the number of iterations it must complete is 0. it completes 0 iterations and skips the break statement.
no break = else condition
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
this returns it in decimal form:
x = int(input("x: "))
y = 0
for num in range(1,x+1):
if num > 1:
for i in range(2,num):
if num % i == 0:
break
else:
y += 1
print(y/x)
this returns it in fractional form:
x = int(input("x: "))
y = 0
for num in range(1,x+1):
if num > 1:
for i in range(2,num):
if num % i == 0:
break
else:
y += 1
print(str(y) + "/" + str(x))
Just curious: why didn't you create a function?
hello dear sir I hope you are doing well,
Sir, I have more problems with Python. How can I contact you?
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~~
The else clause is attached to the for loop, not the if statement. In python, there is something called a for else statement. The else condition in a for else statement executes if the for loop executes in its entirety without hitting a break statement.
run both example codes below
example 1:
in the code below, the else clause will execute because the break statement is not hit
for i in range(10):
if i == 11:
break
else:
print("break statement was not encountered. Else condition executed")
example 2:
in the code below, the else clause will not execute because the break statement was encountered.
for i in range(10):
if i == 4:
break
else:
print("else condition")
print("else condition will not execute because break was encountered")
sir can you write it without using a break command
short and crisp!
Thank you sir love for Bangladesh
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...
How about while loop?
how to print only first prime number in this condition. i.e output must be only single number
Just break out of the loop once you print the first prime number: code below
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)
break
How to get all that output in single line
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, end = " ")
How to count them ?
This will print all prime numbers in the interval as well as printing the count at the end. Copy and paste the code into your IDE/text editor:
l = int(input("low bound: "))
u = int(input("upper bound: "))
count = 0
for num in range(l,u+1):
if num > 1:
for i in range(2,num):
if num % i == 0:
break
else:
count += 1
print(num)
print("number of prime numbers: {}".format(count))
Thank you 😊 thank you sooo much 🙏🙏🙏
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)?
yes. It would actually be even better to not run this code over anything greater than n/2.
Write a python program that asks the user for a number and determines whether it is prime or not.
The idea to solve this problem is to iterate through all the numbers starting from 2 to (N/2) using a for loop and for every number check if it divides N. If we find any number that divides, we return false. If we did not find any number between 2 and N/2 which divides N then it means that N is prime and we will return True.
Sir, can you explain to me how I can do it? thanks🙏
Thank you for this!!
When i was doing this in the range between 1 to 10.....2 is not including in the list when the output was displayed......I dont know why it was not including....pls give me the solution sir
just copy and paste the code below. Its the code from the video
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)
@@wrttech422 okay..Thank You sir
Thank you
In pydroid it doesnt work but in pycharm it works i must have a stupid IDE
Thanks a lot!
thank you!
For some reason, I copied this exactly into IDLE and it is only printing "2". Nothing else
what is your range? can you paste your code here?
@@wrttech422 I actually figured out that I had a capital "I" instead of a "1". But thank you very much!
Thanks
190th like