The most unreliable gun ever

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

Philippe

Posted 2017-06-01T09:42:05.833

Reputation: 437

Question was closed 2017-06-01T10:31:43.240

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 to 8.330. – Peter Taylor – 2017-06-01T10:39:27.627

Answers

5

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

Emigna

Posted 2017-06-01T09:42:05.833

Reputation: 50 798

Explanation is needed – sagiksp – 2017-06-01T09:53:12.087

@sagiksp: Explanation provided :) – Emigna – 2017-06-01T09:57:16.810

2This is pure magic, I'm moving to 05AB1E now, nothing can stop me. – sagiksp – 2017-06-01T09:58:43.760

1

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]]

Augmented Fourth

Posted 2017-06-01T09:42:05.833

Reputation: 71

1

Japt, 6 4+1= 5 bytes

Inspired by Emigna's 05AB1E solution.

+1 byte for the -x flag.

õ@/X

Try it online

Shaggy

Posted 2017-06-01T09:42:05.833

Reputation: 24 623

Are flags worth 1 byte again? – Oliver – 2018-01-15T17:25:58.820

No, @Oliver, this was posted before they cost 2 bytes (or before I knew they did, at least). – Shaggy – 2018-01-15T17:40:53.970

Oh, sorry. I should have looked at the date. – Oliver – 2018-01-15T18:28:38.917

1

Ohm, 4 bytes

D@/Σ

Try it online!

Inspired by Emigna's answer

FrodCube

Posted 2017-06-01T09:42:05.833

Reputation: 539

0

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

L3viathan

Posted 2017-06-01T09:42:05.833

Reputation: 3 151

0

PHP, 37 Bytes

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

Try it online!

Jörg Hülsermann

Posted 2017-06-01T09:42:05.833

Reputation: 13 026

0

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>

Shaggy

Posted 2017-06-01T09:42:05.833

Reputation: 24 623

0

CJam, 11 bytes

rid_,:)f/:+

Try it online!

Inspired by Emigna's answer

FrodCube

Posted 2017-06-01T09:42:05.833

Reputation: 539

0

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

manatwork

Posted 2017-06-01T09:42:05.833

Reputation: 17 865