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 8 3239
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.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
Please, give me an example.
GB
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** @msnews.microso ft.com...
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.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
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.co mwrote in message news:MP******* *************** *@msnews.micros oft.com...
>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
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;
}
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());
}
}
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;
}
}
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
Thank you , Rick.
It works perfectly for me!
GB
2
"Rick Lones" <Wr******@Ychar terZ.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 parts.
If first finds a natural logarithm, then converts it to a percentile.
Even if I had to do it in two steps on the spreadsheet, I can't
determine the calculations.
***************************************************
Logarithm System
The...
|
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)
|
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 As Integer
|
by: Mike |
last post by:
Hi,
I have three tables in the following structure (simplified):
Table 1: Containing the customers
-------------------------------------------------
create table Customers
(
int identity(1, 1) not null,
varchar(25) not null
|
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 to divide the total by
11 to get the gst amount
This is great until the GST % changes one day
| |
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
|
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 Excel is currently too long, complicated and slow
•I am currently using the IRR function on a series of cashflows. eg =IRR(cashflow,0.01)
•I am trying to create a formula called NetIRR(cashflow, fee, hurdle, compound) which will ultimately perform...
|
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 enters the month
(numeric) and the year. Where Year and Month are variables from a dropdown
box.
=DATE(Year,Month,1+((2-(2>=WEEKDAY(DATE(Year,Month,1),2)))*7+(2-WEEKDAY(DATE(Year,Month,1),2))))
What is the best way to execute this formula in...
|
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 controversy surrounding the exact calculation
of the heat index, I would like my own software (which I programmed in the
1990's, when I got their Ultimeter 2000 weather station, but that one
didn't show heat index) to display it. Living in Florida,...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |