sum of squares of consecutive numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v13tn1g
    New Member
    • Feb 2009
    • 31

    sum of squares of consecutive numbers

    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?
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Will this loop do?
    Code:
    squareSum = 0
    for i in xrange(1, 51):
        squareSum += i * i

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      It can be condensed into a list comprehension.
      Code:
      sum([i*i for i in range(1,51)])

      Comment

      Working...