18/11/11 19:28:27.77 RT24eksu.net
/*
C:\pleiades\workspace>coin5 100000 15
Over 15 heads at 100000 flips : 0.782609
Just 15 heads at 100000 flips : 0.248904
*/
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#define p 0.5
double flip(long N,double k){
double P[N];
long i;
for(i = 0; i < k-1; i++) {
P[i] = 0;
}
P[(int)k-1] = pow(p,k);
P[(int)k] = pow(p,k) + (1-p)*pow(p,k);
for(i = (int)k; i < N; i++){
P[i+1] = P[i] + (1-P[i-(int)k])*pow(p,(double)(k+1));
}
return P[N];
}
int main(int argc,char *argv[]){
long N = atol(argv[1]);
double k = atof(argv[2]);
double PN =flip(N,k);
double PNJ = flip(N,k) - flip(N,k+1);
printf("Over %d heads at %d flips : %f\n", (int)k ,N ,PN);
printf("Just %d heads at %d flips : %f\n", (int)k ,N ,PNJ);
return 0;
}