#25 Python Tutorial for Beginners | Prime Number in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Ivan ready and in this video we'll write a code to find a given number is prime or not now first of all just to give you a brief idea of prime so prime number is a number which is divisible only by one and itself so if you take an example of 10 so 10 has a factor which is 1 then we have 2 then we have 5 then we have 10 so we don't want that so 10 is not a prime number so 7 is a prime number that is a prime number the 19 is a prime number so how do you check a given number is prime or not now when you say you want to check out given number is a prime number or not it's very simple right so let's take a number here so let's say 7 in this case so what you will do is you will try to divide this number by 2 because all the numbers are divisible by 1 so let's start with 2 so we check if 7 can be divided by 2 if yes then it's not a prime number if it's not divisible then we'll go for 3 then you go for 4 then we'll go for 5 then we go for 6 so will not check for 7 because I get is divisible so we start from 2 and we'll end with minus 1 so example if we take a number which is 19 so you will end at 18 right that's what you will do so every time you have to check if you find the number if it is divisible you can simply print not a prime number if it is not divisible we can print it's a prime number so how do you write a code so let's take a number so I will say number is equal to let's start with a primer by itself I start with 7 and then let's run a loop so we start a loop we'll say I as a counter variable so we say for I in let's do user the NGO now this range will start from 2 because i'm start from 2 and then we have to end at 7 so we'll end that numb or we have to end at 6 right so of course when you range will not take the last value once we go to the inch here let's check what the condition so what is the condition so condition would be if if num mod I ok with this division equal to equal to 0 that's how you check if the number is divisible or not if it is equal equal to 0 in that case you will simply print not prime that's it right so it will simply print not time because it is divided LC well print its prime oh is it that simple of course not write this code will not work so let's run this button you can see it says primetime climb climb multiple times ok so if we just want to do it once right now if you remember in the last video when we talked about for else so if you want to do it only once what you can do is shove using this else for if we should be using it for for right so we say else for fall and then after this blend you will simply say break because once you know if the number is not prime wife you check for the many numbers example for 10 it is divisible by two right so while you have to check for the numbers so it will simply say break there and I guess this will work so the feeling this code you can see it says Prime so that's what you are doing right so what we're doing is we're checking from two to the second last number which is in this case we are saying num so it will take num minus 1 and then every time you will check if the number is divisible by that number if yes it's not a prime number if no you will complete the entire loop and you will simply print prime that's it but let's change this number so instead of having 7 let's take 10 here and with this let's run this code and you can see it says not prime that's right 10 is not apply number that's how you can check a given number is prime or not ok so this works but don't you think this is not efficient code we have some efficient way as well so this is proper but we are checking from this second number to the last number what if you check only half it went that works in fact so if you go more efficient we can also check for this squared or not that is something you have to find so just go to google and search for how to find a prime number in an efficient way so that's how you improve yourself in the algorithms as well how can you think about efficiency speed efficiency space efficiency all those things so at this point we have only worked with prime number but you can make it efficient so let me know your efficient code in the comment section so that's it from this video where we have talked about how to check a given number is flying or not so I hope you are enjoying the season in the comment section and do subscribe for further videos bye bye
Info
Channel: Telusko
Views: 479,580
Rating: 4.888555 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: SpTAxH_Geow
Channel Id: undefined
Length: 4min 10sec (250 seconds)
Published: Fri Mar 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.