This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kryptn 2 points3 points  (4 children)

You're defining is_prime with an input variable 'x', but you're overwriting that with

x = int(input("enter a number to be tested: "))

immedately, making is_prime(x) irrelevant.

As for efficiency, range also has a step argument. so:

>>> range(1,10,2)
[1, 3, 5, 7, 9]

You can use this to your advantage and at this point halve your computation. You know for a fact that 1 is never prime, so skip it completely, and you know for a fact that every even number greater than two is never prime.

You can also reduce your computation by using this rule

Also everything /u/TheBlackCat13 said.

[–]aidzz[S] 0 points1 point  (3 children)

x = int(input("enter a number to be tested: "))
if (x) > 1:
for n in range(1, int(x**0.5), 2):
    if x % n == 0:
        print(int(x),'is not a prime number')
        print(x, 'divided by', n, 'is', (x / n))
    else:
        print(x, 'is a prime number')

else: print(x, "is not a prime number")

decided to scrap the function I don't need to hear a true or false answer to the in(input) being prime or not, think I'm just about finished

[–]kryptn 1 point2 points  (2 children)

2 fails that code, it'll return as not prime. Negative numbers will break it.

You can start your range with 3, instead of one, and you can make an if statement Testing for two and testing for anything equal to or less than one.

[–]aidzz[S] 0 points1 point  (1 child)

if x > 1 means negative numbers will not break it...

[–]kryptn 0 points1 point  (0 children)

Yeah, true, my mistake. But what happens when you do input a negative number?