3

as a beginner in programming I am trying to convert the following recursive method to an iterative one but I just don't get the hang of it. The method or function has a binary tree like recursion and I would like to use an array for the iterative solution.. unfortunately I am very confused how to do it.

I have already checked the way of converting the fibonnaci recursive method to an iterative one. But I think this is not the same here. Also I am not sure if a tree search method is useful?! Any help, hint, idea would be appreciated. Thanks.

public static int funct(int n) {

  if (n == 0) return 1;
  if (n == 1) return 2;
  if n > 1 return funct(n-2)*funct(n/2);

}
4
  • fibonacci or binary tree?. What is the question about ? Commented Sep 8, 2016 at 10:23
  • funct(n/2)? What if n is odd? That is a strange function you have here Commented Sep 8, 2016 at 10:26
  • Integer division? Commented Sep 8, 2016 at 10:27
  • @fge it seems it's related to A242634 where the answer is 2^A242634(n) Commented Sep 8, 2016 at 15:21

2 Answers 2

8

Since every n-th member is computed by others before if you can cache all in a list. You start by adding the first 2 known members. Fibonacci its easier because you always need only previous value.

private static int func(int n) {
    List<Integer> values = new ArrayList<>(n+1);
    values.add(1);
    values.add(2);
    for (int i = 2; i <= n; i++) {
        values.add(values.get(i - 2) * values.get(i / 2));
    }

    return values.get(n);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's the answer.
I was thinking using a while with a Stack, but yours looks btter
super solution with super logic
2

Now the real function is without last if:

public static int funct(int n) {
    if (n == 0) return 1;
    if (n == 1) return 2;
    return funct(n-2) * funct(n/2);
}

As the recursive calls refer to smaller parameters one can cache all return values upto n.

Unfortunately this already spoils the pleasure, as the resulting code is complete:

public static int funct(int n) {
    int[] results = new int[n+1];
    results[0] = 1;
    results[1] = 2;
    int i = 2;
    while (i <= n) {
        results[i] = results[i-2] * results[i/2];
        ++i;
    }
    return results[n];
}

It indeed looks like fibonacci.

In general one would not need to fill all items of results. like probably results[n - 1]. Unfortunately you should have learnt prior to this problem:

  • Solving tail recursion.
  • Using a stack (like here) to use inner results of a recurive call.

You might look into those topics.


Math afterlude

The initial values are powers of 2. As the result is a product of earlier results, all results will be powers of 2.

f(0) = 1 = 2^0
f(1) = 2 = 2^1
f(n) = f(n - 2) * f(n / 2)

Hence you can introduce:

g(0) = 0
g(1) = 1
g(n) = g(n - 2) + g(n / 2)
f(n) = 2^g(n)

This will enlarge the range you can calculate as say 2100.

You will also see:

g(2k + 1) = g(2k) + 1

So you will only need a domain of even numbers:

g(2k) = g(2(k-1)) + g(k - k%2) + k%2

6 Comments

you should also increment i in the while loop
@user1121883 now it is infinitely better.
@joop just miss the link :)
@JekinKalariya tail recursion in wikipedia was not very convincing for me, and stack is more or less what I did. We both know that simple math will yield some power of 2 formula f(n) = 2^{n-2 + [(n-8):2].[(n-6):2] / 2} given n > 5with integer division and a sum 1+2+3+...+k. (Not sure whether I got it right.) It would indeed be nice to show that math derivation, but the OP has to do that work himself.
@Joop: Thanks.. that's really great.. I very much like your implementation! I definitely will have a look at the mentioned topics of tail recursion and use of stack. I am also thinking to optimize the size of the array. I don't know if this has any benefit but one can set the length to max(n-2, n/2) + 1. What do you think?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.