+ 5

How can I tell a prime number is a prime number and add it in this code

Example This number is odd number This number is a prime number This the output for the input 7 https://code.sololearn.com/cpVPLWb686K5/?ref=app

27th Aug 2023, 3:51 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
17 Answers
+ 7
def is_prime(x): if x < 2: return False for i in range(2, int(x**0.5) + 1): if x% i == 0: return False return True x = int(input()) if is_prime(x): print('This is a prime number') else: print('This is not a prime number')
27th Aug 2023, 4:39 AM
Sreeju
Sreeju - avatar
+ 6
Sreeju , your first suggested code to check for prime number is totally incorrect. > it checks if a number is even or odd. checking for prime number needs an other algorithm. > find for details here: https://en.m.wikipedia.org/wiki/Prime_number it would be great if you could rework your code or remove your post since it is confusing for beginners.
27th Aug 2023, 9:52 AM
Lothar
Lothar - avatar
+ 5
Arrchith and Sreeju the code needs one more check for x==2 so that it returns True in that case.
27th Aug 2023, 5:04 AM
Brian
Brian - avatar
+ 5
def is_prime(number): if number <= 1: return False if number <= 3: return True if number % 2 == 0 or number % 3 == 0: return False i = 5 while i * i <= number: if number % i == 0 or number % (i + 2) == 0: return False i += 6 return True
27th Aug 2023, 11:43 AM
S Jae
S Jae - avatar
+ 3
Sure, if x==2: return True Insert these lines somewhere before where it tests if x % 2 == 0.
27th Aug 2023, 6:02 AM
Brian
Brian - avatar
+ 2
But this answer is not accurate It tells 12 is a prime number Correct me if I am wrong
27th Aug 2023, 4:17 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
+ 2
Thank you sreeju
27th Aug 2023, 4:45 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
+ 2
Sreeju Nice code from you. Can you explain what this line of code is doing exactly: for i in range(3, int(x ** 0.5) + 1, 2): Thanks a lot 🙂
27th Aug 2023, 10:40 AM
sandra
sandra - avatar
+ 2
Thanks
27th Aug 2023, 3:54 PM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
+ 2
You don't need to define functions, you can do it simply like this : n = int(input()) x = int(n/2)+1 for i in range (2,x): rem = n%i if rem == 0: print("not a prime") break else: print("It is a prime no.") The indentations are ok. Just another interesting method to use if else.😄
28th Aug 2023, 6:44 PM
Shinjay Saha
Shinjay Saha - avatar
+ 1
Brian Can you implement it in a code
27th Aug 2023, 5:14 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
+ 1
Hoh def is_prime(x): if x <= 1: return False if x % 2 == 0: return False for i in range(3, int(x ** 0.5) + 1, 2): if x % i == 0: return False return True x = int(input()) if is_prime(x): print('This is a prime number') else: print('This is not a prime number')
28th Aug 2023, 8:42 PM
паркур торт
паркур торт - avatar
+ 1
Arrchith def is_prime(x): if x <= 1: return False if x % 2 == 0: return False for i in range(3, int(x ** 0.5) + 1, 2): if x % i == 0: return False return True x = int(input()) if is_prime(x): print('This is a prime number') else: print('This is not a prime number')
28th Aug 2023, 8:43 PM
паркур торт
паркур торт - avatar
+ 1
@Lothar The code correctly checks for prime numbers using the trial division method. I think you didn't execute the code. ;)
22nd Jan 2024, 4:15 PM
Sreeju
Sreeju - avatar
+ 1
@sandra The line `for i in range(3, int(x ** 0.5) + 1, 2)` initiates a loop to check odd potential factors of `x` up to its square root. It skips even numbers and factors larger than the square root for efficiency. This is part of a prime-checking algorithm. ;)
22nd Jan 2024, 4:21 PM
Sreeju
Sreeju - avatar
0
Hello
28th Aug 2023, 12:31 PM
ABDIHAKIM HUSSEIN ISSE ABDIHAKIM HUSSEIN ISSE
ABDIHAKIM HUSSEIN ISSE ABDIHAKIM HUSSEIN ISSE - avatar
0
Oh it's simple you run a loop from 2 to just before that no. (You can do also do till half of it ) and check whether any of the numbers divides it . If any of the numbers divides it then it's not a prime no. Here's the code for example: x = int(input()) for i in range(2,x): if x%i==0: print("Not a prime no.") break else: print("prime no.") Note in this case I have used else outside the loop.
22nd Jan 2024, 4:58 PM
Shinjay Saha
Shinjay Saha - avatar