By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

sum of squares of consecutive numbers

P: 31
1*1 + 2*2 + 3*3 +... + 49*49 + 50*50

how do i write the thing in bold without having to write every number multiplied by the same number....is there an easier way? is there a simple one word statement/anything?
Feb 22 '09 #1
Share this Question
Share on Google+
2 Replies


boxfish
Expert 100+
P: 469
Will this loop do?
Expand|Select|Wrap|Line Numbers
  1. squareSum = 0
  2. for i in xrange(1, 51):
  3.     squareSum += i * i
Feb 23 '09 #2

bvdet
Expert Mod 2.5K+
P: 2,851
It can be condensed into a list comprehension.
Expand|Select|Wrap|Line Numbers
  1. sum([i*i for i in range(1,51)])
Feb 23 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.