You are given a very special gun with a full magazine.
Let n
be the initial number of bullets in the magazine and i
the number of bullets left.
That gun is really unreliable, hence each time you shoot, you have a i/n
chance to successfully shoot. The fewer bullets you have left, the more tries it requires to shoot.
The goal is to find the average number of attempts to shoot before running out of ammo.
Example
You start with 3 bullets (n=3
). Your first shot is always successful. You have now 2 bullets left. You will shoot first with a probability of 2/3
and misfire with 1/3
. The probability of emptying your magazine in just 3 tries (no misfires) is (3/3) * (2/3) * (1/3)
.
The average number of tries before emptying your magazine for this example is 5.5
.
Test Cases
f(2) = 3.000
f(3) = 5.500
f(4) = 8.330
f(10) ~= 29.290
f(4) = 25/3
exactly, which under no circumstances rounds to8.330
. \$\endgroup\$