Question

X266: Recursion Programming Exercise: log For function log, write the missing base case condition and the recursive call This
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: The code is implemented in Java language.

The required method is as follows:

    // Define the method log().

    public int log(int b, int n)

    {

        // Return 0 if the value of n is equal to 1.

        if (n == 1)

        {

            return 0;

        }

        // Else, call the method recursively with

        // the division of n by b.

        // Add 1 to this result and return the

        // computed value.

        else

        {

            return 1 + log(b, n/b);

        }

    }

The complete program to test the above method for different test cases is as follows:

Program screenshots:

// Define the class Main. class Main { // Define the method log(). public int log(int b, int n) { // Return 0 if the value of

Sample output:

log (2, 4) -> 2 log(10, 100) -> 2 log (3, 27) -> 3 log(10, 100000) -> 5 log (2, 64) -> 6

Code to copy:

// Define the class Main.

class Main

{

    // Define the method log().

    public int log(int b, int n)

    {

        // Return 0 if the value of n is equal to 1.

        if (n == 1)

        {

            return 0;

        }

        // Else, call the method recursively with

        // the division of n by b.

        // Add 1 to this result and return the

        // computed value.

        else

        {

            return 1 + log(b, n/b);

        }

    }

    // Define the main() method.

    public static void main(String[] args)

    {

        // Create an object of the class Main.

        Main main = new Main();

        // Call the method and display the result.

        System.out.println("log(2, 4) -> " + main.log(2, 4));

        System.out.println("log(10, 100) -> " + main.log(10, 100));

        System.out.println("log(3, 27) -> " + main.log(3, 27));

        System.out.println("log(10, 100000) -> " + main.log(10, 100000));

        System.out.println("log(2, 64) -> " + main.log(2, 64));

    }

}

Add a comment
Know the answer?
Add Answer to:
X266: Recursion Programming Exercise: log For function log, write the missing base case condition and the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Recursion is ususally where a function calls itself; (another example of recursion is where function...

    1. Recursion is ususally where a function calls itself; (another example of recursion is where function A calls function B, and B calls C and C calls A, creating a loop in the calls). Some problems are most naturally solved recursively, such as writing the factorial function, or finding all the perumutations of a sequence, or checking if a string is a palindrome. Since those examples were done in class, here we will give you a toy example, which normally...

  • Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops...

    Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops. A synthetic linear recursive procedure for this problem is provided in Code Fragment 1. A recursive function such as the one described is intuitive and easily understandable. On calling myRecursion(input), the execution first checks for the stopping condition. If the stopping condition is not met, then operations inside the recursive call are performed. This can include operations on local variables. The operations inside the function...

  • C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...

    C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1)     Prod = M;                       //base case else     Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...

  • C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...

    C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • C++ Assignment 4 - For example 2, write a non recursive version of this function... using...

    C++ Assignment 4 - For example 2, write a non recursive version of this function... using a regular loop. Let's consider writing a function to find the factorial of an integer, N!. For example 7! equals 7*6*5*4*3*2*1. int myFactorial( int integer) if( integer == 1) return 1; else return (integer * (myFactorial (integer-1))); // action performed on call - pass into function "integer - 1" // action performed on return *

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT