
#include	<stdlib.h>
#include	<stdio.h>
#include	<math.h>

#define	EPSILON	0.00000000001

int main(int argc, char **argv)
{
	long double	winp, resp, bank, pwin,
			formvig, kk, formvig2, mm;
	double	Goal, Payoff, Vigish;
	int	i;

	if (argc != 4) { 
		fprintf(stderr, "Usage: roulette #Goal #Pay #Vig\n");
		exit(1);
	}
	Goal = atof(argv[1]);
	Payoff = atof(argv[2]);
	Vigish = atof(argv[3]);
	winp = 0;
	resp = 1;
	bank = 1;
	pwin = (1 - Vigish) / Payoff;

	printf("Computing the chance of multiplying bank by %6.2f; given a primitive bet\n\twith Payoff = %6.2f -for-1 and Vigorish = %8.6f\n",
		Goal, Payoff, Vigish);

	for (i = 0; resp > EPSILON; i++) {
		if (bank * Payoff < Goal) {
			resp *= pwin;
			bank *= Payoff; 
		} else {
			winp += resp * pwin;
			resp *= 1 - pwin;
			bank -= (Goal - bank) / (Payoff - 1);
		}
	}
	kk = logl(Goal) / logl(Payoff);
	formvig = 1 - expl(kk * logl(1 - Vigish));
	mm = logl(Goal / (Goal - 1));
	mm /= logl(Payoff / (Payoff - 1));
	formvig2 = 1 - Goal + Goal * powl((Payoff + Vigish - 1) / Payoff, mm);
	printf(" Win chance = %13.9Lf Netvig = %9.5Lf\n",
			winp,
			1 - winp * Goal);
	printf(" Vig estim. ('Bet everything') = %10.6Lf (need =%10.5Lf)\n",
		formvig, kk);
	printf(" Vig estim. ('Bet goal-reach') = %10.6Lf (opps =%10.5Lf)\n",
			formvig2, mm);
	exit(0);
}
