Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2014-02-01 03:13:28

gAr
Member
Registered: 2011-01-09
Posts: 3,479

### Program to calculate expectation

A general solution to:
http://www.mathisfunforum.com/viewtopic.php?id=13145
using the formula in #2027 from http://www.mathisfunforum.com/viewtopic … 30#p299130

``````#include <stdio.h>

unsigned long bin[53][53];

void buildbin(){
long i,j;
for(i=0;i<53;i++){
for(j=0;j<53;j++){
if (j==0 || j==i)
bin[i][j]=1;
else if(j>i)
bin[i][j]=0;
else
bin[i][j]=bin[i-1][j]+bin[i-1][j-1];
}
}
}

int main(){
unsigned long sum=0;
unsigned long n[]={0,4,4,4,4,4,4,4,4,4,16}, i, j, k, h, m, sel=8;  // sel: no. of cards selected
buildbin();
for(i=1;i<=10;i++)
for(j=i;j<=10;j++)
for(k=j;k<=10;k++) {
h=0;
if (i==j && i==k){
for(m=1;m<=i;m++)
h += n[m];
h=52-h;
for(m=3;m<=sel;m++)
sum += (i+j+k)*bin[n[i]][m]*bin[52-n[i]-h][sel-m];
}
else if (j!=k && i!=j){
for(m=1;m<=i;m++)
h += n[m];
h=52-h;
for(m=1;m<=n[i];m++)
sum += (i+j+k)*bin[n[k]][1]*bin[n[j]][1]*bin[n[i]][m]*bin[52-n[i]-h][sel-2-m];
}
else if (j==k && i!=j){
for(m=1;m<=i;m++)
h += n[m];
h=52-h;
for(m=1;m<=n[i];m++)
sum += (i+j+k)*bin[n[j]][2]*bin[n[i]][m]*bin[52-n[i]-h][sel-2-m];
}
else{
for(m=1;m<=i;m++)
h += n[m];
h=52-h;
for(m=2;m<=n[i];m++)
sum += (i+j+k)*bin[n[k]][1]*bin[n[i]][m]*bin[52-n[i]-h][sel-1-m];
}
}
printf("%.15f\n",sum/(double)bin[52][sel]);
return 0;
}``````

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense"  - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

## #2 2014-02-01 05:00:49

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

### Re: Program to calculate expectation

Hi gAr;

Thanks for including that and for the whole amazing method of solving the card problem. Wunderbar!

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

## #3 2014-02-01 05:25:45

gAr
Member
Registered: 2011-01-09
Posts: 3,479

### Re: Program to calculate expectation

Hi bobbym,

You're welcome!
It's one of the common methods in probability, I don't think there's something special in what I attempted.

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense"  - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

## #4 2014-02-01 06:00:52

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

### Re: Program to calculate expectation

Hi gAr;

Sometimes using the simplest techniques to do a problem is best. I gave up on the approach until John on another site convinced me it was tedious but doable. You eliminated the tedious part and that was good work.

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

## #5 2014-02-03 00:28:10

gAr
Member
Registered: 2011-01-09
Posts: 3,479

### Re: Program to calculate expectation

Hi,

I have improved the code to eliminate the conditions for the formula.
But couldn't get rid of the loops though, sigh!

This is the code for expected sum of highest 5 numbers from 8 randomly selected numbers:

``````#include <stdio.h>

unsigned long bin[53][53];

void buildbin(){
long i,j;
for(i=0;i<53;i++){
for(j=0;j<53;j++){
if (j==0 || j==i)
bin[i][j]=1;
else if(j>i)
bin[i][j]=0;
else
bin[i][j]=bin[i-1][j]+bin[i-1][j-1];
}
}
}

void init(unsigned long *t){
int i;
for(i=1;i<=10;i++)
t[i]=0;
}

int main(){
unsigned long sum=0, prod=1,x,t[11], cnt=0;
unsigned long n[]={0,4,4,4,4,4,4,4,4,4,16}, i, j, k, l, h, m, o, sel=8;
buildbin();
for(i=1;i<=10;i++)
for(j=i;j<=10;j++)
for(k=j;k<=10;k++)
for(l=k;l<=10;l++)
for(o=l;o<=10;o++)
{
h=0;
cnt=0;
prod=1;
init(t);
for(m=1;m<=i;m++)
h += n[m];
h=52-h;
t[i]++;  // t[x] means the no. of times x appears in top 5
t[j]++;
t[k]++;
t[l]++;
t[o]++;
for(x=i+1;x<=10;x++){
cnt += t[x];
if (t[x] != 0)
prod *= bin[n[x]][t[x]];
}
for(m=t[i];m<=n[i];m++)
sum += (i+j+k+l+o)*prod*bin[n[i]][m]*bin[52-n[i]-h][sel-cnt-m];
}
printf("%.15f\n",sum/(double)bin[52][sel]);
return 0;
}``````

which is close to the simulation.

Last edited by gAr (2014-02-03 00:29:05)

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense"  - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

## #6 2014-02-03 06:46:15

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

### Re: Program to calculate expectation

Hi gAr;

Looks like you only have one more challenge with these type problems. n highest cards in m picked.

In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline