# Do I need a win streak?

You have played $$\N\$$ matches in some game where each match can only result in one of the two outcomes: win or loss. Currently, you have $$\W\$$ wins. You want to have a win percentage of $$\P\$$ or more, playing as few matches as possible. Output the minimum win streak that you need. Assume the current win streak is at $$\0\$$.

For example: If $$\N=10, W=2, P=50\$$, then you can win $$\6\$$ matches in a row, bringing your win percentage to $$\\frac{2+6}{10+6} = \frac{8}{16} = 50\%\$$. You cannot have a win percentage of $$\50\$$ or more earlier than this. So the answer for this case is $$\6\$$.

# Examples

W, N, P ->
2, 10, 50%      -> 6
3, 15, 50%      -> 9
35, 48, 0.75    -> 4
19, 21, 0.91    -> 2
9, 10, 50%      -> 0
0, 1, 1/100     -> 1
43, 281, 24/100 -> 33
0, 6, 52%       -> 7


# Rules

• $$\N\$$ and $$\W\$$ will be integers with $$\0 \le W < N\$$.
• The percentage $$\P\$$ will be an integer between $$\1\$$ and $$\100\$$ inclusive. You can also choose to take a decimal value between $$\0\$$ and $$\1\$$ inclusive instead, which will contain no more than $$\2\$$ decimal places, or take it as a fraction.
• You can take the inputs in any convenient format.
• Standard loopholes are forbidden.
• This is , so the shortest code in bytes wins.
• Sandbox Apr 25, 2021 at 7:30

# Python 3, 35 bytes

lambda w,n,p:-min(0,(w-p*n)//(1-p))


Try it online!

-4 bytes thanks to pajonk

Some simple math: if $$\X\$$ is the number of wins we need, then we have:

$$\\frac{W+X}{N+X}\geq P\$$

$$\W+X\geq PN+PX\$$

$$\X-PX\geq PN-W\$$

$$\X(1-P)\geq PN-W\$$

Since $$\P<1\$$, then $$\1-P>0\$$, so we can divide from both sides without division by zero or flipping the sign.

$$\X\geq\frac{PN-W}{1-P}\$$

• Since $P<1$ it would IMHO be clearer to write $X\geq\frac{PN-W}{1-P}$.
– Neil
Apr 25, 2021 at 8:36
• @Neil Ah, that's true. I think this is a bit clearer now. Thanks :) Apr 25, 2021 at 8:40
• Shouldn't this work? Try it online! Apr 25, 2021 at 19:31
• @pajonk It does seem to. Well spotted; thanks! Apr 25, 2021 at 19:35

# 05AB1E, 1315 11 bytes

∞<.Δ¹+.«/²@


Try it online!

Why aren't I using Vyxal? Because I'm leaving the chance for others so they can claim the bounty.

+2 due to bug fix :(

but -4 thanks to @ovs

## Explained (old)

∞0š.ΔD²+s¹+s/³@
∞0š.Δ            # get the first integer n where:
D²+s¹+s/    #     (wins + n) / (matches + n)
³@  #     >= percentage

• Instead of prepending a 0 you can decrement every integer with <.
– ovs
Apr 25, 2021 at 8:19
• 11 bytes with a slightly different input format.
– ovs
Apr 25, 2021 at 8:22

# Jelly, 10 bytes

Ḣ_Ḣ×$÷’Ċ»0  Try it online! ## Explanation This uses the same formula as my Python answer: $$\X\geq\frac{W-PN}{P-1}\$$ Takes the input as a list of three numbers, $$\W,N,P\$$. Ḣ_Ḣ×$÷’Ċ»0  Main Link
Ḣ           W (pops from the list)
_          minus

Next, we see _, a dyad (subtraction). It is followed by a monad, namely the one formed by the $ grouping two links, thus matching the first pattern. The dyad-monad pattern, if we have some dyad + and some monad F, computes v + F(a) where v is the current value and a is the right argument. Thus, this computes W - F([N, P]). Here, F(a) computes Ḣ× as a monadic chain whose argument is a = [N, P]. The value starts at v = [N, P] as well. The first link is a monad and thus the value becomes N. Also, since "pop" mutates, the argument (to both the sub-link and the main link) is now [P]. Then, the second link is a dyad and nothing else follows, so the value becomes N × [P] which is [NP]. This is a singleton list, not a value, but that's fine because everything else vectorizes. Now, the value is W - [NP] which vectorizes to [W - NP], and the argumnt is [P] since it was mutated within the sub-link. ÷’ is a dyad-monad chain, which computes [W - NP] ÷ P÷’. ’ decrements, so this computes [W - NP] ÷ (P - 1), which vectorizes to [(W - NP) / (P - 1)]. Penultimately, Ċ is a monad meaning "ceiling", which just rounds all of these values up. Finally, »0 is a dyad-nilad chain which takes the maximum of this value and 0, thus preventing negative outputs. Here's a shitty hand-drawn visualization of how chaining works out here: • And you're sure that a "find first n where" approach wouldn't be shorter? Apr 25, 2021 at 8:43 • @Lyxal I can't say for sure that a solution of that form wouldn't be shorter, but I haven't found one yet. Apr 25, 2021 at 8:52 • @Lyxal it's shorter Apr 25, 2021 at 17:20 # R, 454342 38 bytes Edit: simultaneous discovery of -1 byte by Kirill L., and then -4 bytes thanks to pajonk function(W,N,P)-min((W-P*N)%/%(1-P),0)  Try it online! • @KirilL Ha! I was just editing this in while you commented. You get half the credit! Thanks anyway! Apr 25, 2021 at 9:08 • Shouldn't this work in principle? Try it online! Apr 25, 2021 at 19:27 • @pajonk - Er, rather embarassing that I didn't spot that. Thanks. Do you want to just post it & be the winner? I feel rather ashamed... Apr 25, 2021 at 19:45 • Nah, thanks - let's treat it the same as with the python answer :) Apr 26, 2021 at 6:08 # PowerShell, 44 bytes Every answer of Wasif inspires me to make the right one. Thanks. param($w,$n,$p)for(;$w++/$n++-lt$p){$k++}+$k  Try it online! # Clojure, 39 bytes #(max(Math/ceil(/(-(* %2%)%3)(- 1%)))0)  Try it online! Takes arguments in reverse order compared to the provided test cases ($$\P, N, W\$$). # Jelly, 9 bytes 0+÷/:⁴ʋ1#  Try it online! Full program that takes [w, n] on the left and P on the right. ## How it works 0+÷/:⁴ʋ1# - Main link. Takes [w, n] on the left and P on the right ʋ - Previous 4 links as a dyad f(x, [w, n]): + - Yield [w+x, n+x] ÷/ - Yield (w+x)÷(n+x) ⁴ - Yield P : - Floor divide (w+x)÷(n+x) by P, returning 0 if P < (w+x)÷(n+x), and 1 otherwise 0 1# - Count up x = 0, 1, 2, ..., and find the first x such that f(x, [w, n]) is 1  # PowerShell, 51 bytes param($w,$n,$p)while(($w+$k)/($n+$k)-lt$p){$k+=1}$k  Try it online! What? It beat my python answer? oO # Ruby, 34 bytes ->w,n,p{[0,-(w-p*n).div(1-p)].max}  Try it online! # Retina 0.8.2, 68 bytes \d+$*
\G1
100$* 1(?=1*,(1*)%)$1
+^(1*,)(1+\1(1+))
100$*1$1$3$2_
_


Try it online! Link includes test cases. Takes P as a percentage (including % sign). Explanation:

\d+
$*  Convert W, N and P to unary. \G1 100$*


Multiply W by 100.

1(?=1*,(1*)%)
$1  Multiply N by P. +^(1*,)(1+\1(1+))  While 100W<NP... 100$*1$1$3$2_  increment W and N, i.e. add 100 to 100W and P to NP. _  Count the number of increments made. # JavaScript, 4440 34 bytes f=(w,n,p)=>w/n>=p?0:f(w+1,n+1,p)+1  Takes all three parameters as integers except p which is a decimal. -4 bytes, thanks to @DominicvanEssen -6 bytes, thanks to @Arnauld Try it online • You can save 4 bytes by taking p as a fraction instead of a percentage... Apr 25, 2021 at 8:48 • @DominicvanEssen Oh yeah that's right – user100690 Apr 25, 2021 at 8:49 • 33 bytes, or 32 bytes if you don't mind returning false instead of 0. Apr 25, 2021 at 9:13 • 31 bytes by taking (p)(w,n). Apr 25, 2021 at 9:21 • @Arnauld I'll go with your 33 byte suggestion because I prefer its I/O formats. – user100690 Apr 25, 2021 at 10:18 # Haskell, 37 bytes (n,w)#p|100*w<p*n=1+(n+1,w+1)#p|1>0=0  Try it online! # Pari/GP, 31 bytes f(W,N,P)=-min(-(P*N-W)\(1-P),0)  Try it online! # Python 3, 55 bytes f=lambda w,n,p,c=0:(w+c)/(n+c)>=p and c or f(w,n,p,c+1)  Try it online! Simple recursion, I am beaten by few minutes.... # Charcoal, 24 bytes ＮθＮηＮζ≔⁰εＷ‹⁺θε×ζ⁺ηε≦⊕εＩε  Try it online! Link is to verbose version of code. Explanation: ＮθＮηＮζ  Input W, N and P. ≔⁰ε  Start with X=0. Ｗ‹⁺θε×ζ⁺ηε  Until we reach the desired win percentage... ≦⊕ε  ... increment X. Ｉε  Output X. A port of @hyper-neutrino's solutions is of course much shorter at 13 bytes: Ｉ⌈⟦⁰±÷⁻×ＮＮＮ⊖θ  Try it online! Link is to verbose version of code. Takes input in the order P, N, W. Explanation:  Ｎ P as a number × Multiplied by Ｎ N as a number ⁻ Subtract Ｎ W as a number ÷ Floor divided by ⊖θ P decremented ± Negated ⌈⟦⁰ Maximum of that and 0 Ｉ Cast to string Implicitly print  # Japt, 11 bytes @°*L¨V°*W}a  Try it # CSASM v2.4.0.1, 123 bytes A function which pops three values from the stack ([N, W, P]) and pushes the result as an f64. The function expects the types of all three values to be f64. .include <stdmath> func a: pop$3
pop $2 pop$1
push $3 dup push$2
swap
push $1 mul sub neg swap push 1.0 sub div neg push 0.0 call max ret end  Explanation + Full Program: .include <stdmath> func main: ; Get the three inputs in "N: " conv f64 in "W: " conv f64 in "P: " conv f64 call a ; Truncate the result and print it conv i32 push "Wins needed: " swap add print.n ret end func a: pop$3 ; P
pop $2 ; W pop$1 ; N

push $3 ; [ P ] dup ; [ P, P ] push$2
; [ P, P, W ]
swap
; [ P, W, P ]
push \$1
; [ P, W, P, N ]
mul
; [ P, W, P*N ]
sub
; [ P, W-P*N ]
neg
; [ P, P*N-W ]
swap
; [ P*N-W, P ]
push 1.0
; [ P*N-W, P, 1 ]
sub
; [ P*N-W, P-1 ]
div
; [ (P*N-W)/(P-1) ]
neg
; [ -(P*N-W)/(P-1) ]
push 0.0
call max

ret
end
$$$$


# Wolfram Language (Mathematica), 25 bytes

0//.x_/;x+#-##2<x#3:>x+1&


Try it online!

We can also directly compute the number of needed wins:

### Wolfram Language (Mathematica), 25 bytes

⌈Min[#-##2,0]/--+#3⌉&


Try it online!

# Japt, 9 bytes

Takes P as a decimal.

@°/V°<W}f


Try it

# JavaScript, 28 bytes

Takes P as a decimal and outputs false for 0.

w=>n=>g=p=>w++/n++<p&&g(p)+1


Try it online!

# Vyxal 2.4.1, 8 bytes

λ¹+ɖ⁰≥;ṅ


Try it Online! Here's what I managed to come up with. It doesn't seem like anyone else is going to give this a go using Vyxal, so here's my solution.

## Explained

λ¹+ɖ⁰≥;ṅ
λ        # start a lambda that, given a single argument "n":
¹+      #     adds n to the list [wins, matches]
ɖ     #     reduces that list by division
⁰≥;  #    and returns whether that number is greater than the percentage
ṅ # take that lambda, and find the first integer (starting at 0) where it evaluates as truthy
`