#this function checks if a number is a prime number,
#if not it outputs the lowest factor.

def isprime(n):
    """Determines whether a number is a Prime number,
    Takes a single arguement n, which is the number"""
    if n==1:
        return 'Not a Prime number, only has one distinct factor'
    elif n==2:
        return 'Prime number'
    elif n==3:
        return 'Prime number'
    else:
        for i in range(2,n):
            if n%i==0:
                return 'Not a prime number',i,'is a factor'
            else:
                return 'Prime number'
#example
#print(isprime(590))
#outputs
#>>>
#('Not a prime number', 2, 'is a factor')

#Adegoke Obasa ----adegokeobasa@yahoo.com----

Nice try, but it will be too slow for large prime numbers.
For instance try it with a large prime like 2147483647

You can speed things up a little realizing that all even numbers (except 2) are not primes.

Edited 5 Years Ago by vegaseat: 2147483647

Another speedup: You only need to check for factors up to the square root of the target.

Comments
Speed is key, your comment is sure fast.

Thanks so much, I've really gained a lot since joining daniweb community, I will try to optimize it and make the correction thanks a million.

And please do a reasonable number of tests of your code before posting. This is a truncated version of the function to point out the error:

def isprime(n):
    for ctr in range(2,n):  ## do not use 'i', 'l', 'O' as they can look like numbers
        if n%ctr==0:
            return 'Not a prime number',ctr,'is a factor'
        else:
            return 'Prime number'

print isprime(9)  ## 9 is not prime-->3**2

Edited 5 Years Ago by woooee: n/a

Thank you woooee, I'll correct the codes, and thank you for giving me tips that will help me in other programs I write.

Here is what could be considered 'behind the book correct answer' cheat to is_prime function from my utility module:

def is_prime(n):
    sqrt_n = int(n**0.5 + 0.5)
    if n == 2 or n == 3: return True
    elif n < 2 or n % 2 == 0 or n % 3 == 0: return False

    divisor = 5
    increment = 2
    while divisor <= sqrt_n:
        if n % divisor == 0:
            return False
        divisor += increment
        increment = 6 - increment # toggle: 2 -> 4 -> 2 ...
    return True

This code uses the fact that for primes > 4 the prime%6 must be 1 or 5.

Comments
You are great.

Here is what could be considered 'behind the book correct answer' cheat to is_prime function from my utility module:

def is_prime(n):
    sqrt_n = int(n**0.5 + 0.5)
    if n == 2 or n == 3: return True
    elif n < 2 or n % 2 == 0 or n % 3 == 0: return False

    divisor = 5
    increment = 2
    while divisor <= sqrt_n:
        if n % divisor == 0:
            return False
        divisor += increment
        increment = 6 - increment # toggle: 2 -> 4 -> 2 ...
    return True

Wow this is really cool, well that's why i'm on daniweb to learn, and i assure you i've been learning a lot, Thanks a million. Will try to sit down understand then implement.
This code uses the fact that for primes > 4 the prime%6 must be 1 or 5.

Thanks a million, i have learnt something new and will try to understand then implement it.

It is a courtesy to others readers to mark the thread as "Solved" when it is finished so volunteers don't waste their time reading threads that are finished. Sometimes the posts don't end there but it is something to keep in mind.

This question has already been answered. Start a new discussion instead.