The most unreliable gun ever [duplicate]

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

• Can you give a few more input/output examples? I suspect that this is a very simple equation. Jun 1, 2017 at 9:47
• Related Jun 1, 2017 at 10:00
• Now I'm picturing a revolver with a small motor attached to the cylinder to keep it spinning.
– Erik
Jun 1, 2017 at 10:30
• 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 to 8.330. Jun 1, 2017 at 10:39

05AB1E, 3 bytes

L/O


Try it online! or as a Test suite

Explanation

L     # range [1 ... n]
/    # divide n by each in above list
O   # sum

• Explanation is needed Jun 1, 2017 at 9:53
• @sagiksp: Explanation provided :) Jun 1, 2017 at 9:57
• This is pure magic, I'm moving to 05AB1E now, nothing can stop me. Jun 1, 2017 at 9:58

f n=sum$(n/)<$>[1..n]


Try it online (TIO)!

Previous, more self-explanatory version:

f n=sum[n/i|i<-[1..n]]


Ohm, 4 bytes

D@/Σ


Try it online!

Japt, 6 4+1= 5 bytes

Inspired by Emigna's 05AB1E solution.

+1 byte for the -x flag.

õ@/X


Try it online

• Are flags worth 1 byte again? Jan 15, 2018 at 17:25
• No, @Oliver, this was posted before they cost 2 bytes (or before I knew they did, at least). Jan 15, 2018 at 17:40
• Oh, sorry. I should have looked at the date. Jan 15, 2018 at 18:28

PHP, 37 Bytes

for(;$i++<$argn;)$s+=$argn/$i;echo$s;


Try it online!

Aceto, 41 34 bytes

Based on Emigna's insight

Could almost definitely be golfed more.

  9jpX
{:s_=1(
&_L+&l@
iM!l
rLz@


Rough explanation

Part 1: Making a range and dividing by each of the numbers:

 {:s
&_L
iM!l
rLz@


Part 2: Summing them up

    _=1(
+&l@


Part 3: Printing, exiting, and making sure we don't visit this earlier

  9jpX


CJam, 11 bytes

rid_,:)f/:+


Try it online!

jq, 30 characters

. as$n|[range(1;.+1)|$n/.]|add


Sample run:

bash-4.4$jq '. as$n|[range(1;.+1)|\$n/.]|add' <<< 10
29.289682539682538


Try in jq‣play

JavaScript (ES6), 443427 26 bytes

Also inspired by Emigna's 05AB1E solution.

f=(n,i=n)=>i&&n/i+f(n,--i)


(Look at me , going for recursion over array mapping for once!)

Try it

f=(n,i=n)=>i&&n/i+f(n,--i)
oninput=_=>o.innerText=f(+i.value)
o.innerText=f(i.value=1)
<input id=i type=number><pre id=o>