3
\$\begingroup\$

This question already has an answer here:

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
\$\endgroup\$

marked as duplicate by Dada, Peter Taylor code-golf Jun 1 '17 at 10:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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
\$\endgroup\$
  • \$\begingroup\$ Explanation is needed \$\endgroup\$ – sagiksp Jun 1 '17 at 9:53
  • \$\begingroup\$ @sagiksp: Explanation provided :) \$\endgroup\$ – Emigna Jun 1 '17 at 9:57
  • 2
    \$\begingroup\$ This is pure magic, I'm moving to 05AB1E now, nothing can stop me. \$\endgroup\$ – sagiksp Jun 1 '17 at 9:58
1
\$\begingroup\$

Haskell, 22 21 bytes

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

Try it online (TIO)!


Previous, more self-explanatory version:

f n=sum[n/i|i<-[1..n]]
\$\endgroup\$
1
\$\begingroup\$

Ohm, 4 bytes

D@/Σ

Try it online!

Inspired by Emigna's answer

\$\endgroup\$
1
\$\begingroup\$

Japt, 6 4+1= 5 bytes

Inspired by Emigna's 05AB1E solution.

+1 byte for the -x flag.

õ@/X

Try it online

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

PHP, 37 Bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

CJam, 11 bytes

rid_,:)f/:+

Try it online!

Inspired by Emigna's answer

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 44 34 27 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>

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.