Algorithm Sum From Offset To Number
-
I was using code similar to the following to calculate the sum from offset(where offset >=0) to length:
int offset = 2; int length = 364; float sum = 0.0f; for(int i= offset; i<=length; i++) { sum += (float) i; }
However this seemed a naïve approach so I worked out(from the algorithm for sum (0 to number) that I could use the following: where f=offset n=length sum = ((1+n) * (n/2.0)) - ((1+f) * (f/2.0) - f) which seems to give the correct answer. Out of curiosity what algorithm would normally be used for this kind of thing?
My blog:[^]
-
I was using code similar to the following to calculate the sum from offset(where offset >=0) to length:
int offset = 2; int length = 364; float sum = 0.0f; for(int i= offset; i<=length; i++) { sum += (float) i; }
However this seemed a naïve approach so I worked out(from the algorithm for sum (0 to number) that I could use the following: where f=offset n=length sum = ((1+n) * (n/2.0)) - ((1+f) * (f/2.0) - f) which seems to give the correct answer. Out of curiosity what algorithm would normally be used for this kind of thing?
My blog:[^]
-
Found the algorithm I was after on www.wolframalpha.com/[^] which answers my question.
My blog:[^]
-
Wolfram confirmed that the algorithm in my above question is the one to use. To work out the algorithm, start with the algorithm for (sum i from i=0 to number) which is: (n/2.0) * (n+1) Then we need to subtract the offset using the same algorithm: ((n/2.0) * (n+1)) - ((f/2.0) * (f+1)) And finally account for when the offset is greater than zero by subtracting the offset: ((n/2.0) * (n+1)) - (((f/2.0) * (f+1))-f) So the final algorithm is: ((n/2.0) * (n+1)) - (((f/2.0) * (f+1))-f) Incidentally, the same thing applies for i^2, i^3, i^4, and etc, the algorithm just changes.
My blog:[^]
-
Wolfram confirmed that the algorithm in my above question is the one to use. To work out the algorithm, start with the algorithm for (sum i from i=0 to number) which is: (n/2.0) * (n+1) Then we need to subtract the offset using the same algorithm: ((n/2.0) * (n+1)) - ((f/2.0) * (f+1)) And finally account for when the offset is greater than zero by subtracting the offset: ((n/2.0) * (n+1)) - (((f/2.0) * (f+1))-f) So the final algorithm is: ((n/2.0) * (n+1)) - (((f/2.0) * (f+1))-f) Incidentally, the same thing applies for i^2, i^3, i^4, and etc, the algorithm just changes.
My blog:[^]
-
I was using code similar to the following to calculate the sum from offset(where offset >=0) to length:
int offset = 2; int length = 364; float sum = 0.0f; for(int i= offset; i<=length; i++) { sum += (float) i; }
However this seemed a naïve approach so I worked out(from the algorithm for sum (0 to number) that I could use the following: where f=offset n=length sum = ((1+n) * (n/2.0)) - ((1+f) * (f/2.0) - f) which seems to give the correct answer. Out of curiosity what algorithm would normally be used for this kind of thing?
My blog:[^]
C'mon, that's just the formula found by Gauss for calculating the sum from 1 to n. OK, in your case you have to substract the sum from 1 to offset from that sum (and make sure that you include/exclude the offset). But that's a simple thing.
-
C'mon, that's just the formula found by Gauss for calculating the sum from 1 to n. OK, in your case you have to substract the sum from 1 to offset from that sum (and make sure that you include/exclude the offset). But that's a simple thing.