Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
Comment

PREVIOUS NEXT
Code Example
Python :: how can I get response from amazon with beautiful soap if I get 503? 
Python :: tyjacsav 
Python :: ordereddict deleting wrong item 
Python :: python create local list 
Python :: first and last upper 
Python :: online convert http query to json python 
Python :: djb2 hash function c explained 
Python :: he escape() function is used to convert the <, &, and characters to the corresponding entity references: 
Python :: image.show pillow mac os 
Python :: spacy print word in vocab 
Python :: streamlit altair 
Python :: gravar arquivo python 
Python :: print convert hex as string ascii 
Python :: region error when use service account json file dataproc 
Python :: spevify datatype of column 
Python :: python map function using lambda function as one of the parameters 
Python :: Requests-html absolute url 
Python :: python get portion of dictionary 
Python :: bulk m4a to wav ffmepeg 
Python :: python format method align center 
Python :: django list view 
Python :: geomertry 
Python :: python -c crypt command in python3.3 and above 
Python :: countvectorizer minimum frequency 
Python :: how to use rbind() to combine dataframes 
Python :: pandas : stratification (mean) 
Python :: python quick sort 
Python :: General Loop Structure 
Python :: how to check for non-datetime value in python 
Python :: superpixel 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =