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. Algorithms
  4. Algorithm Sum From Offset To Number

Algorithm Sum From Offset To Number

Scheduled Pinned Locked Moved Algorithms
comalgorithmstoolsquestion
8 Posts 3 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
    A
    wrote on last edited by
    #1

    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:[^]

    A B 2 Replies Last reply
    0
    • A A

      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:[^]

      A Offline
      A Offline
      A
      wrote on last edited by
      #2

      Found the algorithm I was after on www.wolframalpha.com/[^] which answers my question.

      My blog:[^]

      D 1 Reply Last reply
      0
      • A A

        Found the algorithm I was after on www.wolframalpha.com/[^] which answers my question.

        My blog:[^]

        D Offline
        D Offline
        dusty_dex
        wrote on last edited by
        #3

        Your link to Wolfram Alpha is pointless. Where is the answer you found?

        A 1 Reply Last reply
        0
        • D dusty_dex

          Your link to Wolfram Alpha is pointless. Where is the answer you found?

          A Offline
          A Offline
          A
          wrote on last edited by
          #4

          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:[^]

          D 1 Reply Last reply
          0
          • A A

            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:[^]

            D Offline
            D Offline
            dusty_dex
            wrote on last edited by
            #5

            But, does it have a name. this algorithm?

            A 1 Reply Last reply
            0
            • D dusty_dex

              But, does it have a name. this algorithm?

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

              Yes, Partial sums or Finite series.

              My blog:[^]

              1 Reply Last reply
              0
              • A A

                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:[^]

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                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.

                D 1 Reply Last reply
                0
                • B Bernhard Hiller

                  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.

                  D Offline
                  D Offline
                  dusty_dex
                  wrote on last edited by
                  #8

                  Oh. I was thinking it looked more like one of Fermat theorems. :)

                  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