from scipy.special import comb
import math

# System parameters
THRESH_HOLD = 0.0001 # gauranteed error rate
ACTIVE_PROB_INDV = 0.1 # probabiliby each user is active

# Function to calculate combination C(n, r)
def combination(n, r):
    if r > n:
        return 0
    return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))

# This method calculates the probability that k out of n users are active simultaneously
def active_rate(k, n): 
    #a_rate = combination(n, k) * pow(ACTIVE_PROB_INDV,k) * pow(1-ACTIVE_PROB_INDV,(n-k)) 
    # or below code is fine too 
    a_rate = comb(n, k) * pow(ACTIVE_PROB_INDV,k) * pow(1-ACTIVE_PROB_INDV,(n-k)) 
    return a_rate 

# This method calculates the failure probabiliy given n users in the system
def failure_rate(n):
    f_rate = 0
    # Write your code to calculate the failure rate given n users
    # for loop ... calls active_rate
    # HINT : When n = 11 
    # f_rate = active_rate(11, 11)
    # When n = 12
    # f_rate = active_rate(11, 12) + active_rate(12, 12)
    # When n = 13
    # f_rate = active_rate(11, 13) + active_rate(12, 13) + active_rate(13, 13)
    for k in range(11, n+1): # k = 11, 12, 13, .... n
        f_rate += active_rate(k, n)
    print(n, " ", f_rate)
    return f_rate

# main method (DO NOT CHANGE ANYTHING HERE)
def main():
    # N is the total number of users in the system
    N = 11 # initialize N as 11 (because we know there is no failure when N is 10 or below)

    # as long as the failure rate does not exceeds the threshold
    # keep increment N
    while (failure_rate(N) <= THRESH_HOLD) :
        N = N + 1

    # Current N would have just exceeded THRESH_HOLD
    print(N-1, " is the answer.") # reduce N back by 1


# python will execute from line 28 (DO NOT CHANGE ANYTHING HERE)
if __name__ == "__main__":
    main() # call main method

