Request Help Debugging Program

Samir spython01 at gmail.com
Thu Jul 24 00:30:54 CEST 2008


Hi Everyone,

In order to get a better command of Python, I have been trying to
solve the puzzles on the Project Euler web site.  I've made my way
down to problem 21:
http://projecteuler.net/index.php?section=problems&id=21

I've created a function (findSumOfDivisor), that when passed an
integer, will return the sum of the argument's natural divisors.  When
tested in isolation, it seems to do what I intended.

When I incorporate it into the larger program, however, I start
getting run-time errors that for the life of me I cannot debug.  My
brute-force approach is to cycle through all of the integers to find
the sum of their divisors and then enter the number (as key) and the
sum of the divisors (as values) to a dictionary called "amicable".  I
only call findSumOfDivisor if the key:value combination does not
already exist in the dictionary (hoping to save some time when I loop
through 10,000 integers).

Below is the code I have so far.  I'm not completely certain if my
logic will lead me to the correct solution, but that is a different
matter.  For now, the program seems to get through the first integer,
2, successfully, but errors out in line 20 when it tries to call the
function.  Apparently, Python doesn't like line 12 in the function
where it returns the sum of the divisors.  The code, which is best
viewed using a fixed-font, is:

from math import sqrt

def findSumOfDivisor(n):
    divisor = [1]                      # start list of divisors at 1
    for x in range(2, int(sqrt(n))+1): # search for possible divisors
between 2 and sqrt(n)
	if n%x == 0:                   # if x is a divisor of n
	    if ((n/x) == x):           # and if x is the sqrt of n
		divisor.append(x)      # add x to the list of divisors
	    else:	               # otherwise
		divisor.append(x)      # add x to the list of divisors
		divisor.append(n/x)    # and add n/x to the list as well
    return sum(divisor)                # return the sum of the
divisors

answer   = []                          # initialize answer to empty
amicable = {1:0}                       # initialize amicable
dictionary for 1 (sum is 0)

for i in range(2,10):                  # loop through integers 2
through 9
    if not amicable.has_key(i):        # if i not found in amicable
keys
	sum = findSumOfDivisor(i)      # then find the sum of its divisors
	amicable[i] = sum              # and add both to amicable dictionary
    else:                              # otherwise
	sum = amicable[i]              # just get its sum
    if not amicable.has_key(sum):
	sum2 = findSumOfDivisor(sum)   # if sum of i's divisors not in
amicable keys
	amicable[sum] = sum2           # then add both to amicable dictionary
    else:                              # otherwise
	sum2 = amicable[sum]           # just get its sum
    if i == sum2:                      # if i is amicable with sum2
	answer.append(i)               # append i to list of answers
	answer.append(sum2)            # and append sum2 to list of answers

print "amicable =", amicable
print "answer =", answer

The error I receive is:

Traceback (most recent call last):
  File "pe_prob021.py", line 19, in <module>
    sum = findSumOfDivisor(i)      # then find the sum of its divisors
  File "pe_prob021.py", line 12, in findSumOfDivisor
    return sum(divisor)                # return the sum of the
divisors
TypeError: 'int' object is not callable

Even after cluttering the code with print statements every other line,
I am completely lost.  Does anyone know why I get this error?

Thanks in advance.

Samir



More information about the Python-list mailing list