Re:I wear your shirt
by m0th87 on Wednesday November 25, 2009 @03:13AM
(#960832)
This is one of those domain-specific areas in which Wolfram is awesome:
http://www.wolframalpha.com/input/?i=sum+of+n+from+1+to+365
[ Parent
| on HN ]
We can't all be Gauss but, then again, we're not in fourth grade anymore, either. You can sum over any range trivially: average of the endpoints times length of the range. The average of 1 and 365 is 183. There are 365 elements between 1 and 365 (both inclusive). 183 * 365 = 66795. Yay, right answer.
(There is a formula. I have a poor memory for formulas but am good at transforming English into them, so I just remember the English gloss.)
I'm decently impressed that Wolfram was able to spit out something equivalent to the right formula with only a little massaging of the inputs from me:
http://www.wolframalpha.com/input/?i=sum+of+n+from+n%3Da+to+...
I was referring to the fact that Wolfram actually understood the input, not that it answered a trivial math question :)
(loop for x from 1 to 365 summing x) ==> 66795There's a famous possibly true story about Gauss having to sum the numbers 1 to 100 as a child. Most versions tell it that he noticed that 1 + 100 = 2 + 99 = 3 + 98 and so on, yielding a general formula of n * (n + 1) / 2, so you could probably skip Wolfram for this one. (Explanation: you have n numbers, on average each is equal to the first + the last divided by 2)
Unless you want it for this: http://www.wolframalpha.com/input/?i=sum+of+n+from+1+to+n
A REPL is nice too:
(reduce + (range 366))Python:
reduce (operator.add, range(366))or
sum(range(366)) (sum (range 1 365))
assuming you defined these already; I have them in my utils library ;-) (defun range (start end)
(loop for x from start to end collecting x))
(defun sum (sequence)
(reduce #'+ sequence))didn't even think of that one! :)
Re:I wear your shirt
by karanbhangui on Wednesday November 25, 2009 @12:52PM
(#961519)
or plain math: n(n+1)/2
How quickly can you generalize the plain math formula to these sequences: http://www.research.att.com/njas/sequences/index.html
I knew there must be a formula but I totally just broke out a spreadsheet and auto-generated the sequence and then auto-summed it.
Math/brain fail.
Re:I wear your shirt
by RyanMcGreal on Wednesday November 25, 2009 @09:36AM
(#961143)
Alternately: fast repetition of trivial operations win.