Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# code for summing up

C# code for summing up

Scheduled Pinned Locked Moved C#
csharpquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Angelinna
    wrote on last edited by
    #1

    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

    P L G A 4 Replies Last reply
    0
    • A Angelinna

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      My blog | My articles

      A 1 Reply Last reply
      0
      • A Angelinna

        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

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        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))

        1 Reply Last reply
        0
        • A Angelinna

          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

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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.

          A P 2 Replies Last reply
          0
          • P Pete OHanlon

            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.

            My blog | My articles

            A Offline
            A Offline
            Angelinna
            wrote on last edited by
            #5

            Just how does a loop get applied in this case.

            1 Reply Last reply
            0
            • G Guffa

              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.

              A Offline
              A Offline
              Angelinna
              wrote on last edited by
              #6

              Would this operate as a recursive function? If not, how best can one implement the summation recursively.

              G 1 Reply Last reply
              0
              • A Angelinna

                Would this operate as a recursive function? If not, how best can one implement the summation recursively.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Why would you do that as a recursive function? It's a very simple calculation. There is no looping required of any kind.

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                • G Guffa

                  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.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  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.

                  My blog | My articles

                  1 Reply Last reply
                  0
                  • A Angelinna

                    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

                    A Offline
                    A Offline
                    Alan Balkany
                    wrote on last edited by
                    #9

                    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.

                    A 1 Reply Last reply
                    0
                    • A Alan Balkany

                      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.

                      A Offline
                      A Offline
                      Angelinna
                      wrote on last edited by
                      #10

                      Thanks all for your guidance. I am trying to have it all programmed in c#.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups