#25 Python Tutorial for Beginners | Prime Number in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 พ.ย. 2024

ความคิดเห็น • 1.1K

  • @BukkaReddy..
    @BukkaReddy.. 2 ปีที่แล้ว +77

    Sir you always simplifies the problems in a very easy way ,even a slow learner like me get the logics in first attempt.This is the real quality of great teacher.Thankyou.. Jai Hind🇮🇳

  • @pannadarao5583
    @pannadarao5583 4 ปีที่แล้ว +82

    x = int (input ("enter your number ? "))
    for i in range (2,x):
    if x%i==0:
    print ('its a non prime number')
    break
    else:
    print ('its a prime number')

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

      x%i ?? can you explain me this part ?

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

      @@shafiquebarudgar8295 it is dividing x with value of i then checking its remainder is equal to 0 or not ( % equals to find remainder not percent)

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

      how would this code work for 2? if user enters 2, then it will satisfy the condition if x%i==0 since 2%2 =0, and it will print it's non prime number while 2 is a prime number

    • @manpreetsinghpanesar5748
      @manpreetsinghpanesar5748 3 ปีที่แล้ว

      @@saniyakhan12939 actually the range function here is using (2,x) in that case if the value of x is also two this function will not give any output as starting is 2 and ending (which is not included in range function ) is also 2 ,[loop will start from 2 and end on 2 not including it which is not feasible] so program will work normally it will print prime as (null % i != 0)

    • @saniyakhan12939
      @saniyakhan12939 3 ปีที่แล้ว

      @@manpreetsinghpanesar5748 so if the loop will work, then why won't the first condition be checked or run? Why only the else part will work?

  • @aakashtiwari3974
    @aakashtiwari3974 5 ปีที่แล้ว +240

    your python series are great sir ,thanks for this

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

    Finally, a clear, concise, easily understood explanation for finding prime. Thanx.

  • @Hindimekahaniya2
    @Hindimekahaniya2 4 ปีที่แล้ว +17

    one of the Best star teacher on TH-cam hats off to you sir

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

    def prime(num) ->bool:
    if num == 1:
    return False
    elif num == 2:
    return True
    else:
    ran = int(num ** 0.5)
    for i in range(2,ran+1):
    if num%i != 0:
    return True
    x= int(input("Enter Number: "))
    n = prime(x)
    if n:
    print("Prime")
    else:
    print("Not Prime")

  • @jayantakumarpal7751
    @jayantakumarpal7751 4 ปีที่แล้ว +11

    n=int(input("Enter a number"))
    c=0
    for i in range(1,n+1):
    if n%i==0:
    c+=1
    if c==2:
    print("prime")
    else:
    print("Not prime")

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

      Bro the tutorial itself is wrong, your code works perfectly thank you!

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

      Yeah he made a mistake for i in range (1,num+1) should come and the +1 is excluded and it prints 1:num)

  • @sahebsarkar8330
    @sahebsarkar8330 5 ปีที่แล้ว +15

    x=int(input("input your number
    "))
    for i in range(2,int(x/2+1)):
    if(x%i==0):
    print("enter number not prime number")
    break;
    else:
    print("enter number is a prime numbere
    ")
    print("SIR YOU RELAY HELP US SO MUCH SO THANK YOU SOOOOOOOOOOOMUCH FOR YOUR EFFORT")

    • @narendrasompalli5536
      @narendrasompalli5536 5 ปีที่แล้ว

      Super

    • @narendrasompalli5536
      @narendrasompalli5536 5 ปีที่แล้ว +1

      But it better x//2insted of x/2

    • @sahebsarkar8330
      @sahebsarkar8330 5 ปีที่แล้ว

      You also right. That's why I caste here by int.

    • @marjugasvoiceout3547
      @marjugasvoiceout3547 5 ปีที่แล้ว

      Please explain me this part int(x/2+1)

    • @sahebsarkar8330
      @sahebsarkar8330 5 ปีที่แล้ว

      @@marjugasvoiceout3547 let x=3 then x/2=1.5 now 1.5+1=2.5 now int(2.5)=2 for the casting float value become int value.

  • @k.manideep2543
    @k.manideep2543 5 ปีที่แล้ว +29

    My Prime code, for checking prime number there is no need to run the loop till its number. We an even achieve it by running the loop till (num/2)+1. In this way you can reduce half of the effort. Will be useful while checking for large numbers.
    Code :
    num = int(input("check number is prime or not :"))
    for i in range(2,int(num/2)+1):
    if num % i == 0:
    print("not prime")
    break
    else:
    print("prime")

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

      You dont have the need to add 1.

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

      Wouldn't need the second "int" call in the range function. But you probably already know that now :D

    • @martinesse4830
      @martinesse4830 3 ปีที่แล้ว

      @@jatinlekhwar6798 I think you do, if you don't put 1 then it will return 4 as a prime because the range will be (2,2) and it will go into else, correct me if I am wrong

    • @martinesse4830
      @martinesse4830 3 ปีที่แล้ว

      @@MikoKikoJo what do you mean, it will throw an error

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

      @@MikoKikoJo you have to, if you put any number like 5 in that formula, answer will be 3.5 and range cannot go till 3.5 it needs integer.

  • @satishvarma456
    @satishvarma456 3 หลายเดือนก่อน +3

    Sir in this case if we give num=1
    Then output is prime
    But 1 is not a prime

  • @forsake5426
    @forsake5426 4 ปีที่แล้ว +15

    Great Mr Naveen,this is the real trick if you want to be a good programmer..Good job,,,keep doing this,People need to learn.

  • @chughprabhkirat4141
    @chughprabhkirat4141 5 ปีที่แล้ว +18

    num = int(input("Enter any number "))
    if num>1:
    for i in range(2,num):
    if num%i==0:
    print("Not a prime number.")
    break
    else:
    print('it is a prime number')
    else:
    print('not a prime number')

    • @akbarmohammad5081
      @akbarmohammad5081 4 ปีที่แล้ว

      Why you wrote else conditions two times?

    • @thanishkrishnamurthy1689
      @thanishkrishnamurthy1689 4 ปีที่แล้ว +1

      @@akbarmohammad5081 One is for "if-else" statement and another is for "For-else" But their is no need for "If-else" if u want you can include

    • @DheerajKumar-yk3yl
      @DheerajKumar-yk3yl 4 ปีที่แล้ว +1

      Could you say why you use break, I'm not under stand by from video

    • @thedmitryguy
      @thedmitryguy 4 ปีที่แล้ว

      dheeraj kumar, because we don’t have to continue dividing ‘num’ by ‘i’. ‘break’ stops ‘for’. For example, ‘num’ = 10, and if 10 % 2 = 0 then this number is definitely not a prime number because prime number can only be divided by itself and by 1, and we don’t have to continue dividing ‘num’ by 3, 4 and so on. Do you understand it now?

    • @lyricathelyricsworld8945
      @lyricathelyricsworld8945 4 ปีที่แล้ว +1

      num=int(input('enter the number'))
      if num%2==0 or num%3==0 or num%5==0 or num%7==0 and num%num!=0:
      print('not prime')
      else:
      print('prime')
      try this mam

  • @a.v.n.ssripavan5506
    @a.v.n.ssripavan5506 4 ปีที่แล้ว +12

    Your beginner video's are best in TH-cam sir, in this lockdown wanted to be productive and thought of learning python after a lot of playlists and channels your channel has got all the information and tutorial a beginner needs

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

    Using while loop for primes
    n = int(input('Enter a number'))
    i = 2
    while (i

  • @sivakrishna3575
    @sivakrishna3575 4 ปีที่แล้ว +15

    if you want to try with function:
    def prime(num):
    for i in range(2,num):
    if num % i == 0:
    return "not a prime number"
    else:
    return "prime number"
    num = int(input("enter a number: "))
    print(prime(num))

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

      Hey , do you know how to do this ques?

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

      Consider a tuple T1 = (1, 2, 5, 7, 9, 3, 4, 6, 8, 10). Write a function primes() that accepts tuple T1 as argument and creates a list T2 having values that are prime numbers in the tuple T1. The function should return a dictionary primeCubes where keys are items of the list T2 and the values are cubes of the keys. For example, when T1 as passed as an argument, T2 will be [2, 5, 7, 3] and primeCubes will be {2:8,5:125,7:343,3:27}.

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

    import math as m
    x=int(input("Enter The Number :"))
    b=m.floor(m.sqrt(x))
    for a in range(2,b+1):
    if a**x%x==a%x:
    print("The no. is prime")
    break
    else:
    print("Not prime")
    Thank You sir for your great lectures..

  • @shivan1487
    @shivan1487 5 ปีที่แล้ว +7

    to my knowledge this is efficient:)
    x=int(input("enter a vlaue"))
    for i in range(2,x):
    if x%i==0:
    print("not a prime")
    break
    else:
    print("is a prime")

  • @rahul141141
    @rahul141141 6 หลายเดือนก่อน +1

    Little more optimized..
    import math
    num = int(input("Enter the number:"))
    n = math.floor(math.sqrt(num))
    for i in range(2,n + 1,1):
    if num % i == 0:
    print("The number is not prime, divisible by", i)
    break
    else:
    print("The number is prime")

  • @sibgatullahshovon
    @sibgatullahshovon 4 ปีที่แล้ว +5

    @telusko: Navin sir , as per condition
    for i in range(2,num):
    if num % i ==0:
    print("not prime")
    If input num is 2 then num%i==0
    So, as per condition it should be not prime though we know it is prime number and python also say so. Could you please explain????

    • @codifa5847
      @codifa5847 4 ปีที่แล้ว

      you can add special condition for 2 because its a start from 2

    • @erz_mafia
      @erz_mafia 4 ปีที่แล้ว

      bro when you debug this, range function doesn't allow it to read ' if ' statement it straight away jump to 'else ' statement.

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

      num=int(input('enter the number'))
      if num%2==0 or num%3==0 or num%5==0 or num%7==0 and num%num!=0:
      print('not prime')
      else:
      print('prime')
      try this

  • @AbhijitDasTitu
    @AbhijitDasTitu 5 หลายเดือนก่อน +1

    a = int(input("Enter a number: "))
    if a

  • @adarshkanojia4192
    @adarshkanojia4192 5 ปีที่แล้ว +15

    We wants such type of videos from you dear navin sir

  • @voiceofvicky
    @voiceofvicky 8 หลายเดือนก่อน +1

    count = 0
    n = int(input("Enter a number:"))
    for i in range(1, n+1):
    if n % i == 0:
    count +=1
    if count == 2:
    print(n, "is a Prime number")
    else:
    print(n, "is Not a Prime number")

  • @_NishaKarki
    @_NishaKarki 8 หลายเดือนก่อน +5

    num = int(input("Enter the num:")) # taking input
    if num < 2:
    print("Not prime")
    else:
    for i in range(2, num): # iterate over possible divisors
    if num % i == 0:
    print("Not Prime")
    break # if a divisor is found, no need to continue checking
    else:
    print("prime")
    Guys this will also work for 0 and 1 and also for negative numbers.

  • @BhargaviBaireddy
    @BhargaviBaireddy 5 หลายเดือนก่อน +1

    n=int(input("enter the number:"))
    c=0
    for i in range(1,n+1):
    if(n%i==0):
    c+=1
    if(c==2):
    print(" prime")
    else:
    print("not prime")

  • @hritwikhaldar9272
    @hritwikhaldar9272 4 ปีที่แล้ว +7

    x=int(input("Enter any interger number: "))
    for i in range(2,x):
    if x%i==0:
    print("=> it is not a prime")
    break
    else:
    print("=> it is a prime")

    • @AbhishekGupta-di6ol
      @AbhishekGupta-di6ol 4 ปีที่แล้ว

      Hritwik Haldar it is same bro .... not efficient

    • @viraldance2303
      @viraldance2303 3 ปีที่แล้ว

      bto agr input 1 ho toh kaise kroge ??

  • @BDIt-vj5hk
    @BDIt-vj5hk 4 หลายเดือนก่อน +2

    n=int(input("enter the value of n"))
    if(n==1):
    print("it is not prime number")
    for i in range(2,n-1):
    if(n%i==0):
    print("it is not a prime number")
    break
    else:
    print("it is a prime number")

  • @JoyBoy_11311
    @JoyBoy_11311 4 ปีที่แล้ว +8

    at first i used to hate for loop and while loop and after watching your and seeing the examples i used to love them sir thanks a lot❤️btw i am aman raj😀

  • @RajeevDhavla-gq7jn
    @RajeevDhavla-gq7jn 10 หลายเดือนก่อน +1

    a = int(input("Enter the number = "))
    count = 0
    if (a>0) :
    if (a == 1) :
    print("{} is neither a prime nor a composite number.".format(a))
    i = 1
    while (i 2) :
    print("{} is a composite number.".format(a))
    elif (a

  • @mohammedimdaad270
    @mohammedimdaad270 4 ปีที่แล้ว +22

    ♐To get code in an efficient way - just reduce the no. of iterations by half😉
    code:
    num = 25
    for i in range(2,(num//2)):
    if num%i==0:
    print("Not Prime")
    break
    else:
    print("Prime")
    o/p:
    not prime

    • @adithyaramapuram4995
      @adithyaramapuram4995 4 ปีที่แล้ว +1

      you can make to square root it is more efficient than halfing number

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

      use seive algo

    • @RickC-ew7zs
      @RickC-ew7zs 2 ปีที่แล้ว

      @@adithyaramapuram4995 brother, you'll have to add 1 after taking the square root. if you directly put sqrt value in range, it won't get included in the code and henceforth resulting in wrong output.

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

    from math import sqrt
    n = 5
    for i in range(2, int(sqrt(n) + 1)):
    if n % i == 0:
    print(n, "is not a prime number")
    break
    else:
    print(n, "is a prime number")

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

    I have written below code. I am implementing things after learning from your course. Thank you Sir.
    nums=range(1,10)
    for num in nums:
    for i in range(2,num):
    if num%i==0:
    print(num,"Not prime number")
    break
    else:
    print(num,"Prime number")

  • @janamshah190
    @janamshah190 4 ปีที่แล้ว +5

    x= int(input('number=?'))
    If x==1:
    print('neither prime nor composite')
    Elif x==2:
    print('prime')
    Else:
    For i in range(2,x):
    If x%i==0:
    print('not prime')
    Break
    Else:
    print('prime')
    Break
    Print('bye')

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

      The last line is an error print() p is small😂

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

      ​@@anirudh4671 Huh acting as friendly Console showing error😹

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

    x=int(input("enter an number howle"))
    if x%2==0 :
    print(x,"not prime")
    elif x%3==0 or x%5==0:
    print(x,"not prime")
    else:
    print(x,"prime")

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

      I like the input part 🤣🤣

    • @SarojSingh-vl2kj
      @SarojSingh-vl2kj 8 หลายเดือนก่อน

      input is 49 output is prime 💀
      but I thought 49 is non prime bro

  • @najimali32
    @najimali32 4 ปีที่แล้ว +11

    we can improve the time by only checking odd divisor till the square root of the given num.
    for eg - num is 29 then check num check by it by 3,5 ,7.

  • @snapindia23
    @snapindia23 4 ปีที่แล้ว +7

    n=int(input("enter a number"))
    for i in range(2,int(n/2)):
    if(n%i==0):
    print("not prime")
    break
    else:
    print("prime")

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

    For this program
    x=int(input("Enter a number"))
    count=0
    for i in range(1,x):
    if x%i==0:
    count=count+1
    if(count>2):
    print("The number is not prime")
    else:
    print("The number is prime")

  • @MrAKTech
    @MrAKTech 4 ปีที่แล้ว

    n = int(input("Enter a number :"))
    i=2
    while i

    • @gameplay-eq8sq
      @gameplay-eq8sq 4 ปีที่แล้ว

      Hey the value i should increase till that no.

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

    from math import *
    x = int(input("Enter the number: "))
    for i in range(2,floor(sqrt(x))+1):
    if x%i==0:
    print("Composite")
    break;
    else:
    print("Prime")

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

    All these days i was thinking who in the world invented this programming ?......but after watching ur videos i really feel learning programming is fun!!!

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

    Your tutorials makes coding life much more simple :)

  • @nishanthkathula2728
    @nishanthkathula2728 5 หลายเดือนก่อน

    import math
    for i in range(2,101):
    count=0
    for j in range(2,int(math.sqrt(i))+1):
    if i%j==0:
    count=count+1
    if count==0:
    print(i)

  • @rishisetpal3537
    @rishisetpal3537 4 ปีที่แล้ว +8

    Your Python series are Great sir, Thank you so much for this ..😇
    Sir i tried my best to find the time efficient manner to solve this problem 😉
    Please take a look on it and give some suggestions if any..
    Code:
    from math import sqrt
    n = int( input ( 'Enter a number :') )
    if n==0 or n==1 :
    print( 'Not Prime' )
    elif n==2 :
    print( 'Prime' )
    elif n%2 ==0 :
    print ( 'Not prime' )
    else:
    for i in range(3, int(sqrt(n))+1, 2)
    if n%i == 0 :
    print (' Not prime ')
    break
    else :
    print ( 'prime' )

    • @mananagarwal1212
      @mananagarwal1212 4 ปีที่แล้ว +1

      why you increasing the value of i by 2 everytime, why not 1?

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

      @@mananagarwal1212
      I did the correction as I didn't used editor i forgot one step..
      Explanation of Code:
      Firstly it will check that the number is divisible by 2 or not if not then it will again check from 3,5,7,9.... till ✓n

    • @mananagarwal1212
      @mananagarwal1212 4 ปีที่แล้ว +1

      @@rishisetpal3537 okay thanks

    • @dishantkumbhar8822
      @dishantkumbhar8822 4 ปีที่แล้ว

      Its not efficient bro, why are u making it complex . Let it be simple

    • @rishisetpal3537
      @rishisetpal3537 4 ปีที่แล้ว

      @@dishantkumbhar8822 i did not understand.. like how.?
      Can you try to explain with the help of code.?

  • @rahuldev007
    @rahuldev007 ปีที่แล้ว +5

    num = int(input())
    for i in range(2,num):
    if num%i==0:
    print("not prime")
    break
    else:
    print("prime")

  • @abhinavsamudrala
    @abhinavsamudrala 4 ปีที่แล้ว

    sir this is my efficiently modified by the clue you have given 👇👇
    num = 19
    for i in range (2,int(num/2)):
    if num % i == 0:
    print("not prime")
    break
    else:
    print("prime")

  • @rahulprajapati991
    @rahulprajapati991 4 ปีที่แล้ว +6

    n=int(input())
    for i in range(2,n//2):
    if n%i==0:
    print(n,"is not prime")
    break
    else:
    if n

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

    num=int(input("number"))
    for i in range(2,num):
    if num%i==0:
    print("not prime")
    break
    else:
    print("prime")
    better code

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

      Your code is same as sir explained in the video you just took input from user bro. Sir meant another method of code which involve less step.

  • @JatinKumar-gz5gg
    @JatinKumar-gz5gg 4 ปีที่แล้ว +6

    a=int(input("enter a number"))
    count=0
    i=1
    while i

    • @mohammadshafaq3685
      @mohammadshafaq3685 4 ปีที่แล้ว

      a=int(input("enter a number"))
      count=0
      i=2
      while i

  • @mohammadmamunchowdhury2141
    @mohammadmamunchowdhury2141 4 ปีที่แล้ว +5

    lower=int(input("Enter lower limit"))
    upper=int(input("Enter upper limit"))
    for num in range(lower, upper+1):
    for i in range(2,num):
    if num%i==0:
    break
    else:
    print(num)

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

    Import math
    X = int (input("please enter a number: "))
    For I in range (2,math.ceil(x/2)):
    If x % i ==0:
    Print (" it's not a prime ")
    Break
    Else:
    Print ( " its a prime number")

  • @farhanahmed-ws1mb
    @farhanahmed-ws1mb 5 ปีที่แล้ว +10

    i=2
    while(i

  • @khushboopandey7983
    @khushboopandey7983 4 ปีที่แล้ว +12

    Num =int(input("enter a No."))
    For i in range(2,num):
    If num%i==0:
    Print("not prime")
    Break
    else:
    Print("prime")

    • @pranshupandey5907
      @pranshupandey5907 4 ปีที่แล้ว +1

      I did same but not working well

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

      @@pranshupandey5907 It is because, In first line "Num" is in Cap letter Try to make as same for all "num"

    • @supercoolkartik
      @supercoolkartik 4 ปีที่แล้ว +1

      Just printing the num which we enter

    • @dippaul301
      @dippaul301 4 ปีที่แล้ว +1

      Just add one code between 1st nd 2nd line that is -if num>1:

    • @kondareddymunagala1336
      @kondareddymunagala1336 4 ปีที่แล้ว

      What if i enter 2 because it's a prime number

  • @mithilajayasundara6158
    @mithilajayasundara6158 4 ปีที่แล้ว +1

    from math import sqrt, floor
    num = int(input("Give me a number.."))
    if num == 1:
    print("Not a prime")
    quit()
    else:
    if num == 2:
    print("Prime")
    quit()
    if num > 2 and num % 2 == 0:
    print("Not a Prime Number.")
    else:
    e = 1 + floor(sqrt(num))
    for i in range(3, e, 2):
    if num % i == 0:
    print("Not a Prime Number...")
    break
    else:
    print("Prime Number")

  • @a_r_u_n7595
    @a_r_u_n7595 4 ปีที่แล้ว +8

    This is a code which prints all prime numbers from 1 to 50
    for num in range(1,50):
    for i in range(2,num):
    if num%i==0:
    break
    else:
    print(num)

    • @mathmagic5579
      @mathmagic5579 4 ปีที่แล้ว

      2 print nhi hoga

    • @leozarni9570
      @leozarni9570 4 ปีที่แล้ว

      what if we want to just print the NON prime numbers?

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

    for i in range(1, 20):
    if i % 2 != 0:
    print(i) This is efficient and I want to say you are a great teacher I actually learned everything i know from you. Which is only python and JavaScript but still I will learn more from you.

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

      Wrong

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

      @@uttkarsh7823 its actually right it worked for me. what did you see wrong. it would be nice if you pointed it out.

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

      That’s for even ni buddy

  • @neelamgupta2505
    @neelamgupta2505 4 ปีที่แล้ว +12

    for i in range(2, num//2):
    if (num % i) == 0:
    print(num, "is not a prime number")
    break
    else:
    print(num, "is a prime number")

    • @nashwanth6640
      @nashwanth6640 4 ปีที่แล้ว +1

      For input 4 it is showing as 4 is prime number but 4 is not prime (factors are 1,2,4)

    • @chikkamchandrasekhar61
      @chikkamchandrasekhar61 4 ปีที่แล้ว +1

      U should add 1 in range
      range(2,(num//2)+1):
      Then 4 will be a "not prime"....

    • @chikkamchandrasekhar61
      @chikkamchandrasekhar61 4 ปีที่แล้ว +1

      @Prem Kumar It works....
      For n=2 it directly enters the else part and it becomes a prime number

    • @chikkamchandrasekhar61
      @chikkamchandrasekhar61 4 ปีที่แล้ว +1

      @Prem Kumar bro here we have to use "for-else" not "if-else"
      Then n=2 enters the else part

    • @chikkamchandrasekhar61
      @chikkamchandrasekhar61 4 ปีที่แล้ว +1

      @Prem Kumar No bro "if loop" will not execute bcoz
      In range(2,(2//2)+1) is range(2,2) i.e it doesn't enter "if loop"

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

    A code for printing Prime no.s
    for I in range(1, 101) :
    if i%2 ! =0:
    print(I)
    Done!!!

  • @newsinfo27
    @newsinfo27 ปีที่แล้ว +18

    n=int(input("Enter a number to check : "))
    for i in range(2,int(n**0.5)+1):
    if n%i==0:
    print("Not Prime")
    break
    else:
    print("Prime")

    • @venugopal-kh5zc
      @venugopal-kh5zc 9 หลายเดือนก่อน +1

      Explain that exponent please

    • @newsinfo27
      @newsinfo27 9 หลายเดือนก่อน

      @@venugopal-kh5zc in python "**" is used for exponential.
      Eg. 5^2 is written as 5**2

    • @venugopal-kh5zc
      @venugopal-kh5zc 9 หลายเดือนก่อน

      @@newsinfo27 that is I now sir ...I want why using exponent. What is logic here

    • @venugopal-kh5zc
      @venugopal-kh5zc 9 หลายเดือนก่อน

      Explain once

    • @newsinfo27
      @newsinfo27 9 หลายเดือนก่อน

      @@venugopal-kh5zc ohh sorry.
      If a given number n is not divisible by any number from 2 to root(n) then it's prime.

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

    isprime=int(input())
    for i in range(2,int(isprime/2)):
    if isprime%i==0:
    print("not prime")
    break
    else:
    print("Prime")

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

      range(2,i nt(isprime/2)) --> if the input is 4 then range(2, 4/2) --> range(2, 2) --> then else statement will directly executed and print prime

  • @roshanreddy1526
    @roshanreddy1526 7 หลายเดือนก่อน

    for n in range(100):
    Count=0
    for i in range(1,n+1):
    if(n/i==0):
    count+=1
    if count==2:
    print(n)

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

    There is no need to iterate the for loop upto n-1 just iterate it upto n/2
    Cause a number having a value more than half cannot divide that number
    Then the time complexity will reduce to n/2 time which is n-1 times in your code

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

      Actually iterating upto floor(n**0.5) is even more efficient

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

      @@khamza8926 hi khamza, can you explain your point of view

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

      ​@@anwarsheik3329search for sieve of eratosthenes proof

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

    this is the best beginner videos on TH-cam

  • @samthejam5202
    @samthejam5202 4 ปีที่แล้ว +7

    I know this is a long code, but I think it will be easier to process.
    a = int(input("type here-"))
    b = [2, 3, 5, 7]
    for i in b:
    if a ==i:
    print("prime")
    elif a%i==0 or a==1:
    print("not prime")
    break
    else:
    print("prime")

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

      Bro how about 121 Or 169?

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

      this is a hardcode method

    • @bov._.v
      @bov._.v 10 หลายเดือนก่อน +1

      it prints prime 2 times

  • @RedicoAccount
    @RedicoAccount 6 หลายเดือนก่อน

    display the prime numbers within a range
    x = int (input ("enter your number ? "))
    y = int (input ("enter your number ? "))
    for i in range (x,y):
    for j in range(2,i):
    if i%j==0:
    break
    else:
    print (i)

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

    a = [ ]
    for x in range (2,50):
    isPrime = True
    for num in range(2, x):
    if x % num == 0:
    isPrime = False
    break
    if isPrime:
    a.append(x)
    print(a)

  • @vijayborate9172
    @vijayborate9172 3 หลายเดือนก่อน

    n = int(input("Enter a number"))
    n1 = int(n / 2)
    for i in range(2, n1):
    x = n % i
    if (x == 0):
    print("Not a prime no")
    break
    else:
    print("Prime no")

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

    sir this code won't work for num=2. Since although 2 is divisible by 2 it is a prime number

  • @Leelasaikrishna-qt4wb
    @Leelasaikrishna-qt4wb 5 หลายเดือนก่อน

    num=3
    factors=[]
    for i in range(1,num+1):
    if num%i==0:
    factors.append(i)
    if len(factors)>2:
    print("not a prime")
    else:
    print("prime number")

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

    perfect channel..........!!!!!!!!!!!!!!
    I was totally confused with for loop and while loop, i mean didn't know to use it well..after seeing some of ur vids i was happy....and got some idea abt python language....i will see all ur python vids as it's too interesting..
    btw my doubt was what is the code to print all prime numbers from 0 to 100...?

  • @gauravjha4260
    @gauravjha4260 ปีที่แล้ว +17

    num = int(input("Enter the number: "))
    factors = []
    for x in range(1, num+1):
    if num % x == 0:
    factors.append(x)


    # A prime number is a number which has only 2 factors one itself and second 1
    # print(len(factors))
    if len(factors) == 2:
    print(f"Congratulations {num} is a prime number, with factors only {factors}")
    else:
    print(f"Aaah! sorry {num} is not a prime number because it has factors {factors}")

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

      This one is correct. Me also do like same as you.😎

  • @samarthsrivastava5888
    @samarthsrivastava5888 4 ปีที่แล้ว

    my efficient way in this code user can choose its own number
    x=int(input('enter a number'))
    for i in range (2,x,1):
    if x%i==0:
    print ('not prime')
    break
    else:
    print (' prime')

  • @AbdulKareem-maks
    @AbdulKareem-maks 4 ปีที่แล้ว +5

    Sir, the program you wrote is not valid if the given number is 2. Since we are starting from 2, the given number(2) will be divisible and the program will give the output as Not Prime whereas 2 is a Prime number. Any other way around this?

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

      a=int(input("enter a number "))
      if a==2:
      print("prime")
      for i in range(2,a):
      if a%i==0:
      print("not prime")
      break
      else:
      print("prime")
      break

  • @hinamiemad6981
    @hinamiemad6981 4 ปีที่แล้ว

    from math import sqrt
    n = int(input("Enter the number"))
    if n2 and n%2==0:
    print("Not Prime")
    else:
    for i in range(3,int(sqrt(n))+1,2):
    if n%i==0:
    print("Not prime")
    break
    else: print("Prime")

  • @MegaKalyan111
    @MegaKalyan111 4 ปีที่แล้ว

    while True:
    n = int(input("Please enter minimum Range"))
    m = int(input("Please enter maximum Range"))
    d = int(input("please enter range difference"))
    x = range(n, m, d)
    s = False
    for i in x:
    if i > 2:
    for j in range(2, i):
    if i % j == 0:
    break
    else:
    print(i)
    c = int(input("1 for continue 0 to end"))
    if c == 0:
    break

  • @selcukdoven
    @selcukdoven 5 ปีที่แล้ว +7

    Thank you. A very useful video. Could you please add this video in python video list so We can reach them easily.

  • @anonymoususer900
    @anonymoususer900 4 ปีที่แล้ว +1

    This works too. Its with while loop. My classes havent reached till for loop yet and i was asked to make it so i made with while loop.
    while True:
    a=int(input('Enter the number'))
    c=int(a/2)
    b=2
    while b

    • @sumeetrox2479
      @sumeetrox2479 3 ปีที่แล้ว

      Hi can you tell me if the -------> 'else' at the bottom is with 'while true' indentation or with 'while b

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

      @@sumeetrox2479 oustide whileb

    • @abhinavmappidichery9371
      @abhinavmappidichery9371 3 ปีที่แล้ว

      print a prime number, greater than or equal to a given number

  • @adarshabbigeriadarshabbige8004
    @adarshabbigeriadarshabbige8004 4 ปีที่แล้ว +7

    Your python series and other technical videos are excellent sir thank you sir it's help me a lot

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

    'x=int(input("enter the number "))'
    for x in range(3,500):
    mod=2
    kalan=x%mod
    if(kalan==0):
    'print(x,"not prime")'
    while kalan!=0:
    while mod

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

      It's seems,, there is syntax error, seems after running this code ,it will not be executed

  • @abhishek-kapoor
    @abhishek-kapoor 5 ปีที่แล้ว +3

    a=int(input('Enter number:'))
    check = False
    for i in range (2,a):
    if a%i == 0:
    print('its not a prime number')
    check = True
    break
    if check == False:
    print('Its a prime number')

  • @ayushimonpara6911
    @ayushimonpara6911 4 ปีที่แล้ว

    num=int(input("enter the number"))
    for i in range(2,num):
    if num%i==0:
    print("it's not a prime no.")
    print("because it's divisible by",num//i,'and',num,'itself')
    break
    else:
    ("it,s a prime number")

  • @medicrohan2686
    @medicrohan2686 5 ปีที่แล้ว +7

    The other better way is to check if number is divisible by 2 first and then only check for odd numbers upto square root of the number.

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

      n=int(input("enter the number you want to check: "))
      for i in range(2,n//2):
      if n%i==0:
      print('its not prime')
      break
      else:
      print("its prime")

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

      i m learning now brother.

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

      ​@@raghavendraam719i did not understand at all😢

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

    sir,
    itsead of giving number inside code
    we can write
    a = int(input(enter a number))
    for i in range(2,a):
    if a %i==0:
    print("its not a prime number")
    break
    else:
    print("its a prime number")
    print("i love telusko")
    out put (1) :---- output (2) :-----
    enter a number 11 | enter a number 12
    |
    its a prime number | its not a prime number
    |
    i love telusko | i love telusko

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

    a=int(input("enter a number"))
    count=0
    for i in range(1,a,1):
    if a%i==0:
    count+=1
    if count==1:
    print("prime")
    else:
    print("not a prime")

  • @011gaurav
    @011gaurav 4 ปีที่แล้ว

    p = 33
    count = 1
    for i in range(2, p // 2):
    if p%i == 0:
    print('Not Prime')
    break
    count = count + 1
    print('Count = ', count)
    else:
    print('Prime')

  • @flamboyantperson5936
    @flamboyantperson5936 5 ปีที่แล้ว +7

    Great great to see you back on Python series. Lovely. Thanks Navin.

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

    a number can be divisible by itself and 1 for sure but except these it can only be divisible by the numbers ranging from 2 to upto half of that given number. so instead of checking from 2 to n we can check from 2 to n/2.
    def findPrime():
    num = int(input("Enter a number : "))
    if num == 1:
    return print("Not a Prime number")
    for x in range(2, (num // 2) + 1):
    if num % x == 0:
    print("Not a Prime number")
    break
    else:
    print(f"{num} is a prime number")
    findPrime()

  • @shivamsingh-fv5uy
    @shivamsingh-fv5uy 5 ปีที่แล้ว +3

    a=int(input("enter number to check: "))
    check=True
    for i in range(2,int((a/2))) :
    if a%i==0 :
    check=False
    break
    else :
    check=True
    if check==True :
    print("prime")
    else :
    print("not")

    • @Omprakash-wh4rb
      @Omprakash-wh4rb 5 ปีที่แล้ว

      What about 1 I think it will show prime

  • @sabalregmi1300
    @sabalregmi1300 4 ปีที่แล้ว

    num = int(input("Enter a number:"))
    for i in range(1, num):
    if num / i == i:
    print("Square of", i)
    break
    else:
    print("square root donot exist")

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

    a = input("Which number do you want to figure out if Prime or not? ")
    a = int(a)
    b = a//2
    for j in range (2,b):
    if a % j == 0:
    print(a, " is not a Prime Number")
    break
    else:
    print(a, " is a Prime Number")

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

      U can do it in the fst line itself int(input("enter a number ")) ..no need to do a = int

  • @SomeshSamadder
    @SomeshSamadder 3 ปีที่แล้ว

    trying efficient way...it is right sir?...
    def prime(num1):
    if num1 ==1 or num1==0:
    return False
    for i in range(3,num1, 2):
    if num1%i==0 or num1%2==0:
    return False
    else:
    return True

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

    n=29
    for x in range(2,n):
    if n%x==0:
    print("Not prime")
    break
    else:
    print("prime")

    • @mohitsonheriya7605
      @mohitsonheriya7605 4 ปีที่แล้ว

      Range (0,n) ya fir range (1,n) s 2 ka b problem solve ho jayega

  • @vamshikrish3646
    @vamshikrish3646 4 ปีที่แล้ว

    to print the list of prime numbers from 1 to given number
    n=int(input('enter the number'))
    list= [ ]
    for i in range (1,n+1):
    count=0
    for j in range(1,i+1):
    if i%j==0:
    count+=1
    if count==2:
    list=list+[i]
    print('the prime numbers are ',list)

  • @sumandas8286
    @sumandas8286 5 ปีที่แล้ว +5

    num=int(input("enter the number"))
    for i in range(2,int(pow(num,0.5))):
    if num%i==0:
    print(num,"is not prime")
    break
    else:
    print(num,"is prime")

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

      Enter 4,.. it shows prime

    • @poojakukreti8618
      @poojakukreti8618 5 ปีที่แล้ว +1

      Why did you take pow() function in this?

    • @akhila1909
      @akhila1909 4 ปีที่แล้ว

      Here,We can use sqrt() instead pow()
      He use pow() to find its sqrt value.

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

    the most simplest code sir
    #program to check the no. is prime
    n=int(input("enter the no.: "))
    for i in range(1,n+1):
    if n%1==0 and n%i==0:
    print("the no. is prime")
    break
    else:
    print("the no. is not prime")

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

    but what if num=2? how will range(2,2) generate a dataset? will it be blank?

    • @falakchudasama9746
      @falakchudasama9746 ปีที่แล้ว +13

      Create an Elif condition,
      elif num == 2:
      print(“2 is a prime number”)

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

      ​@@falakchudasama9746
      And for

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

      I had the same dobut but when I runed the cmnd with input as 2 it printed number is prime

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

      Because 2 is not prime number 😂

    • @MonishThegreat
      @MonishThegreat 28 วันที่ผ่านมา

      Stupid 2 is even prime​@@narendrachampaneri8112

  • @vadekarlaxmi5657
    @vadekarlaxmi5657 3 ปีที่แล้ว

    by using while condition
    n=7
    i=1
    c=0
    while i

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

    Continuously uploading videos thats a great sign sir

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

    we can further cut the range of factors by excluding even numbers as factors of odd numbers
    num = int(input('enter any number'))
    factors_range = range(3, int(num / 2)+1, 2) if num % 2 != 0 else range(2, int(num / 2)+1, 1)
    for i in factors_range:
    if num % i == 0:
    print('number', num, 'is not a prime number, divisible by', i)
    break
    else:
    print('it is a prime number')

  • @blessedinspirational
    @blessedinspirational 4 ปีที่แล้ว +5

    You're the best teacher soo far!
    Please how can i found your Java tutorial?

  • @nitishnitish9172
    @nitishnitish9172 4 ปีที่แล้ว

    for i in range(1,101):
    for j in range(2,i):
    if i%j==0:
    break
    else:
    print(i)

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

    Plz make a video on Selenium with python and welenium with python🙏🙏plzz ,at least in between the lockdown we learn something new