4 ms·
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 http://www.wolframalpha.com/
by m0th87 17y ago
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 http://www.wolframalpha.com/input/?i=sum+of+n+from+1+to+365
- patio11 17y agoWe 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+n%3Db http://www.wolframalpha.com/input/?i=sum+of+n+from+n%3Da+to+...
- three14 17y agoThere'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 http://www.wolframalpha.com/input/?i=sum+of+n+from+1+to+n
- Raphael 17y agoPreaching to the choir. I used it all day for physics homework.
- seldo 17y agoI 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.
- RyanMcGreal 17y agoAlternately: fast repetition of trivial operations win.
- icey 17y agoA REPL is nice too: (reduce + (range 366))
- deleted 17y ago[deleted]
- Torn 17y agoPython: reduce (operator.add, range(366))
- tome 17y agoor sum(range(366))
- deleted 17y ago[deleted]
- Torn 17y agodidn't even think of that one! :)
- karanbhangui 17y agoor plain math: n(n+1)/2
- mahmud 17y agoHow quickly can you generalize the plain math formula to these sequences: http://www.research.att.com/njas/sequences/index.html http://www.research.att.com/njas/sequences/index.html
- deleted 17y ago[deleted]
- mahmud 17y ago(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))