+ 3
Python code to find given number is prime or not?
Please provide a simple and explanatory code. It's a humble request.
24 Réponses
+ 6
S2XPHOENIX🇮🇳
Providing `readymade` code, this feature is not available here.
its humble request First Try by yourself,after this if you face any problems in during learning or making code then ask here.
+ 6
Yosef Sahle thanks, but by simply providing an answer S2XPHOENIX🇮🇳 missed an oppertunity to collaborate with a mentor to learn new skills and understanding of the fundamentals needed to complete the code task on their own.
+ 3
First, it's important to know what a prime number means. This requires a basic understanding of math.
Secondly, which formula can you use to figure out if a number is prime?
Lastly, how do you put that formula into your code to make it work?
The last part requires an understanding of the basic fundamentals.
+ 3
S2XPHOENIX🇮🇳
Have you tried the search feature in the app? You will find that others have a similar question to yours. There are code samples available too.
I would like to help you, but I feel that providing a code will be redundant, as you have various resources at your disposal.
May I suggest you share your code attempt and comment on each part, explaining your thinking? This will help me understand your current knowledge and thought process. From there, I can suggest an alternative approach to complete the code based on your input.
+ 2
S2XPHOENIX🇮🇳
#update the app
#Go on learn section & select course and read again with carefully...
1:Python Intermediate
2:Python introduction
+ 2
Here You Can Have the Code
def check_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
number = int(input("Enter a number: "))
if check_prime(number):
print(number, "is a prime number.")
else:
print(number, "is not a prime number.")
+ 1
Here is a good explanation and walk thru of code:
https://www.geeksforgeeks.org/prime-numbers/
+ 1
Chris Coder ik the number but I'm confused which all logics to implement here
+ 1
Darpan kesharwani🇮🇳 link isn't working 🥲
+ 1
Darpan kesharwani🇮🇳 but I've already completed the course
+ 1
Sure Chris Coder
+ 1
It first checks if num is less than or equal to 1, in which case it returns False since prime numbers are greater than 1
Next, it iterates through a range from 2 to the square root of num (inclusive) using a for loop. Within the loop, it checks if num is divisible by the current iteration value i using the modulo operator (%). If num is divisible by any value of i, it means that num is not prime and the function returns False.
If the loop completes without finding any factors of num, it means that num is prime and the function returns True.
+ 1
If You want the simple one You Can Also Use this code but it will take 2x time since it iterates (num) times
def check_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num % i == 0:
return False
return True
number = int(input("Enter a number: "))
if check_prime(number):
print(number, "is prime.")
else:
print(number, "is not prime.")
+ 1
Thnx vro Yosef Sahle
+ 1
Chris Coder sorry, i though i was helping and you are right that was a mistake
+ 1
#taking value from user
try:
num = int(input("Enter the number: "))
except ValueError:
print("Please enter the Integer value!")
exit()
isPrime = True
if num<2:
isPrime = False
else:
for i in range(2, num):
if num%i == 0:
isPrime = False
break
if isPrime:
print(num, "Number is Prime number.")
else:
print(num, "Number is not a Prime number.")
0
Darpan kesharwani🇮🇳 But I'm confused which logic to implement here 🥲
0
Yosef Sahle could u pls explain the code.
0
am having troubles finding the next course to take after python intermediate, coz this "python developer course" that i thought was supposed to be next turns out it a combination of introduction & intermediate python courses, so i would appreciate any help/guidance from anyone🙏thnx!