Dumb python questions

Joal Heagney s713221 at student.gu.edu.au
Wed Aug 15 21:36:30 EDT 2001


Paul Rubin wrote:

> Here's another dumb question, about the for loop: if I want to add
> up the first 100 numbers in the naive way, am I really supposed to say
> 
>    sum = 0
>    for i in range(101) :
>      sum = sum + i
> 

Out of curiosity, is there a reason (e.g. curiosity on your part?)
you're not using:

N = 100
sum = N*(N+1)/2

which returns the sum of the first 100 integers much more quickly?
-- 
      Joal Heagney is: _____           _____
   /\ _     __   __ _    |     | _  ___  |
  /__\|\  ||   ||__ |\  || |___|/_\|___] |
 /    \ \_||__ ||___| \_|! |   |   \   \ !



More information about the Python-list mailing list