Ask Your Question
0

How can I find the sum of first 20 positive perfect square?

asked 2018-08-10 15:57:42 +0100

pizza gravatar image

updated 2022-01-21 21:41:20 +0100

FrédéricC gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2018-08-10 17:51:25 +0100

slelievre gravatar image

Note 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))
edit flag offensive delete link more

Comments

Thank you for answering.

pizza gravatar imagepizza ( 2018-08-11 16:39:21 +0100 )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

Stats

Asked: 2018-08-10 15:57:42 +0100

Seen: 548 times

Last updated: Aug 21 '18