Support Forums
isprime function - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32)
+---- Thread: isprime function (/showthread.php?tid=4969)



isprime function - nevets04 - 02-23-2010

I made this function to check whether a number was prime or not.
This was developed using codepad.org to test it during my 15 minute study hall. So it's simple, but it does work as far as I can see.

Code:
#!/usr/bin/env python
def isprime(n):
    if n > 10:
        if n%2 == 0 or n%3 == 0 or n%4 == 0 or n%5 == 0 or n%6 == 0 or n%7 == 0 or n%8 == 0 or n%9 == 0 or n%10 == 0:
            return False
        else:
            return True
    if n <= 10:
        if n == 2 or n == 3 or n == 5 or n == 7:
            return True
        if n == 1 or n == 4 or n == 6 or n ==8 or n == 9 or n == 10:
            return False



RE: isprime function - uber1337 - 02-23-2010

Seems to be effective, you could have shortened it a bit though
Code:
#!/usr/bin/env python
def isprime(n):
    if n > 10:
        for x in range(2, 11):
            if n%x == 0: return False
        else:
            return True
    if n <= 10:
        if n in (2, 3, 5, 7): return True
        if n in (1, 4, 6, 8, 9, 10): return False
I can see your a little rusty Tongue


RE: isprime function - Fallen - 04-05-2010

(02-23-2010, 06:46 PM)uber1337 Wrote:
Code:
if n in (2, 3, 5, 7): return True
        if n in (1, 4, 6, 8, 9, 10): return False
I can see your a little rusty Tongue

Code:
return False if n in [1, 4, 6, 8, 9, 10] else True

You are aswell Tongue


RE: isprime function - uber1337 - 04-06-2010

(04-05-2010, 06:15 PM)Fallen Wrote:
Code:
return False if n in [1, 4, 6, 8, 9, 10] else True

You are aswell Tongue
LOL you really need to stop shortening things lol. Why don't you just use C++ and have a full length program on one line?


RE: isprime function - symetrik - 04-18-2010

Quote:
Code:
#!/usr/bin/env python
def isprime(n):
    if n > 10:
        for x in range(2, 11): if n%x == 0: return False
        else: return True
    if n <= 10: return False if n in [1, 4, 6, 8, 9, 10] else True

Defeated by:
Code:
if isprime(x) and x > 11: isprime(x**x)

For example:
Code:
if isprime(13) and x > 11: # Will return true, as 13 is a prime and is larger than 10.
    isprime(13**13) # As 13 is not in the check list, this number will be found to be a prime, even though it clearly is not.



RE: isprime function - wat - 05-05-2010

Your function assumes that all composite numbers above 10 are divisible by a number smaller than 10; not true.

Here's an article explaining some of the intricacies that go into finding prime numbers Smile.

Keep in mind that the first method mentioned could be optimized more by only checking numbers smaller than sqrt(num) instead of num / 2.