Horje
factors of a number with memoization Code Example
factors of a number with memoization
import math

def memoize(f):
    memo = {}
    def helper(x):
        if x not in memo:
            memo[x] = f(x)
        return memo[x]
    return helper

@memoize
def isprime (n):
    if n == 1:
        return False
    elif n == 2:
        return True
    else:
        for x in range (2, int(math.sqrt(n))+1):
            if n % x == 0:
                return False
                break
        else:
            return True


def factors (a):
    if a == 1:
        return []
    elif isprime(a):
        return [a]
    else:
        for x in range (2, int(math.sqrt(a))+1):
            if a % x == 0:
                return [x] + factors(a/x)

testnumber = int(input("Enter a number."))
print factors(testnumber)




Python

Related
styling  filter form django Code Example styling filter form django Code Example
clase describe algo Code Example clase describe algo Code Example
filter query objects by date range in Django Code Example filter query objects by date range in Django Code Example
python x,y,z is d (20, 30, False) Code Example python x,y,z is d (20, 30, False) Code Example
python dictionary remove nonetype Code Example python dictionary remove nonetype Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7