So, your code is:
sage: def BS(N):
sage: a0 = 1
sage: b0 = 1/sqrt(2)
sage: s0 = 1/2
sage: B = 2
sage: a1 = a0
sage: b1 = b0
sage: s = s0
sage: for k in range(N+1):
sage: a = (a1+b1)/2
sage: b2 = a1 * b1
sage: b = sqrt(b2)
sage: c = a^2-b2
sage: s -= B * c
sage: p = 2* a^2/s
sage: a1 = a
sage: b1 = b
sage: s1 = s
sage: B = 2*B
sage: return p.n(digits=2^N)
When you write b0 = 1/sqrt(2)
, you define an element of the Symbolic Ring
:
sage: b0 = 1/sqrt(2)
sage: b0.parent()
Symbolic Ring
Which means that all compurations are done in a symboloc way, there is no
numerical approximation at all there. So, with square roots, products and
sums, Sage will deal with bigger and bigger formulas, which explains your
timings.
For example, if i replace return p.n(digits=2^N)
by return p
in your code, i got:
sage: BS(5)
-1/32*(4*sqrt(1/2)*2^(1/4) + 4*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4)) + 4*sqrt((4*sqrt(1/2)*2^(1/4) + sqrt(2) + 2)*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4))) + 4*sqrt((4*sqrt(1/2)*2^(1/4) + 4*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4)) + sqrt(2) + 2)*sqrt((4*sqrt(1/2)*2^(1/4) + sqrt(2) + 2)*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4)))) + 4*sqrt((4*sqrt(1/2)*2^(1/4) + 4*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4)) + 4*sqrt((4*sqrt(1/2)*2^(1/4) + sqrt(2) + 2)*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4))) + sqrt(2) + 2)*sqrt((4*sqrt(1/2)*2^(1/4) + 4*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4)) + sqrt(2) + 2)*sqrt((4*sqrt(1/2)*2^(1/4) + sqrt(2) + 2)*sqrt((sqrt(2) + 2)*sqrt(1/2)*2^(1/4))))) + s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