# Revision history [back]

Let me try to mimic you approach in a Pythonic way:

First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an iterator instead (search the web for this notion in Python), NN denotes the nonnegative numbers, it is an iterator too:

sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15


Then, you can add the element of your sequence one by one, starting from zero:

sage: s = RDF(0)
sage: for i in range(0,1000000):
....:     s += (-1)^i / composites.next()
....:     print s


Note that if you restart the computation, you have to redefine composites, since otherwise it will continue to be exhausted from its state, not from the beginning.

Note that the convergence is very slow and the computation above is not exact, so some errors mught add up. You could :

• use rational numbers and make exact computations (bu their size will explode)
• increase the precision of the real field
• use interval arithmetics to have a guarantee of your errors

Let me try to mimic you approach in a Pythonic way:

First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an iterator instead (search the web for this notion in Python), NN denotes the nonnegative numbers, it is an iterator too:

sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15


Then, you can add the element elements of your sequence one by one, starting from zero:

sage: s = RDF(0)
sage: for i in range(0,1000000):
xrange(0,1000000):
....:     s += (-1)^i / composites.next()
....:     print s


Note that if you restart the computation, you have to redefine composites, since otherwise it will continue to be exhausted from its state, not from the beginning.

Note that the convergence is very slow and the computation above is not exact, so some errors mught add up. You could :

• use rational numbers and make exact computations (bu their size will explode)
• increase the precision of the real field
• use interval arithmetics to have a guarantee of your errors

Also, note that the variable i is only here to make the sign oscilate, so it is not very useful to compute (-1)^i, instead you can introduce a sign variable and let it alternate at each loop. Also, you can compute them two by two:

sage: while True:
....:     s += 1/composites.next() - 1/composites.next()
....:     print s


Let me try to mimic you approach in a Pythonic way:

First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an iterator instead (search the web for this notion in Python), NN denotes the nonnegative numbers, it is an iterator too:

sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15


Then, you can add the elements of your sequence one by one, starting from zero:

sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....:     s += (-1)^i / composites.next()
....:     print s


Note that if you restart the computation, you have to redefine composites, since otherwise it will continue to be exhausted from its state, not from the beginning.

Note that the convergence is very slow and the computation above is not exact, so some errors mught add up. You could :

• use rational numbers and make exact computations (bu their size will explode)
• increase the precision of the real field
• use interval arithmetics to have a guarantee of your errors

Also, note that the variable i is only here to make the sign oscilate, so it is not very useful to compute (-1)^i, instead you can introduce a sign variable and let it alternate at each loop. Also, you can compute them two by two:

sage: while True:
....:     s += 1/composites.next() - 1/composites.next()
....:     print s


EDIT Regarding the use of RLF, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only makes integer divisions, the element s will be a rational number. You can checking by doing:

sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field


The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number s will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.

Let me try to mimic you approach in a Pythonic way:

First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an iterator instead (search the web for this notion in Python), NN denotes the nonnegative numbers, it is an iterator too:

sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15


Then, you can add the elements of your sequence one by one, starting from zero:

sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....:     s += (-1)^i / composites.next()
....:     print s


Note that if you restart the computation, you have to redefine composites, since otherwise it will continue to be exhausted from its state, not from the beginning.

Note that the convergence is very slow and the computation above is not exact, so some errors mught add up. You could :

• use rational numbers and make exact computations (bu their size will explode)
• increase the precision of the real field
• use interval arithmetics to have a guarantee of your errors

Also, note that the variable i is only here to make the sign oscilate, so it is not very useful to compute (-1)^i, instead you can introduce a sign variable and let it alternate at each loop. Also, you can compute them two by two:

sage: while True:
....:     s += 1/composites.next() - 1/composites.next()
....:     print s


EDIT Regarding the use of RLF, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only makes make integer divisions, the element s will be a rational number. You can checking check this by doing:

sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field


The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number s will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.

Let me try to mimic you approach in a Pythonic way:

First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an iterator instead (search the web for this notion in Python), NN denotes the nonnegative numbers, it is an iterator too:

sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15


Then, you can add the elements of your sequence one by one, starting from zero:

sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....:     s += (-1)^i / composites.next()
....:     print s


Note that if you restart the computation, you have to redefine composites, since otherwise it will continue to be exhausted from its state, not from the beginning.

Note that the convergence is very slow and the computation above is not exact, so some errors mught might add up. You could :

• use rational numbers and make exact computations (bu (but their size will explode)
• increase the precision of the real field
• use interval arithmetics arithmetic to have a guarantee of your errors

Also, note that the variable i is only here to make the sign oscilate, so it is not very useful to compute (-1)^i, instead you can introduce a sign variable and let it alternate at each loop. Also, you can compute them two by two:

sage: while True:
....:     s += 1/composites.next() - 1/composites.next()
....:     print s


EDIT Regarding the use of RLF, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only make integer divisions, the element s will be a rational number. You can check this by doing:

sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field


The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number s will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.