Search code examples
sage

Sage: Sum of divisors of an integer


I would like to use sage to compute the following sum:

sum of the divisors of (D-b^2)/4 where b is an integer satisfying; absolute value of b is less than D and b is equivalent to D modulo 2.

This is what I tried:

def bqfs(d):
answer=[]
for b in range (sqrt(d)):
    c=(d-b^2)/4
if c.is_integral():
        answer.extend(sum(sigma(c))
return answer

I get invalid syntax error message.


Solution

  • Currently, you do indeed have invalid syntax. Python (which Sage is built upon) indentation as syntax.

    This works:

    def bqfs(d):
        answer = []
        for b in range(sqrt(d)):
            c = (d-b^2)/4
            if c.is_integral():
                answer.extend(sum(sigma(c)))
        return answer
    

    but it doesn't make sense and doesn't work either, because sigma(c) is already the sum of divisors of c, and you can't sum an integer all by itself; you need a list.

    What if you just did answer.extend(sigma(c)), which would give you a list of the sums in question? (I don't see you trying to check the modulo condition yet, but you are right that you want to build up code incrementally, so that is no problem.)