+ 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
17 Respostas
+ 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')
+ 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.
+ 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
+ 3
Sure,
if x==2:
return True
Insert these lines somewhere before where it tests if x % 2 == 0.
+ 2
But this answer is not accurate
It tells 12 is a prime number
Correct me if I am wrong
+ 2
Thank you sreeju
+ 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 🙂
+ 2
Thanks
+ 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.😄
+ 1
Brian Can you implement it in a code
+ 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')
+ 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')
+ 1
@Lothar
The code correctly checks for prime numbers using the trial division method.
I think you didn't execute the code. ;)
+ 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.
;)
0
Hello
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.