简体   繁体   中英

Issue with an “is_prime” python function on Sublime Text

I am trying to run the following simple code:

import math
def is_prime(N):
    for i in range(2, int(math.sqrt(N))):
        if (N % i == 0) :
            print(str(N) + " is not prime")
            return False 
    print(str(N) + " is prime")
    return True

The goal is to determine whether or not a number is prime (pretty simple). However, I can't get this code to work. I have managed to pinpoint the issue around the int(math.sqrt(N)) instructions, but I don't understand why it won't work, as running int(math.sqrt(134)) for example works fine for me. Is there anything wrong I can't see in this code?

I have also come to question the compiler's viability. I am using Sublime Text 3 with the default Python Builder. Maybe there is something I am doing that is specifically wrong on Sublime Text?

EDIT: Forgot to mention the issue. When I run is_prime(8) for instance, it returns 8 is prime . But it doesn't display any error at all. However, it should be noted that running is_prime(134) prints 134 is not prime as expected. I could try a for loop if anyone wants.

Try this:

import math
def is_prime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i == 0 :
            return False 
    return True

range() stops before it actually gets to the second number. In this case, you want to test that number as well, so you want to round down, and then add one.


More efficient version ( x10 - x1000 speed improvement):

def memoise(func):
    memory = {}
    def wrapper(n):
        if n in memory:
            return memory[n]
        ret = func(n)
        memory[n] = ret
        return ret
    return wrapper

@memoise
def is_prime(n):
    if n <= 3:
        return n > 1
    if not n%2 or not n%3:
        return False

    for i in range(5, int(n**0.5)+1, 6):
        if not n%i or not n%(i+2):
            return False
    return True

Playing in the interpreter:

>>> math.sqrt(8)
2.8284271247461903
>>> int(math.sqrt(8)) + 1
3
>>> for i in range(2, 2):
        print(i)

>>> for i in range(2, 3):
        print(i)

2

I rewrote some of the lines in your program making it a little bit more efficient. Your for loop iterates over all numbers up to the root of N. If you start by checking if the number % 2 == 0, you can then exclude all even numbers and only check the odd numbers 3, 5, 7, ....

import math
def is_prime(N):
    if(N == 2):
        print(str(N) + " is a prime")
        return True
    if((N % 2 == 0)or(N == 1)):
        print(str(N) + " is not prime")
        return False
    for i in range(3,int(math.sqrt(N))+1,2):
        if(N % i == 0):
            print(str(N) + " is not prime")
            return False
    print(str(N) + " is prime")
    return True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM