Equation

Pages: 12
That's exactly your problem.
If not, why did you post incorrect code if you already knew what you had to do to fix it?

didn't work

is not a valid problem description.
Last edited on
I can fix some of the code but not the code all together, I can't make it give me the correct result

I have also tried this code: (((double)23*x0/((double)1*x0)*(double)(23-1)*x0))*(double)pow((double)1/144,1)*(double)pow((double)1-1/144,23-1)

Why do you keep multiplicating random values with the faculty of 23?
Just assign the right values to some variables, replace ! with fac (you need to create that function yourself) and ^ with pow. There's your C++ code.
Last edited on
n choose x


   n!                n(n-1)(n-2)...(n-x+1)         n      n-1      n-2             n-x+1
--------    =    ----------------------------  =   --- * ------ * ------ * ... * --------
x!(n-x)!              x(x-1)(x-2)... 1             x      x-1      x-2               1


accumulate the product:

1
2
3
double n_C_x = 1.0;
for(i=0;i<x;++i)
   n_C_x *= double(n-i)/(x-i);


this means you don't have to calculate massive upper and lower numbers which might overflow

what about q^(n-x) here you might get underflow.
The product accumulating in this loop gets bigger and bigger, so maybe if you sometimes multiply the accumulating product by q (so as to get the right number of q's then you can avoid the underflow.

In Excel are you using some inbuilt function like BINOMIAL or something?





n! is about 2.6*10²² (or 5.5*10⁵⁷ for n=46) and q^(n-x) is ~0.858, so there should be no danger of overflow or underflow with these numbers.
Last edited on
In excel I just wrote the formula and it worked

=(23*fac(23))/((1*fac(23))*(23-1)*fac(23))*(1/144)^1*(1-1/144)^(23-1)

but it is Binomial dispersion

The result I wan't to get is 2.41E-25
Well, if you believe that formula makes sense (it doesn't), then how about doing what Disch said?
But if you want the real result, you'd better stick to the original formula you posted, with n=46 and p=1.0/12.
Last edited on
The formula you have there doesn't make any sense to me, for a start in the initial fraction you have 23! in both numerator and denominator (twice)
I'll ask my teacher in the morning and I will get back to you... I might have misunderstood something..
For the record, the actual result you should be getting is 0.0764.
So I had misunderstood a thing or two, but the formula in my second post is still correct. My definition of the symbol ! was wrong.

n! is the faculty(23)

x! is the faculty(1)

(n-x)! is the faculty(22)

n is a constant and x is a variable defined by the player of the game.

How do I take the faculty of something in c++?

btw thanks for all your help guys..
n! is faculty(23) if n = 23, of course.

Anyway, the faculty function has already been explained above, by mik2718.
I am not sure I understand how to use the function explained by mik2718.

I am relatively new at c++.
n! is equal to n*(n-1)*(n-2)*...*1.

Thus, n! can be calculated by taking 1, and multiplying it by i, for each i in [2,n]. This can be done with a single loop.
I tried using the loop and it just prints out 23, I do not understand it.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
double n = 23, i = 1, x = 1;
double n_C_x = 1.0;
for(i=0;i<x;++i)
   n_C_x *= double(n-i)/(x-i);
   
   cout << n_C_x << endl;
system ("pause");

return 0;
}
That's not the faculty, but it is the solution to Comb(23, 1). Just look at the structure of the faculty calculation: 1*2*3*....*n. That's a perfect candidate for a for loop.

(Do note: I think overflows will happen around 15!. Hence why mik2718 gave the Comb() function rather than the faculty).
As Athar has said there is actually plenty of headroom for calculating this particular problem using factorials.

double precision goes up to ~1.7x10^308

I make it that you can calculate factorials up to 170! ~ 7.25742e+306

Using the method I've shown you can do better, for instance if you wanted to do 200 choose 1, then using factorials you would come unstuck.

Which made me realize that the function could be improved upon since 200 choose 1 is the same as 200 choose 199, so by choosing the smallest of x or n-x for the number of iterations you could speed up the calculation.

@peter, I think you have put a wrong formula into Excel and got a wrong answer, which could not be replicated in C++ which does not have an in-built factorial function. Probably best to go back to the formula and get the maths right.
Topic archived. No new replies allowed.
Pages: 12