Home Segments Index Top Previous Next

316: Mainline

Next, note that you can arrange for recursivePowerOfTwo to hand over a little less work to powerOfTwo by performing one of the multiplications by 2 in recursivePowerOfTwo itself, and subtracting 1 from powerOfTwo's argument:

Integer method definition • instance 
recursivePowerOfTwo 
  self = 0 
    ifTrue: [^ 1] 
    ifFalse: [^ 2 * (self - 1) powerOfTwo] 
Workspace
Transcript show: 4 recursivePowerOfTwo printString; cr  
Transcript 
16 

Clearly, recursivePowerOfTwo must work as long as one of the following two situations holds: