Python - Print All Prime Numbers In Any Interval

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ธ.ค. 2020
  • in this video I show you how to create a function that prints out all prime numbers in any given interval

ความคิดเห็น • 46

  • @wrttech422
    @wrttech422  2 ปีที่แล้ว +15

    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)

    • @amnahansari4083
      @amnahansari4083 2 ปีที่แล้ว

      thankyou

    • @Spotloans
      @Spotloans ปีที่แล้ว

      dear sir , i want the largest prime number can u explain

  • @naveenkumarg3015
    @naveenkumarg3015 2 ปีที่แล้ว +10

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

    • @user-jx9ho8vt1j
      @user-jx9ho8vt1j 2 ปีที่แล้ว

      You can also use a flag e.g is_prime=True or False to track all the prime numbers. for/else clause is pythonic.

  • @chesb6426
    @chesb6426 2 ปีที่แล้ว +2

    Thank you for this!!

  • @NaveenKumar-fv2wn
    @NaveenKumar-fv2wn 3 หลายเดือนก่อน +1

    ❤❤❤keep continue sir

  • @anasshahid8564
    @anasshahid8564 3 ปีที่แล้ว +3

    Very helpful video for me.Thanks sir

  • @vishnukantchaudhary285
    @vishnukantchaudhary285 2 ปีที่แล้ว +2

    short and crisp!

  • @salad02
    @salad02 3 ปีที่แล้ว +1

    Thanks a lot!

  • @S.ZT119
    @S.ZT119 2 ปีที่แล้ว +1

    Thank you 😊 thank you sooo much 🙏🙏🙏

  • @q1mora
    @q1mora ปีที่แล้ว

    thank you!

  • @mus_3219
    @mus_3219 2 ปีที่แล้ว +1

    Thank you

  • @AmirAli-no8ye
    @AmirAli-no8ye ปีที่แล้ว

    Thank you sir love for Bangladesh

  • @houseofelectronics5869
    @houseofelectronics5869 2 ปีที่แล้ว +4

    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
      @wrttech422  2 ปีที่แล้ว +2

      yes, you are correct

    • @cdemr
      @cdemr ปีที่แล้ว

      @@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)

  • @brendanthorne8353
    @brendanthorne8353 2 หลายเดือนก่อน

    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?

  • @telsaabi9865
    @telsaabi9865 2 ปีที่แล้ว +1

    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

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +3

      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))

  • @user-ds2vu7uy2q
    @user-ds2vu7uy2q ปีที่แล้ว +1

    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~~

    • @wrttech422
      @wrttech422  ปีที่แล้ว +2

      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")

  • @snehap7057
    @snehap7057 2 ปีที่แล้ว +4

    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.

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +2

      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.

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +1

      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

  • @killua7963
    @killua7963 2 ปีที่แล้ว

    Thanks

  • @cokfarm6373
    @cokfarm6373 2 ปีที่แล้ว

    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...

  • @dries59dep
    @dries59dep ปีที่แล้ว

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

  • @jadeboom9685
    @jadeboom9685 ปีที่แล้ว

    sir can you write it without using a break command

  • @ayearninghope
    @ayearninghope 2 ปีที่แล้ว

    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)?

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +2

      yes. It would actually be even better to not run this code over anything greater than n/2.

  • @pakapeta7772
    @pakapeta7772 2 ปีที่แล้ว

    How about while loop?

  • @rohithkoushal5109
    @rohithkoushal5109 2 ปีที่แล้ว +1

    190th like

  • @bhavanipullepu1416
    @bhavanipullepu1416 2 ปีที่แล้ว

    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

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +1

      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)

    • @bhavanipullepu1416
      @bhavanipullepu1416 2 ปีที่แล้ว

      @@wrttech422 okay..Thank You sir

  • @chadvukondiifirstuu3231
    @chadvukondiifirstuu3231 2 ปีที่แล้ว

    How to get all that output in single line

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +1

      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 = " ")

  • @bharadhwajsinguluri3384
    @bharadhwajsinguluri3384 2 ปีที่แล้ว

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

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว +1

      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

  • @10F2C
    @10F2C 2 ปีที่แล้ว

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

  • @reaxer3687
    @reaxer3687 ปีที่แล้ว

    How to count them ?

    • @wrttech422
      @wrttech422  ปีที่แล้ว

      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))

  • @bryceberry5852
    @bryceberry5852 2 ปีที่แล้ว

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

    • @wrttech422
      @wrttech422  2 ปีที่แล้ว

      what is your range? can you paste your code here?

    • @bryceberry5852
      @bryceberry5852 2 ปีที่แล้ว

      @@wrttech422 I actually figured out that I had a capital "I" instead of a "1". But thank you very much!