Yep, I have a Math Degree now
bad answer.litlkeck wrote: c) for c, I would use ML, since anyone could download sml and check the answer out. Code and result follows:
- fun sum(x,y) = if x = y then x else x + sum(x+1, y);
val sum = fn : int * int -> int
- sum(1, 1000000000);
val it = 500000000500000000: int
Gauss did something similar in few seconds without a computer when he was 7.
Thus spake Zarathustra
More simple: all numbers from 1 to 1000000000 is an arithmetic serie.DrPaul wrote:Assuming we are summing over integers and the probem is in base 10, a quick and easy way to sum all the integers between 1 and 1000000000 is to use the fact that the
sum over k for k = 1 to N is N^2/2 + N/2.
In this case, N is 10^9, so the sum is 10^18/2 + 10^9/2 = 5E17 + 5E8 = 500000000500000000. No programming necessary. Same as litlkeck's result.
I had to derive this formula a long time ago.
The sum of the members of an arithmetic serie: ((first + last) * how many) / 2.
((1 + 1000000000) * 1000000000) / 2 = 500000000500000000
Thus spake Zarathustra
Zarathoustra, yes, I'm familiar with Guass' Law. In fact, when I was referring, in a previous post, to my knowledge of series, I was thinking directly of that. I was just trying to demonstrate some knowledge in both Math and Computer Science. Also, another reason for the code was so others could run it and check it out for themselves.zarathoustra wrote:
More simple: all numbers from 1 to 1000000000 is an arithmetic serie.
The sum of the members of an arithmetic serie: ((first + last) * how many) / 2.
((1 + 1000000000) * 1000000000) / 2 = 500000000500000000
~Keck
Yeah what he said... Also if you didn't know, I heard that one plus one equaled two. Hey Jim, How much is that bridge your selling?zarathoustra wrote:More simple: all numbers from 1 to 1000000000 is an arithmetic serie.DrPaul wrote:Assuming we are summing over integers and the probem is in base 10, a quick and easy way to sum all the integers between 1 and 1000000000 is to use the fact that the
sum over k for k = 1 to N is N^2/2 + N/2.
In this case, N is 10^9, so the sum is 10^18/2 + 10^9/2 = 5E17 + 5E8 = 500000000500000000. No programming necessary. Same as litlkeck's result.
I had to derive this formula a long time ago.
The sum of the members of an arithmetic serie: ((first + last) * how many) / 2.
((1 + 1000000000) * 1000000000) / 2 = 500000000500000000
I'm not fat ... I'm festively plump.
I wouldn't call it simpler. It's exactly the same thing.More simple: all numbers from 1 to 1000000000 is an arithmetic serie.
The sum of the members of an arithmetic serie: ((first + last) * how many) / 2.
((1+N)*N)/2 = N^2/2 + N/2. You recognize it as an arithmetic series, I recognize it as a sum over k from 1 to N. Different name, same sum same result.