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

 

bono@usc.edu recursionLab > fastExpon
prev  |  next  |  chance

Using recursion (no loops) implement the method fastExpon which raises x to the nth power more efficiently than the version discussed in lecture. It will take advantage of the fact that

x^(n) = (x^(n/2))^2
to greatly reduce the number of of recursive calls and multiplications that will need to be done to compute the answer. (We're using ^ to indicate exponentiation in the above formula.) You may assume x and n will both be non-negative.

HINT: for this to be fast, in any one activation of the method, only one recursive call should be made.  There may be multiple recursive calls in the code, but only one will get executed in any one activation.

fastExpon(3, 4) → 81
fastExpon(2, 3) → 8
fastExpon(2, 12) → 4096

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

public int fastExpon(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: 340

Copyright Nick Parlante 2017 - privacy