#25 Python Tutorial for Beginners | Prime Number in Python
Vložit
- čas přidán 30. 06. 2024
- Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
Instagram : / navinreddyofficial
Linkedin : / navinreddy20
TELUSKO Android App : bit.ly/TeluskoApp
Discord : / discord
Github :- github.com/navinreddy20/Python- - Věda a technologie
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🇮🇳
Finally, a clear, concise, easily understood explanation for finding prime. Thanx.
Your beginner video's are best in CZcams 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
one of the Best star teacher on CZcams hats off to you sir
We wants such type of videos from you dear navin sir
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.
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😀
Great great to see you back on Python series. Lovely. Thanks Navin.
Thank you. A very useful video. Could you please add this video in python video list so We can reach them easily.
Your tutorials makes coding life much more simple :)
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.
your python series are great sir ,thanks for this
Thank you
Continuously uploading videos thats a great sign sir
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')
x%i ?? can you explain me this part ?
@@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)
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
@@niyadata 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)
@@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?
Your python series and other technical videos are excellent sir thank you sir it's help me a lot
great work navin !!! really enjoying learning Python
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...?
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")
Super
But it better x//2insted of x/2
You also right. That's why I caste here by int.
Please explain me this part int(x/2+1)
@@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.
there's a neat trick using mathematics. for a number n to be prime, it shouldn't be divisible by all the prime numbers till floor(sqrt(n)).
love this! thanks for the info!
Loved it Reddy Sahab...thankxxxx a ton
Plz make a video on Selenium with python and welenium with python🙏🙏plzz ,at least in between the lockdown we learn something new
Found a couple of more efficient methods and then came across sieve of eratosthenes , would love to see a video explainer by you
Thanks Navin, very cool!
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")
You dont have the need to add 1.
Wouldn't need the second "int" call in the range function. But you probably already know that now :D
@@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
@@MikoKikoJo what do you mean, it will throw an error
@@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.
Sir, take love from Bangladesh
I tried a lot many times to learn code for prime. But didn't worked .
U r really great 💯🔥
Best Tutorials series ever I have seen...........
LOOKING SMART SIR
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!!!
thanks Navin....Brilliant teaching method....Kudos
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.
No..143...has divisor 13.
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')
Why you wrote else conditions two times?
@@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
Could you say why you use break, I'm not under stand by from video
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?
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
Same output can be achived by if-else, just using one more "break" in the else part.
Really simple and interesting. Thanks
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))
Hey , do you know how to do this ques?
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}.
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")
Bro the tutorial itself is wrong, your code works perfectly thank you!
Yeah he made a mistake for i in range (1,num+1) should come and the +1 is excluded and it prints 1:num)
This guy is amazing teacher! Bravvo
We can put IF condition with squr() function to get that division number.If squr() of that number come with greater than zero that means it is division of some number and we dont need to go in the roop condition. we can come out by else.
Really thank you sir, i like this algorithm ❤
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.
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")
i m learning now brother.
@@raghavendraam719i did not understand at all😢
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")
Really u r great sir, excellent explaining in simple code
num = int(input())
for i in range(2,num):
if num%i==0:
print("not prime")
break
else:
print("prime")
count=0
x=int(input("Enter a number:"))
for i in range(1,x+1):
if(x%i==0):
count+=1;
if(count==2):
print("Prime")
else:
print("Not prime")
Good answer
👏👏👏👏Bravo.
I had to debug the whole code in order to understand.
Won't call it efficient but that was creative as hell. 👌
thank you very much , this video helped me a lot!
😊Enjoyed...Everytimes...!
You're the best teacher soo far!
Please how can i found your Java tutorial?
go to playlist.
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")
Hritwik Haldar it is same bro .... not efficient
bto agr input 1 ho toh kaise kroge ??
First education purpose video without any advertisement 🤩
Thankyou so much you art brilliant . Please can you start teaching java pleasseee
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")
Sorry,but it is wrong
Yes Its Wrong
isprime=int(input())
for i in range(2,int(isprime/2)):
if isprime%i==0:
print("not prime")
break
else:
print("Prime")
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
Thanks sir......for python series...
😊😃😃
this is the best beginner videos on CZcams
sir i am getting this error, help me please.
connection to python debugger failed interappted funtion call accepted failed .
i=2
while(i
What is x
Video was really helpful. Thanks a lot
Boss Ur amazing person ...
♐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
you can make to square root it is more efficient than halfing number
use seive algo
@@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.
welcome back aliens 🤣🤣🤣🤣
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")
I like the input part 🤣🤣
input is 49 output is prime 💀
but I thought 49 is non prime bro
Thank you this helped me a lot with my mini project!
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")
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
Actually iterating upto floor(n**0.5) is even more efficient
@@khamza8926 hi khamza, can you explain your point of view
@@anwarsheik3329search for sieve of eratosthenes proof
@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????
you can add special condition for 2 because its a start from 2
bro when you debug this, range function doesn't allow it to read ' if ' statement it straight away jump to 'else ' statement.
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
Under what condition does the break outside the for loop work? Does it work if there is no if condition in the for loop? Or does it work only if there is if and all the conditions in the if are not satisfied. What happens if there are 2 if's in the for loop?
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
Hi can you tell me if the -------> 'else' at the bottom is with 'while true' indentation or with 'while b
@@sumeetrox2479 oustide whileb
print a prime number, greater than or equal to a given number
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")
For input 4 it is showing as 4 is prime number but 4 is not prime (factors are 1,2,4)
U should add 1 in range
range(2,(num//2)+1):
Then 4 will be a "not prime"....
@Prem Kumar It works....
For n=2 it directly enters the else part and it becomes a prime number
@Prem Kumar bro here we have to use "for-else" not "if-else"
Then n=2 enters the else part
@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"
a=int(input("enter a number"))
count=0
i=1
while i
a=int(input("enter a number"))
count=0
i=2
while i
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")
'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
It's seems,, there is syntax error, seems after running this code ,it will not be executed
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?
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
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")
Instead of floor use ceil and don't add it
Correct
@@BeUniqueMotivation yeah thank you
The way of explanation is good sir
Thank you so much sir
sir this code won't work for num=2. Since although 2 is divisible by 2 it is a prime number
it works
big brain
@@onkarparandwal2386 how it works?
2 is the even prime number.
2 is a prime number
Love from Bangladesh 🇧🇩🇧🇩😊
Fouzeya Fardous 👍👍👍
x=int(input())
print("2")
count=1
for num in range(3,x):
for i in range(2,x):
if num%i==0:
break
else:
print(num)
count+=1
print("No. of prime numbers less than",x,"= ",count)
Efficiency we can check from 2 to num/2, because if any number is not divisible till it's half number then it's prime.
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)
2 print nhi hoga
what if we want to just print the NON prime numbers?
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')
The last line is an error print() p is small😂
@@anirudh4671 Huh acting as friendly Console showing error😹
Hi Sir, Please make a tutorial videos on automation testing tools Selenium with Python and Java
thank you i had a working programm and it really did its job good but everytime i typed in 99 it said it is a prime number but with the break it works now
num=int(input("Enter a number:"))
for i in range(2,num):
if num%i==0:
print("Not Prime")
break
else:
print("Prime")
I did same lol
@@prajithraj9547 no it will print not prime only check once
All prime numbers are greater one
After Enter the value
Use if num>1
Bhai sir ne copy marne ko nai bola tha.
Dusri method use karo aur sikho
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')
This really helped, I dont get why my python Professor has to overcomplicate these simple maths question by using tens of different functions and identifiers, Thank you again
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")
What about 1 I think it will show prime
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')
I think it takes more complexity
Thank u sir excellent lectures
You are the best 👌👌👌👌👌👌
but what if num=2? how will range(2,2) generate a dataset? will it be blank?
Create an Elif condition,
elif num == 2:
print(“2 is a prime number”)
@@falakchudasama9746
And for
I had the same dobut but when I runed the cmnd with input as 2 it printed number is prime
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)
thnx
Thank you sir
Thanks for helping sir.....
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")
Bro how about 121 Or 169?
this is a hardcode method
it prints prime 2 times
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")
Explain that exponent please
@@venugopal-kh5zc in python "**" is used for exponential.
Eg. 5^2 is written as 5**2
@@dailyshorts1164 that is I now sir ...I want why using exponent. What is logic here
Explain once
@@venugopal-kh5zc ohh sorry.
If a given number n is not divisible by any number from 2 to root(n) then it's prime.
first time on your video. Nice explanation brother👌👌
Plz make the videos on testing with python🙏🙏🙏🙏
how to check for a series or range of numbers? Like, if we don't want to give a specific number as input and we want to check all the numbers from 2-100 if prime or not. So what changes would be there in the code, could someone pls help?
It's simple:
c = range(2,101)
for i in c:
for e in range(2,i):
if i%e==0:
print(i,'NOT A PRIME No.')
break
else:print(i,'IT\'S A PRIME No.')
Try this you will get all prime nos from 2-100
@@jainammadrecha2758 thanks