math - Looking for values in Python Dictionaries -


i'm trying memoize divisor sums of numbers.

divisorsums = {}  def sumdivisors(num):      global divisorsums      total = 0      if num == 1:         return 0      in xrange(num/2, 0, -1):         if in divisorsums:             return divisorsums[i]         else:             if not num % i:                 total +=      divisorsums[num] = total      return total 

however, returns 1 numbers, when loop through numbers. correct when used singularly, problem lookup system. i'm pretty sure don't understand how value in dictionary. can me out?

the usual shortcut trick memoize use mutuable default argument

def sumdivisors(num, divisorsums={}):      if num in divisorsums:          # check if have         return divisorsums[num]     # memoized answer here      total = 0      if num == 1:         return 0      in xrange(num/2, 0, -1):         if not num % i:             total +=      divisorsums[num] = total      return total 

other code seems work ok. how runnning 1?


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -