python - How many times does my code multiply the number: -
i given code (which integer exponentiation) , i'm supposed find out how many times code loops on while using (n1) , how many times if true using (n2) , how many multiplications done using (n3), think placed n1 , n2 correctly don't know should place n3 or how should count it.
def power(a,b): n1=0 n2=0 n3=1 result = 1 while b>0: n1+=1 if b%2 == 1: # b odd result = result*a n2+=1 = a*a # squares power of b = b//2 # removes least significant bit of b n3+=1 return result,n1,n2,n3
since inside if
statements multiplication done:
result = result *
you should increment n3
there:
def power(a, b): n1 = 0 n2 = 0 n3 = 1 result = 1 while b > 0: n1 += 1 # loop if b % 2 == 1: result = result * n2 += 1 # if n3 += 1 # multiplication = * b = b // 2 n3 += 1 # multiplication return result, n1, n2, n3
Comments
Post a Comment