## Dutching Calculator

Many years ago, long before the advent of Smartphones, I wanted a dutching calculator. This is a betting tool that allows you to work out how much to bet on several different horses in the same race such that it makes no difference which of them wins – you will still win the same amount of money. I mention horses here just for the sake of argument. The tool is equally applicable to working out odds for betting on anything you can think of. So wherever I have written “horse” in this article just think “contestant”.

The tool I have written allows you to specify how much you have in total to bet (first argument) and then the odds of the horses you wish to bet on in subsequent arguments. You enter the odds in exactly the same way as the bookies present them – so you can specify 9/4 or 100/30 for example.

When you run the tool it works out the amounts you should bet. What you are looking for is positive values for the profit figures quoted – if these are positive you are in business.

Obviously the utopian situation is if you find it is possible to bet on every single horse in the race and you will still win whichever horse comes in first. You are very unlikely to find such a scenario I am afraid – the bookies would very soon go out of business if they allowed this sort of thing. However, you may just find a situation like this once in a blue moon, by spreading your bets across several different bookies which are offering different odds.

The tools works out the profit you will make, and also works out the profit you will make if there is any tax to be paid. Tax on betting is being phased out in the UK, but in other parts of the world you may find you have to pay tax on your winnings. There are usually two possibilities – you can either pay tax up front on the stake you place, or you can pay tax on the winning amount. The tool will calculate the amount of tax you pay both ways, so you can choose which to do. The Tax percentage is hardwired into the tool (at 10%) so you could either change this to the required tax level or change the program so the tax percentage is one of the arguments.

## Program code

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

#define BET_TAX     0.1                                    /* 10% */

static char buffer [100] = { (char) NULL } ;

double getOdds (char *str)
{
double value = 0.0 ;

strcpy (buffer, str) ;

char *rest = NULL ;

char *ptr = NULL ;
char *ptr2 = NULL ;

ptr = (char *) strtok_r (buffer, ":/", &rest) ;
if (rest && strlen (rest))
{
ptr2 = (char *) strtok_r (NULL, ":/", &rest) ;
double val1 = (double) atoi(ptr) ;
double val2 = (double) atoi(ptr2) ;
value = val1 / val2 ;
}
else
{
value = (double) atoi (str) ;
}

return (value) ;
}

main(argc, argv)
int             argc;
char          **argv;
{
int             loop;
double          total, totsum, profit;
double          odds[100];
double          bet[100];
double                  totalBet = 10.0 ;

printf("\n\n");

/* read in the odds, and work out the value of all the odds + 1 multiplied
together */

if (argc < 3)
{
printf ("Usage: win <totalToBet> <odds1> <odds2> <odds3> ... \n") ;
exit (0) ;
}

totalBet = (double) atoi(argv[1]);
for (loop = 2, total = 1.0; loop < argc; loop++)
{
odds[loop] = getOdds(argv[loop]);
total *= (odds[loop] + 1.0);
}

/* calculate each bet and add up the total bet */

for (loop = 2, totsum = 0; loop < argc; loop++)
{
bet[loop] = total / (odds[loop] + 1.0);
totsum += bet[loop];
}

/* reduce total bet to totalBet */

totsum /= totalBet;

for (loop = 2; loop < argc; loop++)
{
bet[loop] = bet[loop] / totsum;
printf("Bet on %d, amount %f \n", loop-1, bet[loop]);
}

/* calculate profit by taking horse 1 wins as an example */

profit = bet[2] * odds[2] - totalBet + bet[2] ;

printf("\n\nProfit       %f\n", profit);
printf("Profit Post  %f\n", profit * (1.0 - BET_TAX));
printf("Profit Pre   %f\n", profit - (BET_TAX * totalBet));
}```

## Example Run

Notice in this example the third parameter is passed as value 3. You could equally well enter 3/1 here. The first argument (total amount to bet) should be an integer value.

```% ./win 100 9/4 3 100/30

Bet on 1, amount 39.024390
Bet on 2, amount 31.707317
Bet on 3, amount 29.268293

Profit       26.829268
Profit Post  24.146341
Profit Pre   16.829268

[1]    26707 exit 23    ./win 100 9/4 3 100/30```

Here is an example where there is no solution that will win you money.

```% ./win 100 7/4 2/1 5/2 5/2

Bet on 1, amount 28.668942
Bet on 2, amount 26.279863
Bet on 3, amount 22.525597
Bet on 4, amount 22.525597

Profit       -21.160410
Profit Post  -19.044369
Profit Pre   -31.160410

[1]    26710 exit 24    ./win 100 7/4 2/1 5/2 5/2```