Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of recursive calls

Tags:

java

recursion

How to change the algorithm to further show the amount of recursive calls?

public class fibb {

    static long fibonacci(long n){
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args){
        System.out.println(fibonacci(14));
    }
}
like image 650
Pit998 Avatar asked May 25 '13 08:05

Pit998


People also ask

How many recursive calls are made by Fibonacci?

fib() is a recursive function, and it's called 67 times.

What is meant by recursive calls?

A recursive call is one where procedure A calls itself or calls procedure B which then calls procedure A again. Each recursive call causes a new invocation of the procedure to be placed on the call stack.

How many recursive calls can you make Java?

For a 64 bits Java 8 program with minimal stack usage, the maximum number of nested method calls is about 7 000.

How many recursive cases can you have?

A recursive implementation may have more than one base case, or more than one recursive step. For example, the Fibonacci function has two base cases, n=0 and n=1.


3 Answers

You can use static variable to keep count of recursive call.

public class fibb {
    public static int count;
    static long fibonacci(long n){
        count++;
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args){
        System.out.println(fibonacci(14));
        System.out.println("Number of times fibonacci function called is :" +count);
    }
}
like image 72
Rohan Avatar answered Oct 04 '22 14:10

Rohan


If you don't want to use a static counter, you can add a parameter for the counter. You need to wrap the counter into an object, so that it can be modified in a way visible to the caller, for example an array of length 1:

public class fibb {

    static long fibonacci(long n) {
        return fibonacci(n, null);
    }

    static long fibonacci(long n, long[] counter){
        if (counter != null && counter.length > 0) ++counter[0];
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1, counter) + fibonacci(n - 2, counter);
    }

    public static void main(String[] args){
        long n = args.length > 0 ? Long.parseLong(args[0]) : 14;
        long[] counter = new long[1];
        System.out.println(fibonacci(n, counter));
        System.out.println(counter[0] + " calls to fibonacci.");
    }
}
like image 21
Daniel Fischer Avatar answered Oct 04 '22 14:10

Daniel Fischer


I suggest separate classes for both counter and the result:

class Counter {
    private int value = 0;

    public void inc() {
        value++;
    }

    public int get() {
        return value;
    }
}

class Result {
    public final long value;
    public final int calls;

    public Result(long value, int calls) {
        super();
        this.value = value;
        this.calls = calls;
    }

    public final long getValue() {
        return this.value;
    }

    public final int getCalls() {
        return this.calls;
    }
}

to be used like this

public class FibonacciWithCounter {
    static Result fibonacci(long n) {
        final Counter counter = new Counter();
        final long value = fibonacci(n, counter);
        return new Result(value, counter.get());
    }

    static long fibonacci(long n, Counter counter) {
        counter.inc();

        if (n == 0)
            return n;
        else if (n == 1)
            return n;
        else 
            return fibonacci(n - 1, counter) + fibonacci(n - 2, counter);

    }

    public static void main(String[] args) {
        final Result result = fibonacci(14);
        System.out.println("Result is " + result.value 
              + " (" + result.getCalls() + " calls)");
    }
}

So there is no static, and each parameter/class describes its purpose clearly. I prefer this, because its more expressive, even if this version is the longest one (due to the boiler plate of the additional classes).

like image 44
Beryllium Avatar answered Oct 04 '22 14:10

Beryllium