Answers for "How would you optimally calculate p^k, where k is a non-negative integer? What is the complexity of the solution?"

0

How would you optimally calculate p^k, where k is a non-negative integer? What is the complexity of the solution?

FUNCTION pow(base, exponent)
	IF exponent == 0
		RETURN 1
	ELSE IF exponent is even
		RETURN pow(base * base, exponent / 2)
	ELSE
		RETURN base * pow(base * base, (exponent - 1) / 2)
	END IF
Posted by: Guest on September-13-2020

Code answers related to "How would you optimally calculate p^k, where k is a non-negative integer? What is the complexity of the solution?"

Browse Popular Code Answers by Language