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.Wed, 11 Jul 2018 16:23:21 +0200How to sum numbers on a list?https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/ Hi, I'm stuck at this problem:
**Create the SC20 list with the sums of the squares of the 20 first positive integers**
I know that if I want to do a list with the squares of the 20 first positive integers I have to do this:
[(x+1)^2 for x in range(20)]
And I'll get this:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
But I don't know how to do it with the sums:
Basically, what I want to get is this:
[5,14,30,55,...]
Do you know how to do it?
Thanks in advance.
P.S. Sorry for my English.Fri, 29 Jun 2018 17:39:18 +0200https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/Comment by tmonteil for <p>Hi, I'm stuck at this problem:</p>
<p><strong>Create the SC20 list with the sums of the squares of the 20 first positive integers</strong></p>
<p>I know that if I want to do a list with the squares of the 20 first positive integers I have to do this:
[(x+1)^2 for x in range(20)]
And I'll get this:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]</p>
<p>But I don't know how to do it with the sums:
Basically, what I want to get is this:
[5,14,30,55,...]</p>
<p>Do you know how to do it?
Thanks in advance.
P.S. Sorry for my English.</p>
https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?comment=42774#post-id-42774This looks like homework. Could you tell us what did you try already, and where were you stuck ? Did you try to mae a ``for`` loop ?Fri, 29 Jun 2018 17:46:34 +0200https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?comment=42774#post-id-42774Comment by Norraimon for <p>Hi, I'm stuck at this problem:</p>
<p><strong>Create the SC20 list with the sums of the squares of the 20 first positive integers</strong></p>
<p>I know that if I want to do a list with the squares of the 20 first positive integers I have to do this:
[(x+1)^2 for x in range(20)]
And I'll get this:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]</p>
<p>But I don't know how to do it with the sums:
Basically, what I want to get is this:
[5,14,30,55,...]</p>
<p>Do you know how to do it?
Thanks in advance.
P.S. Sorry for my English.</p>
https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?comment=42775#post-id-42775I tried to use sum() function, but it didn't work.
I could use a for loop, but all the code has to be in this: LSC_20 =Fri, 29 Jun 2018 17:59:05 +0200https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?comment=42775#post-id-42775Answer by Norraimon for <p>Hi, I'm stuck at this problem:</p>
<p><strong>Create the SC20 list with the sums of the squares of the 20 first positive integers</strong></p>
<p>I know that if I want to do a list with the squares of the 20 first positive integers I have to do this:
[(x+1)^2 for x in range(20)]
And I'll get this:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]</p>
<p>But I don't know how to do it with the sums:
Basically, what I want to get is this:
[5,14,30,55,...]</p>
<p>Do you know how to do it?
Thanks in advance.
P.S. Sorry for my English.</p>
https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?answer=42776#post-id-42776I think I found a way to do it:
LSC_20 = [sum(LC_20[:i+1]) for i in range(len(LC_20))]
Note: LC_20 is the name I put to the first list.
I'm closing this post since the problem is now solved.
Sorry for bothering you, guys.Fri, 29 Jun 2018 18:35:09 +0200https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?answer=42776#post-id-42776Comment by slelievre for <p>I think I found a way to do it:
LSC_20 = [sum(LC_20[:i+1]) for i in range(len(LC_20))]
Note: LC_20 is the name I put to the first list.
I'm closing this post since the problem is now solved.
Sorry for bothering you, guys.</p>
https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?comment=42921#post-id-42921You can accept your own answer.Wed, 11 Jul 2018 16:23:21 +0200https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/?comment=42921#post-id-42921