Python 1 liner..finding root of quadratic equation made from the sum of arithmetic progression...


  • 0
    D

    sum = n/2(2a+(n-1)d)
    here sum = given number ...
    n = number of terms/ here # of stairs
    a = first term = 1
    d= difference = 1
    make an equation and solve for n^2 + n -2*n = 0
    root = (-b + sqrt(4ac))/2...
    constant for us a = 1, b =1 so just solve it convert it to int

    return int((math.sqrt(1+8*n)-1)/2)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.