How can I make a recursive version of my iterative method? - java

I am trying to write a recursive function in Java that prints the numbers one through n. (n being the parameter that you send the function.) An iterative solution is pretty straightforward:
public static void printNumbers(int n){
for(int i = 1; i <= n; i++){
System.out.println(i);
i++;
}
As a new programmer, I'm having troubles figuring out how a recursive version of this method would work.

You are using a for loop that is iterating from i=1 to n. As you want to do this with recursion and it is easier to pass n instead of i and n, we just reverse the whole thing, so we count down n to 1. To keep the order of the prints, we first call the recursive function and print the number after the execution:
public static void printNumbers ( int n )
{
if ( n > 0 )
{
printNumbers( n - 1 ); // n - 2, if the "i++" within the for loop is intended
System.out.println( n );
}
}
For simple iterative -> recursive conversions it is easy to change loops into a format like this:
public static void printNumbers ( int n )
{
int i = 1;
while ( i <= n )
{
System.out.println( i );
i++; // i += 2, if the "i++" within the for loop is intended
}
}
Now you can easily transform that into a recursive function:
public static void printNumbers ( int n, int i )
{
if ( i <= n )
{
System.out.println( i );
i++; // i += 2, if the "i++" within the for loop is intended
printNumbers( n, i );
}
}
Everything else is optimization.

The recursive version needs two arguments (n and i) so make it an auxiliary non-public method and just call it from the public method to start the recursion going:
static void auxPrintNumbers(int n, int i){
if(i <= n) {
System.out.println(i);
auxPrintNumbers(i + 1);
}
}
public static void printNumbers(int n){
auxPrintNumbers(n, 1);
}

Your iterative version has some problems: you are iterating i twice, in the for statement then again at the end of the loop; also you should let i < n be the ending condition of your loop.
To answer your question, obviously the recursive function will have to print out the current number and if the current number hasn't yet reached n, call itself again - so it must take the current number (which we're calling i in the iterative version) as a parameter - or the class needs to hold it as an instance variable, but I'd stick with the parameter.

According to your function that prints every odd number from 1 to n the recursive function should look something like this:
public static void printNumbersRecursive(int n)
{
if (n % 2 == 0) printNumbersRecursive(n - 1);
else if (n > 0)
printNumbersRecursive(n - 2);
System.out.println(n);
}
if it is an error and that you'd want to print EVERY number from 1 to n then:
public static void printNumbersRecursive(int n)
{
if (n > 0)
printNumbersRecursive(n - 1);
System.out.println(n);
}

A class version (just for fun):
class R {
private final int n;
public R (final int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive");
}
this.n = n;
}
#Override
public String toString () {
final StringBuilder sb = new StringBuilder();
if (this.n > 1) {
sb.append(new R(this.n - 1).toString());
}
sb.append(this.n).append(" ");
return sb.toString();
}
}
Used as:
System.out.println(new R(10));

public static void printNumbers(int n){
if( n > 1 ){
printNumbers(n - 1);
}
System.out.println(n);
}
This function calls itself recursively until it reaches n = 1. From this point all values are printed in correct order: 1, 2, 3, ...

Related

Clarification on tail recursive methods

Is the following method tail-recursive?
I believe that it is not tail recursive because it relies on the previous results and so needs a stack frame, am I correct to state this?
public int[] fib(int n)
{
if(n <= 1){
return (new int[]{n,0});
}
else{
int[] F = fib(n-1);
return (new int[]{F[0]+ F[1], F[0]});
}
}
You are correct: It is not tail recursive because the last line is not of the form
return funcName(args);
Yes, you are correct, since it does not end with a call to itself of the form of
return fib(somevalue);
To convert it into a tail-recursive version you could do something like
// Tail Recursive
// Fibonacci implementation
class GFG
{
// A tail recursive function to
// calculate n th fibonacci number
static int fib(int n, int a, int b )
{
if (n == 0)
return a;
if (n == 1)
return b;
return fib(n - 1, b, a + b);
}
public static void main (String[] args)
{
int n = 9;
System.out.println("fib(" + n +") = "+
fib(n,0,1) );
}
}
Code taken from https://www.geeksforgeeks.org/tail-recursion-fibonacci/

Java Recursion General Concept

Here is a simple recursion question in java. This one I have been working on but need to refine my approach.
Write a recursive method with two int parameters, m and n. The precondition requires 0 <= m and m <= n. The method prints a line of m asterisks, then a line of m+1 asterisks, and so on up to a line of n asterisks. Then the same pattern is repeated backward: a line of n asterisks, then n-1, and so on down to n. The only loop allowed in your implementation is a loop to print a line of m asterisks.
This is what I have so far as test methods
package Recursion;
class Asterisk
{
public static void asterisk(int m, int n)
{
if (m == n)
{
printAsterisk(n);
return;
}
else if (m < n)
{
printAsterisk(m);
asterisk(m + 1, n);
}
else
{
printAsterisk(m);
asterisk(m - 1, m);
}
}
public static void printAsterisk(int m)
{
for (int i = 0; i < m; i++)
{
System.out.print("*");
}
System.out.println("");
}
public static void main(String[] args)
{
int m = 3;
int n = 5;
asterisk(m, n);
asterisk(n, m);
}
}
So, you need this:
printAsterisk(3, 5)
***
****
*****
*****
****
***
Think of it this way: printAsterisk(6, 5) prints nothing. printAsterisk(3, 5) prints 3 asterisks, then inserts printAsterisk(4, 5), then prints 3 asterisks again.
The following code will work:
class Test {
public static void asterisk(int m, int n) {
if (m == n) {
printAsterisk(m);
return;
} else if (m < n) {
printAsterisk(m);
asterisk(m + 1, n);
} else {
printAsterisk(m);
asterisk(m - 1, n);
}
}
public static void printAsterisk(int m) {
for (int i = 0; i < m; i++) {
System.out.print("*");
}
System.out.println("");
}
public static void main(String[] args) {
int m = 3;
int n = 5;
asterisk(m, n);
asterisk(n, m);
}
}
Please note that this is not the right way to do it. Since this question feels like an assignment, I will not post the accurate version. Now, your task is to remove two calls in the main method to asterisk and modify the method to work with a single call.
Your approach is fine you just need to think about what's going into printAsterisk(int m, int n).
A recursive function can often be thought of as solving one small part of a repeatable problem and then delegating the rest of the problem to itself using different parameters.
Consider the following output (printAsterisk(4, 4);):
****
If we wanted to get the following output (printAsterisk(3, 4);):
***
****
***
it is easy to see that this output contains the output of printAsterisk(4, 4); in the middle so when you call printAsterisk(3, 4); it should print 3 asterisks, call printAsterisk(4,4); and then print the remaining 3 asterisks.
If you extend this a bit further the implementation should be easy to see.

Strange behavior in recursive algorithm,

I was writing a recursive algorithm to calculate Fibonacci numbers in Java as part of a programming 101 course. This is the code:
public class Fib {
public static void main(String[] args) {
Fib fib = new Fib();
}
public Fib() {
int end = 9;
long[] nums = new long[2];
printFib(0, end, nums);
}
private void printFib(int i, int end, long[] nums) {
while(i < end) {
if(i == 0 || i == 1) {
nums[i] = 1;
System.out.println("1");
} else {
long fib;
fib = 0;
fib += (nums[0] + nums[1]);
nums[0] = nums[1];
nums[1] = fib;
System.out.println(fib);
}
i++;
printFib(i, end, nums);
}
}
}
As I was stepping through the program it was working as intended until i became equal to end, the variable telling the printFib method how many Fibonacci numbers it should print out. When ì was equal to end while(i < 1) returns false as expected and the program go to the last }, now you'd(me)
expect the program to return the constructor from which I initially called the function and the program should exit, this not the case. The program goes back to the while statement and somehow evaluates to false again. Then it does the same thing again except the second time it decreases i by 1(what?!) and then proceeds to the else clause when it reaches the if statement. It then does the same thing over and over alternating the amount it subtracts from i between 1 and 2. I've asked my teacher about this and he was unable to explain it.
The program works fully like I intended if I replace the while with an if so maybe there is something about while that I don't know.
Edit
So I realize now that each time the method is called i has a different value which is stored and when the method exits and i = end the program goes back to the previous calls where i had a different value.
You implemented an iterative algorithm to calculate Fibonacci series. That's what the while loop does. There is no point in making the recursive call - printFib(i, end, nums) - at the end.
If you intended a recursive implementation, the entire while loop is not needed.
This code doesn't look right to me.
I would recommend that you not print from your method. Return a value to the main and let it print.
Your recursive method should not have a while loop in it. That's iteration - exactly what you're trying to avoid here.
Your method should have a stopping condition and a call to itself. That's not what you're doing.
Think about it like this:
/**
* Recursive Fibonnaci
* User: mduffy
* Date: 2/11/2015
* Time: 8:50 AM
* #link http://stackoverflow.com/questions/28455798/strange-behavior-in-recursive-algorithm/28455863#28455863
*/
public class Math {
private static Map<Integer, Integer> memo = new ConcurrentHashMap<Integer, Integer>();
public static void main(String [] args) {
for (String arg : args) {
int n = Integer.valueOf(arg);
System.out.println(String.format("n: %d fib(n): %d", n, fibonnaci(n)));
}
}
public static int fibonnaci(int n) {
if (n < 0) throw new IllegalArgumentException("index cannot be negative");
int value = 0;
if (memo.containsKey(n)) {
value = memo.get(n);
} else {
if (n <= 1) {
value = n;
} else {
value = fibonnaci(n-1)+fibonnaci(n-2);
}
memo.put(n, value);
}
return value;
}
}
Basicly this is happening because i would guess that you are thinking of i as an reference which will influence the basic callings of the Fibunacci method calling the sub Fibunacci method. This will finally lead way to many calls of the fibunacci method.
in my eyes the loop doesn´t make sense in your recursive way of solving it.

Different ways of using recursion in Java

I'm thinking of several elegant ways of writing a simple Lisp-like recursive function in Java that does, let's say, a simple summation.
In Common Lisp it would be like this:
(defun summation(l)
(if l
(+ (car l) (summation (cdr l)))
0))
(summation '(1 2 3 4 5)) ==> 15
In Java the one of many possible solutions would be:
public int summation(int[] array, int n) {
return (n == 0)
? array[0]
: array[n] + summation(array, n - 1);
}
CALL:
summation(new int[]{1,2,3,4,5}, 4); //15
1) Is there any possible way NOT to use the index n?
2) Or leave your solution (non-iterational) which you see as interesting.
Thanks.
Using Java Collections - something like this should give you an idea of how to eliminate n and recurse in terms of the list size instead:
public int summation( List<Integer> list ) {
return list.isEmpty()
? 0
: list.get( list.size - 1 ) + summation( list.subList( 0 , list.size() - 1 ) );
}
Cheers,
Usually, I solve this kind of recursion with a public API that does not require the index parameter and a private API with any signature I#d like it to be. For this I would separate it this way:
public int summation(int[] numbers) {
return summation(numbers, numbers.length - 1);
}
private int summation(int[] numbers, int till) {
return (till < 0) ? 0 : numbers[till] + summation(numbers, till - 1);
}
Note that you must check till < 0 as this handles an empty array correctly.
Another way would be to not use an array, but any Iterable<Integer>:
public int summation(Iterable<Integer> numbers) {
return summation(numbers.iterator());
}
private int summation(Iterator<Integer> numbers) {
return (numbers.hasNext()) ? numbers.next() + summation(numbers) : 0;
}
Hint: The order of calls in numbers.next() + summation(numbers) is important, as the next() call must be done first.
If you use List.subList method, it may perform iteration, underneath. You can use Queue instead, to avoid iteration. For example:
public int sum(Queue queue) {
return queue.isEmpty() ? 0 : (queue.poll() + sum(queue));
}
public class HelloWorld{
static int sum=0;
static int c;
public static void main(String []args){
int[] y={1,2,3,4,5};
c=y.length;
System.out.println( summation(y)); //15
}
public static int summation(int[] array) {
c--;
if(c<0){
return sum;
}
else{
sum+=array[c];
return summation(array);
}
}
}
Here's a simple method that seems pretty close to what's being asked for.Basically, we are taking a recursive approach to performing summation ascontrasted with brute force from the bottom up.
public static int sumToN(int n) {
if( n == 0 ){
return 0;
}
return n + sumToN(n - 1);
}

Basic Java Recursion Method

I am having a lot of trouble with this basic recursion problem in java; any pointers would be great.
"Write a static recursive method to print out the nth term of the
geometric sequence: 2, 6, 18, 54."
From what I can gather, somewhere in the code I should be recursively multiplying something by 3, but I'm struggling to figure out how to do this. I know I need a termination statement, but when does that occur? Do I need a helper method?
A Recursive Function is a function whose implementation references itself. Below is some funny example:
public class Inception {
public void dream() {
boolean enoughDreaming = false;
//Some code logic below to check if it's high time to stop dreaming recursively
...
...
if(!enoughDreaming) {
dream(); //Dream inside a Dream
}
}
}
And the solution for your problem:
public class GeometricSequence {
public static void main(String[] args) {
//Below method parameters - 5 = n, 1 = count (counter), res = result (Nth number in the GP.
System.out.println(findNthNumber(5, 1, 2));
}
public static int findNthNumber(int n, int count, int res) {
return ((count == n)) ? res : findNthNumber(n, count+1, res *3);
}
}
EDIT:
The above class uses "int", which is good only for small numbers (because of Integer Overflow problem). The below class is better for all types/numbers:
public class GeometricSequence {
public static void main(String[] args) {
//Below method parameters - 5 = n, 1 = count (counter), res = result (Nth number in the GP.
System.out.println(findNthNumber(2000, 1, new BigInteger("2")));
}
public static BigInteger findNthNumber(int n, int count, BigInteger res) {
return ((count == n)) ? res : findNthNumber(n, count+1, res.multiply(new BigInteger("3")));
}
}
This is the simplest example of recursion.
You need a method declaration.
You need to check if the end has been reached.
Otherwise you need to call the method again with an operation which makes the difference between one term and the next.
Yes, you need a termination condition - basically when you've taken as many steps as you need. So consider how you want to transition from one call to another:
How are you going to propagate the results so far?
What extra state do you need to keep track of how many more steps you need to take?
What are you going to return from the method?
Here's a C# example (I know your doing Java but it's pretty similar)
public static void Recursive(int counter, int iterations, int value, int multiplier)
{
if (counter < iterations)
{
Console.WriteLine(value);
counter++;
Recursive(counter, iterations, (value * multiplier), multiplier);
}
}
So when you run the function you enter the parameters
"counter" will always be 0 when you first call it
"iterations" is the value of n
"value" is your starting value, in your case 2
"multiplier" is how much you want to multiply by each iteration, in your case 3
Every time it runs it will check to see if counter is less than iterations. If it is more, the value is printed, the counter is incremented, the value is multiplied by the multiplier and you add the same parameters back in to the function.
A recursive solution: Seq(1) is the first element of the sequence .... Seq(n-th)
public static void main(String args[]) throws Exception {
int x = Seq(3); //x-> 18
}
public static int Seq(int n){
return SeqRec(n);
}
private static int SeqRec(int n){
if(n == 1)
return 2;
else return SeqRec(n - 1) * 3;
}
Non-Recursive solution:
public static int Non_RecSeq(int n){
int res = 2;
for(int i = 1; i < n; i ++)
res *= 3;
return res;
}
public static void main(String args[]) throws Exception {
int x = Non_RecSeq(3); //x-> 18
}

Categories