id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

fastPower


Define a recursive function that when passed two integers (say x and n), returns x to the power of n. You should think of how to make it faster to avoid time-outs and/or StackOverflowErrors. You may not use any loops. For brownie points, write a theoretically tail-optimized version (since Java doesn't really facilitate tail-optimization... yet). Use the hint only if you have spent sufficient time thinking and are still stuck.


fastPower(1, 10000) → 1
fastPower(0, 200000) → 0
fastPower(2, 30) → 1073741824

...Save, Compile, Run (ctrl-enter)

int fastPower(int x, int n) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 3 Post-solution available

Copyright Nick Parlante 2017 - privacy