473,417 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

Formula calculation C# code needed

GB
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
Aug 30 '06 #1
8 3222
GB <ge*****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 30 '06 #2
GB
Please, give me an example.

GB
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP***********************@msnews.microsoft.co m...
GB <ge*****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Aug 30 '06 #3
On Wed, 30 Aug 2006 06:26:21 GMT, "GB" <ge*****@telus.netwrote:
>Please, give me an example.

GB
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP***********************@msnews.microsoft.c om...
>GB <ge*****@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

Aug 30 '06 #4
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;
}

Aug 30 '06 #5
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.TryGetValue(factor, out power);
factors[factor] = power + 1;
}
public void Divide(int quotient) {
int power;
factors.TryGetValue(quotient, out power);
factors[quotient] = power - 1;
}
public double Evaluate() {
// could also perhaps do with logarithms?
double result = 1.0;
foreach (KeyValuePair<int, intpair in factors) {
int power = pair.Value;
if (power == 0) continue; // nothing to do
result *= Math.Pow(pair.Key, 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().ToString());
}
}
Aug 30 '06 #6
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.TryGetValue(factor, out power);
factors[factor] = power + 1;
}
public void Divide(int quotient) {
if (quotient == 0) throw new DivideByZeroException();
if (zero) return;
if (quotient < 0) {
negate = !negate;
quotient = -quotient;
}
if (quotient == 1) { // unit
return; // nothing more to do
}
int power;
factors.TryGetValue(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<int, 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;
}
}
Aug 30 '06 #7
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

Aug 30 '06 #8
GB
Thank you , Rick.
It works perfectly for me!

GB

2
"Rick Lones" <Wr******@YcharterZ.netwrote in message
news:t6*************@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

Aug 30 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cowboyboborton | last post by:
Looking for some help here. I've tried to solve this, but I just can't. What I need to know is what formula to use in an excel calculation to complete the following calculation. It's in two...
2
by: alex | last post by:
I need a more advanced formula than just an average for calculating items rating. I have: raitng value is on scale 1 to 10. s - sum of all ratings for an item n - number of rates (votes)
3
by: SiewSa | last post by:
I have come to a situation that I need to use the result of a string variable as a formula for performing calculation. As an example below: Dim X As Integer Dim Y As Integer Dim Result...
3
by: Mike | last post by:
Hi, I have three tables in the following structure (simplified): Table 1: Containing the customers ------------------------------------------------- create table Customers ( int...
5
by: steve | last post by:
Hi All Not sure if this is the right forum, but I need the formula for calculating the amount of Sales tax (GST) from the tax included price In Australia GST is 10% and the standard formula is...
8
by: grahamhow424 | last post by:
Hi I am trying to figure out how to duplicate a, financial, calculation that uses the caret, Exponentiation. Here's the formula... A = 0.0755 B = 34 C = 50000
1
by: JHaworth | last post by:
Hi, Please could someone point me in the right direction with a formula I am trying to create which will enable me to calculate a Net IRR for a series of data? My Problem: •My formula in...
2
by: =?Utf-8?B?QWxhbg==?= | last post by:
I have a InfoPath form that I need to use an event handler to perform a calculation. The calculation is derived from a excel formula that calculates the second tuesday of the month when the user...
30
by: Barry L. Bond | last post by:
Greetings! I just got a new Peet Brothers Ultimeter 2100 Weather Station. This new one has a way to display the heat index, if you press the "dew point" key twice. Being aware of all the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.