Python - Print All Prime Numbers In Any Interval

Поделиться
HTML-код
  • Опубликовано: 15 дек 2024

Комментарии • 49

  • @wrttech422
    @wrttech422  3 года назад +18

    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 3 года назад +12

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

    • @颐佳健康
      @颐佳健康 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.

  • @bts4ever990
    @bts4ever990 15 дней назад

    God tier explanation. Thanks!

  • @brendanthorne8353
    @brendanthorne8353 7 месяцев назад +1

    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?

  • @anasshahid8564
    @anasshahid8564 3 года назад +3

    Very helpful video for me.Thanks sir

  • @NaveenKumar-fv2wn
    @NaveenKumar-fv2wn 9 месяцев назад +2

    ❤❤❤keep continue sir

  • @houseofelectronics5869
    @houseofelectronics5869 3 года назад +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  3 года назад +2

      yes, you are correct

    • @cdemr
      @cdemr 2 года назад +1

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

  • @snehap7057
    @snehap7057 3 года назад +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  3 года назад +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  3 года назад +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

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

  • @dries59dep
    @dries59dep Год назад

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

  • @roya1244
    @roya1244 24 дня назад

    hello dear sir I hope you are doing well,
    Sir, I have more problems with Python. How can I contact you?

  • @白馬義從寶葵
    @白馬義從寶葵 2 года назад +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 года назад +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")

  • @jadeboom9685
    @jadeboom9685 2 года назад

    sir can you write it without using a break command

  • @vishnukantchaudhary285
    @vishnukantchaudhary285 3 года назад +2

    short and crisp!

  • @AmirAli-no8ye
    @AmirAli-no8ye Год назад +1

    Thank you sir love for Bangladesh

  • @cokfarm6373
    @cokfarm6373 3 года назад

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

  • @pakapeta7772
    @pakapeta7772 2 года назад

    How about while loop?

  • @bharadhwajsinguluri3384
    @bharadhwajsinguluri3384 3 года назад

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

    • @wrttech422
      @wrttech422  3 года назад +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

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

  • @Daily_babababa
    @Daily_babababa Год назад

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

  • @S.ZT119
    @S.ZT119 2 года назад +1

    Thank you 😊 thank you sooo much 🙏🙏🙏

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

  • @roya1244
    @roya1244 24 дня назад

    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🙏

  • @chesb6426
    @chesb6426 2 года назад +2

    Thank you for this!!

  • @bhavanipullepu1416
    @bhavanipullepu1416 3 года назад

    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  3 года назад +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 3 года назад

      @@wrttech422 okay..Thank You sir

  • @mus_3219
    @mus_3219 2 года назад +1

    Thank you

  • @10F2C
    @10F2C 2 года назад

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

  • @salad02
    @salad02 3 года назад +1

    Thanks a lot!

  • @q1mora
    @q1mora 2 года назад

    thank you!

  • @bryceberry5852
    @bryceberry5852 3 года назад

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

    • @wrttech422
      @wrttech422  3 года назад

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

    • @bryceberry5852
      @bryceberry5852 3 года назад

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

  • @killua7963
    @killua7963 3 года назад

    Thanks

  • @rohithkoushal5109
    @rohithkoushal5109 2 года назад +1

    190th like