0% found this document useful (0 votes)
25 views3 pages

Practice Problem # 1 - Recursion 1

The document discusses recursion, which is a method where a function calls itself using smaller inputs. It provides examples of recursive functions like computing factorials and sums of arrays. It also discusses the base case and recursive case of recursive functions. Finally, it provides self-test questions to solidify understanding of recursion.

Uploaded by

Kayee Alz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Practice Problem # 1 - Recursion 1

The document discusses recursion, which is a method where a function calls itself using smaller inputs. It provides examples of recursive functions like computing factorials and sums of arrays. It also discusses the base case and recursive case of recursive functions. Finally, it provides self-test questions to solidify understanding of recursion.

Uploaded by

Kayee Alz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DCIT25 February 4, 2020

Recursion
Recursion is the process of defining a function to call itself using smaller inputs. A function that uses
recursion is called a recursive function. As an example, consider the factorial function, 𝑛! defined
to be 1 × 2 × 3 × 4 × … × 𝑛. This function can easily be implemented as a for-loop statement, but it
can be defined as a function 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(𝑛):
𝑛 × 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(𝑛 − 1) 𝑛 > 1
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(𝑛) = {
1 𝑛=1
The computation of 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(5) is shown below:
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(5) = 5 ∗ 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(4)
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(4) = 4 ∗ 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(3)
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(3) = 3 ∗ 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(2)
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(2) = 2 ∗ 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(1)
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(1) = 1
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(2) = 2 ∗ 1 = 2
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(3) = 3 ∗ 2 = 6
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(4) = 4 ∗ 6 = 24
𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(5) = 24 ∗ 5 = 120
This function can be implemented in Java:
public int factorial(int n){
if(n==1) return 1
return n * factorial(n-1);
}

A recursive function has two major parts - the base case and the recursive case. The base case
returns a result without recursion when the input is small enough that it can be computed directly.
The base case in the factorial function is the part that computes the factorial function for 𝑛 = 1. The
recursive case is the part where recursive calls are made. It also includes procedures for combining
results from each recursive calls.

The factorial function can be written to be tail recursive as follows:


public int factorial(int n){
return factorial(n, 1);
}
public int factorial(int n, int p){
if(n == 1) return p
return factorial(n-1, n * p);
}

In tail recursion, the only step after doing the recursion is to return the output.
DCIT25 February 4, 2020

Problems that can be solved using loop statements can be solved using recursion. Execute and
analyze the following:
Printing the First 𝒏 Positive Integers
Idea: If 𝑛 = 1 then print 1. Otherwise, print the first 𝑛 − 1 positive integers recursively then print
𝑛.

public void printNPositive(int n){


if(n==1) {
System.out.println(1);
} else {
printNPositive(n-1);
System.out.println(n);
}
}

Computing the Sum of an Array of Integers


Idea: If the array contains 1 element only, output the element. Otherwise, add the first
element to the sum of the remaining elements computed recursively.

public int sum(int[] a){


return sum(a,0);
}
public int sum(int[] a, int i){
if(start == a.length - 1) return a[i];
return a[i] + sum(a, i + 1);
}

The first sum() function that takes only one input is a helper function (this is not the recursive
function) to call the recursive sum() function (the other sum function that takes 2 inputs) with the
correct initial values. This strategy is common with recursive functions.
Computing the sum can be written as tail recursive:

public int sum(int[] a){


return sum(a,0,0);
}
public int sum(int[] a, int i, int s){
if(start == a.length) {
return s;
}
return sum(a, i + 1, s + a[i]);
}
DCIT25 February 4, 2020

Finding the Maximum Value in the Array


Idea: If the array contains 1 element only, output the element. Otherwise, compare the first
element to the maximum of the remaining elements computed recursively.

public int max(int[] a){


return max(a,0);
}
public int max(int[] a, int start){
if(start == a.length - 1) return a[start];
return Math.max(a[start], max(a, start + 1));
}

Reversing the Array


Idea: If the array contains at most 1 element do nothing. Otherwise, swap the first and last
element of the array then recursively reverse the remaining elements.

public int reverse(int[] a){


reverse(a,0,a.length-1);
}
public int reverse(int[] a, int start, int end){
if(start < end){
int tmp = a[start];
a[start] = a[end];
a[end] = tmp;
reverse(a, start + 1, end -1);
}
}

Self-Test

1. Create a tail recursive function to compute the maximum value of an array.

2. Create a recursive function that takes as input an array 𝐴 of positive integers and a number
𝑑 then prints all numbers in 𝑎 divisible by 𝑑, one line at a time.

3. Create a recursive function that returns the index of element 𝑥 in the array 𝐴 if 𝑥 is in the array,
otherwise it returns -1. Hint: If 𝑥 is in the array then it is either the first element or some other
element after the first element.

4. Implement a recursive function that returns true if an element 𝑥 is in a 2-dimensional array 𝐴


otherwise it prints false.

5. Create a recursive function that takes an array of characters then print all possible pairs of
characters in the array, one line at a time.

You might also like