Ask Your Question

Composite Number Sum Error

asked 2016-07-15 20:55:47 -0500

redbaron01 gravatar image

updated 2016-07-16 08:35:55 -0500

New to sage, although familiar with a few other coding langages. Trying to obtain a sum of alternating terms. Any idea on this one? Thanks in advance.

def a(n) : return [k for k in (1..n) if not is_prime(k)]
from mpmath import *
mp.dps = 15
mp.pretty = True
nsum(lambda n:  (1)/(a(n)), [1,5000], method='alternating')

Error Received:

TypeError: unable to coerce <type 'sage.libs.mpmath.ext_main.mpf'>
to an integer
edit retag flag offensive close merge delete


What sum are you trying to compute?

slelievre gravatar imageslelievre ( 2016-07-16 08:59:42 -0500 )edit

OEIS A269229. The sum of ((-1)^n) / x(n) where x(n) is the next composite (non-prime) number.

redbaron01 gravatar imageredbaron01 ( 2016-07-16 09:04:17 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-16 13:11:50 -0500

tmonteil gravatar image

updated 2016-07-19 03:07:38 -0500

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

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 /
....:     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 might add up. You could :

  • use rational numbers and make exact computations (but their size will explode)
  • increase the precision of the real field
  • use interval 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/ - 1/
....:     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.

edit flag offensive delete link more


This is brilliant - thank you! Would you recommend switching from RDF to RLF and defining the precision of the real field in a way similar to the below method:

composites = (i for i in NN if i>3 and not i.is_prime())
s = RLF(0); s
for i in xrange(0,5000): s += (-1)^i /
print s
redbaron01 gravatar imageredbaron01 ( 2016-07-16 18:47:16 -0500 )edit

I edited my answer about RLF to have more space.

tmonteil gravatar imagetmonteil ( 2016-07-18 06:55:45 -0500 )edit

Also, note that writing RealField(300)(s) is not changing s, it is just returning another object. Note also that RLF and RealField(300)(s) are very different objects.

tmonteil gravatar imagetmonteil ( 2016-07-18 06:58:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-07-15 20:55:47 -0500

Seen: 130 times

Last updated: Jul 19 '16

Related questions