Ask Your Question
2

Composite Number Sum Error

asked 2016-07-16 03:55:47 +0200

redbaron01 gravatar image

updated 2016-07-16 15:35:55 +0200

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

Comments

What sum are you trying to compute?

slelievre gravatar imageslelievre ( 2016-07-16 15:59:42 +0200 )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 16:04:17 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-07-16 20:11:50 +0200

tmonteil gravatar image

updated 2016-07-19 10:07:38 +0200

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

edit flag offensive delete link more

Comments

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
RealField(300)(s)
for i in xrange(0,5000): s += (-1)^i / composites.next()
print s
redbaron01 gravatar imageredbaron01 ( 2016-07-17 01:47:16 +0200 )edit

I edited my answer about RLF to have more space.

tmonteil gravatar imagetmonteil ( 2016-07-18 13:55:45 +0200 )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 13:58:36 +0200 )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

Stats

Asked: 2016-07-16 03:55:47 +0200

Seen: 646 times

Last updated: Jul 19 '16