ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 19 Jun 2015 16:02:08 +0200Summing primes, cannot convert to inthttps://ask.sagemath.org/question/27159/summing-primes-cannot-convert-to-int/What can I do so that the following code works:
sage: var('k')
sage: sum(nth_prime(k),k,1,10)
ValueError: cannot convert k to int
Thanks in advance!Fri, 19 Jun 2015 15:33:10 +0200https://ask.sagemath.org/question/27159/summing-primes-cannot-convert-to-int/Comment by kcrisman for <p>What can I do so that the following code works:</p>
<pre><code>sage: var('k')
sage: sum(nth_prime(k),k,1,10)
ValueError: cannot convert k to int
</code></pre>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/27159/summing-primes-cannot-convert-to-int/?comment=27163#post-id-27163The reason this *doesn't* work while `sum(x^2,x,1,10)` does is because `nth_prime` isn't a symbolic expression, it's just a random Sage function. `nth_prime(x)` already fails all by itself.Fri, 19 Jun 2015 16:02:08 +0200https://ask.sagemath.org/question/27159/summing-primes-cannot-convert-to-int/?comment=27163#post-id-27163Answer by slelievre for <p>What can I do so that the following code works:</p>
<pre><code>sage: var('k')
sage: sum(nth_prime(k),k,1,10)
ValueError: cannot convert k to int
</code></pre>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/27159/summing-primes-cannot-convert-to-int/?answer=27161#post-id-27161Don't involve symbolic variables.
Try this instead:
sage: sum(nth_prime(k) for k in (1 .. 10))
129Fri, 19 Jun 2015 15:52:23 +0200https://ask.sagemath.org/question/27159/summing-primes-cannot-convert-to-int/?answer=27161#post-id-27161