C# code for summing up
-
Is it possible to get an approximate sum of S defined as 1/2 + 1/4 + 1/8 to a certain number of counts? Say up to 1/16? If so how does one write such a code in c# to achieve this? Thanks
Angelinna wrote:
If so how does one write such a code in c# to achieve this?
Personally speaking, I'd use a loop. It's the simplest way.
Deja View - the feeling that you've seen this post before.
-
Is it possible to get an approximate sum of S defined as 1/2 + 1/4 + 1/8 to a certain number of counts? Say up to 1/16? If so how does one write such a code in c# to achieve this? Thanks
You could use a Fraction/Rational number class or find the GCD of all the numbers, multiply them by the GCD, sum them all, then divide by the GCD.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Is it possible to get an approximate sum of S defined as 1/2 + 1/4 + 1/8 to a certain number of counts? Say up to 1/16? If so how does one write such a code in c# to achieve this? Thanks
-
Angelinna wrote:
If so how does one write such a code in c# to achieve this?
Personally speaking, I'd use a loop. It's the simplest way.
Deja View - the feeling that you've seen this post before.
-
You can get much better than an approximation, you can get the exact value. The sum of (1/2 + 1/4 + 1/8 + ... + 1/n) is (1 - 1/n).
Despite everything, the person most likely to be fooling you next is yourself.
-
Would this operate as a recursive function? If not, how best can one implement the summation recursively.
-
You can get much better than an approximation, you can get the exact value. The sum of (1/2 + 1/4 + 1/8 + ... + 1/n) is (1 - 1/n).
Despite everything, the person most likely to be fooling you next is yourself.
Good point. I should have thought of that. :doh: I've been out of school way too long.
Deja View - the feeling that you've seen this post before.
-
Is it possible to get an approximate sum of S defined as 1/2 + 1/4 + 1/8 to a certain number of counts? Say up to 1/16? If so how does one write such a code in c# to achieve this? Thanks
You don't need any loop or recursion. Here's the formula: http://www.ies.co.jp/math/java/misc/sum/sum.html[^]. The formula is at the end of the page; 'a' is the first number in the series, and 'r' is the ratio between consecutive terms.
-
You don't need any loop or recursion. Here's the formula: http://www.ies.co.jp/math/java/misc/sum/sum.html[^]. The formula is at the end of the page; 'a' is the first number in the series, and 'r' is the ratio between consecutive terms.