Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Prime Number implementation

Name: Anonymous 2011-03-20 8:29


def factors(x):
    '''returns the factors for an integer'''
    facts=[]
    for i in range(x+1):
        if i==0:
            continue
        elif x%i==0:
            facts.append(i)
    return facts

def isprime(x):
    y=factors(x)
    if len(y)==2:
        return True
    else:
        return False

Name: Anonymous 2011-03-20 12:55

For extra speed you could find all prime numbers under reasonable bound and hardcoded them in an array in your code. With this, you could check, if searched number is in array by using binary search. If yes, it's prime, if not, it isn't. That would be really fast. For number over bound you could use your algorithm.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List