Odd Series?

16 views
Skip to first unread message

Chris Maness

unread,
Jun 25, 2014, 5:58:33 PM6/25/14
to sage-s...@googlegroups.com
Is there a way to only add the odd integers of a sum in Sage?

Chris 

kcrisman

unread,
Jun 25, 2014, 11:21:07 PM6/25/14
to sage-s...@googlegroups.com


Is there a way to only add the odd integers of a sum in Sage?

Do you mean something like this?

sage: L = [1,2,3,4,5,6,7,8,9,10]
sage: L[1::2]
[2, 4, 6, 8, 10]
sage: L[0::2]
[1, 3, 5, 7, 9]
sage: sum(L[1::2])
30

I didn't know about this cool feature of slice notation until looking it up - see http://stackoverflow.com/questions/12433695/extract-elements-of-list-at-odd-positions

- kcrisman

John Cremona

unread,
Jun 26, 2014, 4:10:01 AM6/26/14
to SAGE support
On 26 June 2014 04:21, kcrisman <kcri...@gmail.com> wrote:
>
>
>> Is there a way to only add the odd integers of a sum in Sage?
>
>
> Do you mean something like this?
>
> sage: L = [1,2,3,4,5,6,7,8,9,10]
> sage: L[1::2]
> [2, 4, 6, 8, 10]
> sage: L[0::2]
> [1, 3, 5, 7, 9]
> sage: sum(L[1::2])
> 30

Or perhaps this?

sage: L = [1,2,3,4,5,6,7,8,9,10]
sage: sum(L)
55
sage: sum([x for x in L if x%2==1])
25



>
> I didn't know about this cool feature of slice notation until looking it up
> - see
> http://stackoverflow.com/questions/12433695/extract-elements-of-list-at-odd-positions
>
> - kcrisman
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages