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.Sat, 11 Aug 2018 16:39:21 +0200How can I find the sum of first 20 positive perfect square?https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/Hi! First post!
When I type " k^2 for k in [1..20] ",
it says
" k**Integer(2) for k in (ellipsis_range(Integer(1),Ellipsis,Integer(20)))
^
SyntaxError: invalid syntax "
What can I type?
Fri, 10 Aug 2018 15:57:42 +0200https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/Answer by slelievre for <p>Hi! First post!</p>
<p>When I type " k^2 for k in [1..20] ",</p>
<p>it says </p>
<p>" k**Integer(2) for k in (ellipsis_range(Integer(1),Ellipsis,Integer(20)))
^
SyntaxError: invalid syntax "</p>
<p>What can I type?</p>
<p></p>
https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/?answer=43314#post-id-43314Note that you can use `(1 .. 20)` to iterate through the integers from 1 to 20,
without constructing the list of these integers, as `[1 .. 20]` does.
To obtain an iterator of squares:
sage: (k^2 for k in (1 .. 20))
To obtain the list of squares:
sage: [k^2 for k in (1 .. 20)]
To obtain the sum of squares:
sage: sum(k^2 for k in (1 .. 20))Fri, 10 Aug 2018 17:51:25 +0200https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/?answer=43314#post-id-43314Comment by pizza for <p>Note that you can use <code>(1 .. 20)</code> to iterate through the integers from 1 to 20,
without constructing the list of these integers, as <code>[1 .. 20]</code> does.</p>
<p>To obtain an iterator of squares:</p>
<pre><code> sage: (k^2 for k in (1 .. 20))
</code></pre>
<p>To obtain the list of squares:</p>
<pre><code>sage: [k^2 for k in (1 .. 20)]
</code></pre>
<p>To obtain the sum of squares:</p>
<pre><code>sage: sum(k^2 for k in (1 .. 20))
</code></pre>
https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/?comment=43334#post-id-43334Thank you for answering.Sat, 11 Aug 2018 16:39:21 +0200https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/?comment=43334#post-id-43334