- #1

- 93

- 0

if (n>0){

if(b==0)

return a;

else

return function(n-1,a,function(n,a,b-1));

}

return b;

}

I have a recursive function of this form, how do I convert it to a loop?

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter japplepie
- Start date

- #1

- 93

- 0

if (n>0){

if(b==0)

return a;

else

return function(n-1,a,function(n,a,b-1));

}

return b;

}

I have a recursive function of this form, how do I convert it to a loop?

- #2

Mark44

Mentor

- 35,392

- 7,269

Code:`public static int function(int n, int a, int b){ if (n>0){ if(b==0) return a; else return function(n-1,a,function(n,a,b-1)); } return b; }`

I have a recursive function of this form, how do I convert it to a loop?

First, figure out what the recursive function does, then reproduce that action in a loop.

- #3

- 93

- 0

If it helps here's the real code.

Code:

```
public class hyperoperations {
public static int baseElement(int n, int a){
if (n==0)//successorship case (do nothing & retrieve the 1st parameter)
return a;
else if (n==1)//addition case (addition depends on iteration of incrementing with a constant)
return 0;
else if (n>1)//multiplication and higher case (anything higher than addition depends on iteration of operations depending on the 1st parameter itself)
return 1;
return -1;
}
public static int operation(int n, int a, int b){
if (n==0){//successorship case (increment the 2nd parameter by 1)
return b+1;
}
else if (n>0){//addition and higher case
if(b==0)
return baseElement(n-1,a);//get the base element or the zero element of the preceding order of n
else
return operation(n-1,a,operation(n,a,b-1));//expand until the 2nd parameter equals zero while considering right associativity
}
return -1;
}
public static void main (String[] args){//negative value means undefined (for the domain of non-negative a,b,n)
System.out.println("Format : input_a <operation_magnitude_n> input_b = output_c");
System.out.println("5<1>2="+operation(1,5,2));
System.out.println("5<2>2="+operation(2,5,2));
System.out.println("5<3>2="+operation(3,5,2));
System.out.println("5<4>2="+operation(4,5,2));
}
}
//Notes:
//operation n=1 = addition (a+b) (a+b = a+1+1+... b many times)
//operation n=2 = multiplication (a*b = a+a+... b many times)
//operation n=3 = exponentiation (a^b = a*a*... b many times)
//operation n=4 = tetration (a^^b = a^a^... b many times)
//operation n=k = k-tion
//always assumes right associativity e.g. 3<4>3 = 3^(3^(3)) != 3^3^3 <-- 3^27 != 3^9
```

- #4

AlephZero

Science Advisor

Homework Helper

- 7,002

- 294

http://blogs.msdn.com/b/chrsmith/archive/2008/08/07/understanding-tail-recursion.aspx

http://en.wikipedia.org/wiki/Tail_call

But your function makes

- #5

harborsparrow

Gold Member

- 613

- 155

By "pointer", in the above paragraph, I just mean an index into the array.

If you walk thru three stages of a factorial call by hand, you'll see what I mean.

For some problems, however, you may be able to find a non-recursive algorithm to use instead.

Share: