Editor

Raise to a Power

#32 · Recursion and backtracking · Beginner · 30 XP · about 6 min

Write power(base, exp) that returns base raised to the power exp, without using ** or pow(). Both are whole numbers, and exp is 0 or more.

Solve it with recursion: a function that answers a question by calling itself on a smaller version of the same question.

Examples

power(2, 10)  # returns 1024
power(5, 0)   # returns 1
power(-3, 3)  # returns -27

Note

Any number to the power 0 is 1, and that includes power(0, 0), as in Python. exp is at most 100.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.