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 8:46

>>5
Only that's the slowest implementation you can think of. (that, and your Lisp DSL does not exist)

>>1
There's some redundancy in your code: try range(1, x+1) instead of just skipping 0 with a continue statement and use return len(y) == 2 instead of an if-statement.

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