0

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.

sophie668
  • 3
  • 2

1 Answers1

1

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

kcrisman
  • 4,374
  • 20
  • 41
  • Thanks. I created a list and append the c values which seems to work but couldnt figure out how to incorporate the modulo condition.. – sophie668 Aug 06 '14 at 14:29
  • In Sage (not just Python) this should be easy - for instance, you could do `mod(d,2)==mod(b,2)` or even `(d % 2) == (b % 2)` probably works, all inside yet another `if` statement. Unless I'm misunderstanding what you want to do. – kcrisman Aug 06 '14 at 16:35