- #1
llstelle
- 20
- 0
Homework Statement
I'm supposed to write a program which asks for an integer input, then determines if the input is a prime or not.
I wrote a program but I have 2 issues:
1) It works well for primes up to around the size of 30000~, then above that (I just tried 65537, for a weak upper bound) it just freezes... I'm guessing Python has some memory allocation issue here? Is there a solution?
2) I don't like my code. It looks ugly. Having so many nested conditionals is not elegant. And so on. Are there any tips to improve what I have written? Thanks!
2. The attempt at a solution
Here's my code...
Code:
#This program verifies if an integer is prime.
import time
x = int(raw_input('Please enter an integer.\n'))
if x == 2:
print ('2 is a prime')
elif x < 2:
print (str(x) + ' is not a prime.')
elif x > 3:
i = 2
while True:
j = x%i
if j == 0:
print(str(x) + ' is not a prime.')
time.sleep(5)
break
elif j != 0:
if i > x/2:
print(str(x) + ' is a prime.')
time.sleep(5)
break
elif i < x/2:
i = i+1
Last edited: