3
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
2Can you give a few more input/output examples? I suspect that this is a very simple equation. – L3viathan – 2017-06-01T09:47:44.263
Related – Adnan – 2017-06-01T10:00:46.433
Now I'm picturing a revolver with a small motor attached to the cylinder to keep it spinning. – Erik – 2017-06-01T10:30:00.070
Aside from being a trivial variation on a question which was posted three days ago, this a) is underspecified, making no mention of the required precision; b) gives an incorrect test case:
f(4) = 25/3
exactly, which under no circumstances rounds to8.330
. – Peter Taylor – 2017-06-01T10:39:27.627