Recursive Problems
Recursive Problems
Recursive Iterative
approach approach
Table of contents
Problem 1: Calculate the sum of natural numbers from 1 to n
Problem 2: Calculate factorial of n. Remember n! = 1 * 2 * … * n
Problem 3: Calculate nm - the value of n to the m power
Problem 4: Find the nth Fibonacci number. The Fibonacci series is the series of numbers in which each number is the
sum of the previous two numbers.
Problem 5: Calculate the sum of elements of an array of numbers
Problem 6: Sort an array of numbers using bubble sort algorithm
Problem 7: Find a number in a sorted array of numbers (binary search)
Problem 8: Find the maximum number in an array containing numbers or other array of numbers
(on an unlimited number of levels)
Problem 1: Calculate the sum of natural numbers from 1 to n
Recursive solution
return sum;
}
If I know the sum of numbers
from 1 to n - 1 then I can
calculate the sum of numbers
from 1 to n by adding n to
previous sum
Problem 2: Calculate factorial of n. Remember n! = 1 * 2 * … * n
Recursive solution
return prod;
}
Recursive solution
println(powerNo(3, 2));
Iterative solution
function powerNo(n, m)
{ println(powerNo(3, 2));
if (m == 0)
return 1; The for loop is used to repeat function powerNo(n, m)
{
the multiplication operation var prod = 1;
if (m == 1)
return n; m times.
for(var i = 1; i <= m; i++)
return n * powerNo(n, m - 1); {
} prod *= n;
}
return prod;
}
Iterative solution
Recursive solution
function findFibonacci(n)
function findFibonacci(n) {
{ var fib0 = 0;
if (n == 0) var fib1 = 1;
return 0;
if (n == 0) This iterative solution is so FAST!
if (n == 1) return fib0; I can calculate much bigger
return 1;
if (n == 1)
Fibonacci numbers than with
return findFibonacci(n - 1) + return fib1; the recursive solution!
findFibonacci(n - 2);
} var fib;
fib0 = fib1;
fib1 = fib;
Ah… the classical problem }
of calculating the Fibonacci
return fib;
number using recursion! }
println(findFibonacci(10));
Problem 5: Calculate the sum of elements of an array of numbers
Recursive solution
// Find number n in sorted array ar // Find number n in sorted array ar using iterative approach
// Returns array index if found or -1 if not found // Returns array index if found or -1 if not found
function findNumber(n, ar) function findNumber(n, ar)
{ {
return _findNumber(n, ar, 0, ar.length - 1); var i1 = 0;
} var i2 = ar.length - 1;
Recursive solution // This is the stack on which will put the first array and then
// all the other sub-arrays that we find as we traverse an array
var ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];
var arrays = [];
var max = findMax(ar);
arrays.push(arElements);
println("Max = ", max);
// Loop as long as are arrays added to the stack for processing
// Use recursion to find the maximum numeric value in an
while(arrays.length > 0)
// array of arrays
{
function findMax(ar)
// Extract an array from the stack
{
ar = arrays.pop();
var max = -Infinity;
// ... and loop through its elements
// Cycle through all the elements of the array
for(var i = 0; i < ar.length; i++)
for(var i = 0; i < ar.length; i++)
{
{
var el = ar[i];
var el = ar[i];
// If an element is of type array, we'll add it to stack
// If an element is of type array -> invoke the same
// to be processed later
// function to find out the max elem. of that subarray
if ( Array.isArray(el) )
{
if ( Array.isArray(el) ) An iterative approach
arrays.push(el);
{
el = findMax( el );
continue; is also possible with
}
} the help of a stack
if ( el > max )
if ( el > max ) Traversing a {
{
max = el; hierarchical structure max = el;
}
} is so easy using }
}
recursion! }
return max;
return max;
}
}
These coding challenges were brought to you by codeguppy.com –
the fun coding site for kids, teens and creative adults