Ask Your Question

Revision history [back]

First of all, Sage has a great feature for caching output of functions

def fibo(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fibo(n-1) + fibo(n-2)

Then you can call fibo with quite high values and result is automatically cached for you!

sage: fibo(20)

If you use it you will get a simpler code.

Next, be careful that dividing Python ints is different than dividing Sage integers:

sage: for i in range(10): print i / int(3),
0 0 0 1 1 1 2 2 2 3
sage: for i in range(10): print i / 3
0 1/3 2/3 1 4/3 5/3 2 7/3 8/3 3

In particular, in your loops you are dividing Python integers with Python integers (range generates Python integers). Hence you are doing integer divisions. You can either convert explicitely the input at the begining of each function

def H11(r1):
    r1 = ZZ(r1)

or use srange instead of range

sage: map(type, range(4))
[<type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>]
sage: map(type, srange(4))
[<type 'sage.rings.integer.Integer'>,
 <type 'sage.rings.integer.Integer'>,
 <type 'sage.rings.integer.Integer'>,
 <type 'sage.rings.integer.Integer'>]

I hope it is clear enough.