Formula calculation C# code needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GB

    Formula calculation C# code needed

    Hello,
    How to calculate value for the following formula (I need C# code):

    res = (((m+1)(m+2)... (m+(k-1)))/1.2...(k-1))

    or more generalized formula is:

    k-1
    __
    | | (m+i)
    i=1
    res = ---------------
    k-1
    __
    | | i
    i=1


    Thanks,
    GB


  • Jon Skeet [C# MVP]

    #2
    Re: Formula calculation C# code needed

    GB <gennady@telus. netwrote:
    Hello,
    How to calculate value for the following formula (I need C# code):
    >
    res = (((m+1)(m+2)... (m+(k-1)))/1.2...(k-1))
    >
    or more generalized formula is:
    >
    k-1
    __
    | | (m+i)
    i=1
    res = ---------------
    k-1
    __
    | | i
    i=1
    >
    Well, why not just do the calculation in a loop? Loop round doing the
    multiplication, then loop round doing the division. You'll lose a fair
    amount of precision, but that may be okay depending on what your use
    is...

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • GB

      #3
      Re: Formula calculation C# code needed

      Please, give me an example.

      GB
      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.1f5f3d 13b7c680b98d401 @msnews.microso ft.com...
      GB <gennady@telus. netwrote:
      >Hello,
      >How to calculate value for the following formula (I need C# code):
      >>
      >res = (((m+1)(m+2)... (m+(k-1)))/1.2...(k-1))
      >>
      >or more generalized formula is:
      >>
      > k-1
      > __
      > | | (m+i)
      > i=1
      >res = ---------------
      > k-1
      > __
      > | | i
      > i=1
      >>
      >
      Well, why not just do the calculation in a loop? Loop round doing the
      multiplication, then loop round doing the division. You'll lose a fair
      amount of precision, but that may be okay depending on what your use
      is...
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Ben Newsam

        #4
        Re: Formula calculation C# code needed

        On Wed, 30 Aug 2006 06:26:21 GMT, "GB" <gennady@telus. netwrote:
        >Please, give me an example.
        >
        >GB
        >"Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        >news:MPG.1f5f3 d13b7c680b98d40 1@msnews.micros oft.com...
        >GB <gennady@telus. netwrote:
        >>Hello,
        >>How to calculate value for the following formula (I need C# code):
        >>>
        >>res = (((m+1)(m+2)... (m+(k-1)))/1.2...(k-1))
        >>>
        >>or more generalized formula is:
        >>>
        >> k-1
        >> __
        >> | | (m+i)
        >> i=1
        >>res = ---------------
        >> k-1
        >> __
        >> | | i
        >> i=1
        Let me get this right...

        In your example, say, if m=3 and k=10, and i=1 (is i *always* 1?),
        then you need to calculate:

        (4.5.6.7.8.9) / (1.2.3.4.5.6.7. 8.9), which works out at 0.16667 (same
        as 1/6)

        and m=4 and k=9 would be (5.6.7.8)/(1.2.3.4.5.6.7. 8), which is
        0.041667

        If this is correct, then I would do it recursively:


        long MyFunc ( long n, long Limit, long i )
        {
        long Result = n;
        if ( n <= Limit )
        return Result;
        return ( n * MyFunc ( n - i, Limit, i ) );
        }

        Then you can do:

        long k = 10;
        long m = 3;
        long i = 1;
        double res1 = 0.0, res2 = 0.0;
        double result = 0.0;

        res1 = (double) MyFunc(k - 1, m + 1, i);
        res2 = (double) MyFunc(k - 1, 1, 1);
        result = res1 / res2;

        and
        k = 9;
        m = 4;
        res1 = (double) MyFunc(k - 1, m + 1, i);
        res2 = (double) MyFunc(k - 1, 1, 1);
        result = res1 / res2;

        The last one gives 0.041666...664
        Oh well, it's close.

        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • DaanishRumani

          #5
          Re: Formula calculation C# code needed

          I would do it this way.

          // Numerator
          double Pi_MplusI(int start, int end, int m)
          {
          double result = 1.0;

          if(start end)
          {
          // swap start and end
          int temp = start;
          start = end;
          end = temp;
          }

          if(start == end)
          {
          // not sure about this!!!
          return 1.0;
          }

          for(int i= start; i < end; ++i)
          {
          result *= m + i;
          }

          return result;
          }

          // Denominator
          double Pi_I(int start, int end)
          {
          double result = 1.0;

          if(start end)
          {
          // swap start and end
          int temp = start;
          start = end;
          end = temp;
          }

          if(start == end)
          {
          // not sure about this!!!
          return 1.0;
          }

          for(int i= start; i < end; ++i)
          {
          result *= i;
          }

          return result;
          }

          // Result
          double res(int i, int m)
          {
          double result = 1.0;
          int i;
          int k;
          int m;

          // get your values here
          i = 1;
          k = 10;
          m = 100;

          result = Pi_MplusI(1, k) / Pi_I(1, k);

          return result;
          }

          Comment

          • Marc Gravell

            #6
            Re: Formula calculation C# code needed

            actually, if my math is right that would be 4.5.6.7.8.9.10. 11.12 /
            1.2.3.4.5.6.7.8 .9, leaving 10.11.12 / 1.2.3, leaving 220 [i is just the
            product-indexer in the range 1 to k-1 inclusive]

            Perhaps the trick here is to look for those terms that are not cancelled;
            you could probably do this by looking at the figures and doing a lot of
            thinking, but you'd need to think for each and every formula; how about this
            instead? It detects cancelled terms before they are multiplied (reducing
            both rounding error and the FLOP count), and could be applied to any such
            formula.

            Marc

            class Product {
            // dictionary key is the factor, value is the power
            readonly Dictionary<int, intfactors = new Dictionary<int, int>();
            public void Multiply(int factor) {
            int power;
            factors.TryGetV alue(factor, out power);
            factors[factor] = power + 1;
            }
            public void Divide(int quotient) {
            int power;
            factors.TryGetV alue(quotient, out power);
            factors[quotient] = power - 1;
            }
            public double Evaluate() {
            // could also perhaps do with logarithms?
            double result = 1.0;
            foreach (KeyValuePair<i nt, intpair in factors) {
            int power = pair.Value;
            if (power == 0) continue; // nothing to do
            result *= Math.Pow(pair.K ey, power);
            }
            return result;
            }

            }
            static class Program {
            static void Main() {
            int k = 10, m = 3;
            Product p = new Product();
            // could actually do both loops at once, but
            // keep it simple for easy re-use with different
            // maths...
            for(int i = 1; i <= k - 1; i++) {
            p.Multiply(m+i) ;
            }
            for (int i = 1; i <= k - 1; i++) {
            p.Divide(i);
            }
            MessageBox.Show (p.Evaluate().T oString());
            }
            }


            Comment

            • Marc Gravell

              #7
              Re: Formula calculation C# code needed

              Some refinements below:
              * handles zero multipliers much more efficiently (short-circuits everything)
              * throws exception on zero quotients
              * tracks "sign" separately, so that -17 can cancel 17
              * discards "unit" (but tracks sign)
              * performs single * and / directly, to avoid a Math.Pow() call

              Marc

              class Product {
              readonly Dictionary<int, intfactors = new Dictionary<int, int>();
              bool zero = false, negate = false;
              public void Multiply(int factor) {
              if (zero) return;
              if (factor == 0) {
              zero = true;
              factors.Clear() ;
              return; // all over
              }
              if (factor < 0) {
              negate = !negate;
              factor = -factor;
              }
              if (factor == 1) { // unit
              return; // nothing more to do
              }
              int power;
              factors.TryGetV alue(factor, out power);
              factors[factor] = power + 1;
              }
              public void Divide(int quotient) {
              if (quotient == 0) throw new DivideByZeroExc eption();
              if (zero) return;
              if (quotient < 0) {
              negate = !negate;
              quotient = -quotient;
              }
              if (quotient == 1) { // unit
              return; // nothing more to do
              }
              int power;
              factors.TryGetV alue(quotient, out power);
              factors[quotient] = power - 1;
              }
              public double Evaluate() {
              if (zero) return 0;
              // could also perhaps do with logarithms?
              double result = 1.0;
              foreach (KeyValuePair<i nt, intpair in factors) {
              int factor = pair.Key, power = pair.Value;
              switch (power) {
              case 0: break; // do nothing (cancelled factor)
              case 1: result *= factor; break; // single mult
              case -1: result /= factor; break; // single div
              default: result *= Math.Pow(factor , power); break; //
              everything else
              }
              }
              return negate ? -result : result;
              }
              }


              Comment

              • Rick Lones

                #8
                Re: Formula calculation C# code needed

                So you are evalulating C(m+k-1, k-1), the number of combinations of (m+k-1)
                things taken (k-1) at a time, correct?

                Then you want:

                Prod((m+1),(m+k-1)) / Prod(1, (k-1)), where:

                (warning, untested code!)

                long Prod(int low, int high)
                {
                long result = low;
                int multiplier = low;

                while (++multiplier <= high)
                result *= multiplier;

                return result;
                }

                HTH,
                -rick-

                GB wrote:
                Hello,
                How to calculate value for the following formula (I need C# code):
                >
                res = (((m+1)(m+2)... (m+(k-1)))/1.2...(k-1))
                >
                or more generalized formula is:
                >
                k-1
                __
                | | (m+i)
                i=1
                res = ---------------
                k-1
                __
                | | i
                i=1
                >
                >
                Thanks,
                GB
                >
                >

                Comment

                • GB

                  #9
                  Re: Formula calculation C# code needed

                  Thank you , Rick.
                  It works perfectly for me!

                  GB

                  2
                  "Rick Lones" <WrlonesX@Ychar terZ.netwrote in message
                  news:t6fJg.172$ JQ2.7@newsfe07. lga...
                  So you are evalulating C(m+k-1, k-1), the number of combinations of
                  (m+k-1)
                  things taken (k-1) at a time, correct?
                  >
                  Then you want:
                  >
                  Prod((m+1),(m+k-1)) / Prod(1, (k-1)), where:
                  >
                  (warning, untested code!)
                  >
                  long Prod(int low, int high)
                  {
                  long result = low;
                  int multiplier = low;
                  >
                  while (++multiplier <= high)
                  result *= multiplier;
                  >
                  return result;
                  }
                  >
                  HTH,
                  -rick-
                  >
                  GB wrote:
                  Hello,
                  How to calculate value for the following formula (I need C# code):

                  res = (((m+1)(m+2)... (m+(k-1)))/1.2...(k-1))

                  or more generalized formula is:

                  k-1
                  __
                  | | (m+i)
                  i=1
                  res = ---------------
                  k-1
                  __
                  | | i
                  i=1


                  Thanks,
                  GB

                  Comment

                  Working...