Request Help Debugging Program

S

Samir

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 = sum # and add both to amicable dictionary
else: # otherwise
sum = amicable # 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
 
N

Niklas Norrthon

from math import sqrt

def findSumOfDivisor(n): [...]
    return sum(divisor)                # return the sum of the
divisors
for i in range(2,10):                  # loop through integers 2
through 9 [...]
        sum = findSumOfDivisor(i)      # then find the sum of its divisors [...]
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

'int' object is not callable... hmmm...

Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
42()
TypeError: 'int' object is not callable

Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
sum(divisor)
TypeError: 'int' object is not callable

That must be it.
No debugging, just reading the error message and the code...
 
M

mzdude

S

Samir

Hi Everyone,
def findSumOfDivisor(n):
    return sum(divisor)                # fine using function sum()
        sum = findSumOfDivisor(i)      # then find the sum of its divisors

oops redefine what sum is.


x = [1,2]
sum(x) 3
sum = 4
sum(x)

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    sum(x)
TypeError: 'int' object is not callable



- Hide quoted text -

- Show quoted text -

I am an IDIOT! Thanks, mzdude! That did the trick.
Sorry for posting something this stupid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top