Factorials for 1-10, but not all together. ??

So I have a formula, P(x)=((y^x)*(e^-y))/x!
e is a constant = 2.71828
for each input I need to calculate the probability(p) of 1 - 10 customers(x) coming into the bank. I need to calculate x every time, so like the probability of 1 customer, then 2, then 3, up to 10.
This is what I have.
1
2
3
4
5
  for(int i =1;i<=10;i++)
  {
    factorial *= i;
    prob = (pow(avg_cust,i)*pow(eulers_num,(avg_cust*-1)))/factorial;
    cout << "The prob. of " << i <<" customers arriving is " << prob << endl;

When it outputs, it gives me a separate line for 1-10, but I can't seem to get x to stay with the factorial. When I calculate the factorial I'm just getting 10!*9!*8!*7!.....
P(x) = yx * e-y / x!
Let e-y = k

P(x) = k * yx / x!

P(x+1) =  k * yx+1 / (x+1)!
       =  k * yx * y / ( x! * (x+1) )
       =  ( k * yx /  x! ) * ( y / (x+1) )
       =  P(x) * y / (x+1)

P(0) = k 
P(x) = P(x-1) * y / x


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
#include <iomanip>

void poisson_probabilities( double avg_cust )
{
    const double e = 2.71828182845904523536 ;
    const double k = std::pow( e, -avg_cust ) ;

    int x = 0 ;
    double probability = k ; // P(0)
    do
    {
        std::cout << std::setw(2) << x << ". " << probability << '\n' ;
        ++x ;
        probability *= avg_cust / x ; // P(x) = P(x-1) * y / x
    }
    while( x < 11 ) ;
}

int main()
{
    poisson_probabilities(2.5) ;
}

http://coliru.stacked-crooked.com/a/a4270aa4524d4d5f
factorial looks ok to me.
assuming factorial = 1 to start?
it should give 1,2,6, ... which it does. (That does not mean you needed it, as stated, but I don't see anything wrong with your computation).

did you know that e is just the sum of 1/x! for x = 1-infinity?
Last edited on
jonnin wrote:
did you know that e is just the sum of 1/x! for x = 1-infinity?


Actually, it's the sum of 1/x! from x = 0 (zero) to infinity. (With 0!=1)
true. Good catch, had starting at 1 on my mind from checking the code.
Topic archived. No new replies allowed.