go to previous page   go to home page   go to next page hear noise highlighting

Answer:

No. The test for the base case is wrong.


Two Base Cases

Sometimes there are several base cases. Be sure to test for each one.

Here is another math-like definition for zero or positive integers N (don't worry about what it defines):

Thing( 0 ) = 0
Thing( 1 ) = 1
Thing( N ) = 2*N + Thing( N/2 -1 )

Assume that N/2 means integer division of N by 2.


QUESTION 12:

Is the following a correct translation into Java?

public int Thing( int N )
{
  if ( N == 0 )
    return 0;
  else if ( N == 1 )
    return 1;
  else
    return 2*N + Thing( N/2 - 1 );
}

go to previous page   go to home page   go to next page