Yep, I have a Math Degree now

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
bad answer.
Gauss did something similar in few seconds without a computer when he was 7.
Thus spake Zarathustra
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.
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
Thus spake Zarathustra
I was going to post that but Z beat me to the punch.


And if you believe that, I have a nice little bridge in Brooklyn for sale.
"If you look to me for illumination, you better have a flashlight!"
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
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.

~Keck
Image
zarathoustra wrote:
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.
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
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?
I'm not fat ... I'm festively plump.
I'm selling the eiffel tower, if a scrap merchant needs cheap steel.
MP me for more details. ;)
Thus spake Zarathustra
You guys! :lol: :lol: :lol:
"If you look to me for illumination, you better have a flashlight!"
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.
I wouldn't call it simpler. It's exactly the same thing.
((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.
DrPaul wrote:I wouldn't call it simpler. It's exactly the same thing.
((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.
3 operations vs 4 operations :)
Thus spake Zarathustra
It's what I refer to as "the same thing only different."
"If you look to me for illumination, you better have a flashlight!"
3 operations vs 4 operations
Well, if you want to be that way about it,
(N^2+N)/2. I expanded my expression for clarity and ease of
writing it in scientific notation.
Your way:
(1.000000001E9)*1E9/2
My way:
1E18/2 + 1E9/2
It's just a matter of style. Whatever works for ya.