top | item 6162265

(no title)

joe24pack | 12 years ago

I think I might be doing it wrong, because I didn't do any looping. I'm a bit too lazy and impatient for that, who wants to spend their afternoon adding all those numbers up even with a computer.

  [joe24pack@staropramen ~]$ python
  Python 2.6.6 (r266:84292, May  1 2012, 13:52:17) 
  [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> def gauss(x):
  ...   return (x+1)*(x/2)
  ... 
  >>> gauss(10)
  55
  >>> gauss(1000000000)
  500000000500000000
  >>>

discuss

order

Tibbes|12 years ago

Hmmmm, make that:

    def gauss(x):
        return (x+1)*x/2
(consider gauss(11), for example)

You gotta admit, on an article about the difference between integer and floating-point arithmetic, that's pretty ironic!

joe24pack|12 years ago

It's odd that the odd numbers slipped my mind. Thank you for your gracious correction.