import mathx = intinputenter the numbery = intmathsqrtxi = 2for i in range2y if xi != 0 printthe number is not a prime i += 9999999999999999999999 else i += 1if i 999999999
There are a few issues with the code:
-
The loop condition
for i in range(2,y)should befor i in range(2, y+1)to include the square root value in the iteration. -
The check
if x%i != 0should beif x % i == 0. If the remainder is 0, it means the number is divisible byiand hence not prime. -
The line
i += 9999999999999999999999is unnecessary and incorrect. It doesn't serve any purpose in determining whether the number is prime or not. -
The final condition
if i < 9999999999999999999999is not needed. Instead, you can use anelsestatement to print that the number is prime.
Here's the modified code:
import math
x = int(input('enter the number'))
y = int(math.sqrt(x))
is_prime = True
for i in range(2, y+1):
if x % i == 0:
is_prime = False
break
if is_prime:
print('The number is prime')
else:
print('The number is not prime')
This code checks if the number is divisible by any number from 2 to the square root of the given number. If it is divisible by any of these numbers, it is not prime. Otherwise, it is prime
原文地址: http://www.cveoy.top/t/topic/iNqX 著作权归作者所有。请勿转载和采集!