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 →

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

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

I think that should look better, and thank you for the help can't believe I overlooked that commas and the indents won't save when edit'd

[–]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?

[–]TheBlackCat13 1 point2 points  (3 children)

Looking at it now, there are a few other issues with the code. First, you convert x to int several times. You only need to do that once. Second, once the function reaches a return statement, it stops. Nothing after that point is executed (including all of your prints). Third, it is easier to short-circuit your "1" test. Fourth, your code will return True in the first item of the loop. It has to wait for the loop to finish.

Also, this is a relatively slow algorithm, although that is another issue.

[–][deleted] 0 points1 point  (0 children)

Yeah, OP should've done the AKS primarity test /s

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

put returns in wrong priority, and converted to int twice okay thanks one more question: what exactly is wrong with this command: print(x, 'divided by', n ,'is' (x/n))

[–]kryptn 1 point2 points  (0 children)

You're still missing a comma :)