0% found this document useful (0 votes)
7 views16 pages

Recurs I On Lesson

Recursion is a method where a function calls itself, requiring a base case to prevent infinite loops. Key properties include having a base case and a progressive case to approach that base case. Examples of problems that can be solved using recursion include calculating factorials and generating Fibonacci numbers.

Uploaded by

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

Recurs I On Lesson

Recursion is a method where a function calls itself, requiring a base case to prevent infinite loops. Key properties include having a base case and a progressive case to approach that base case. Examples of problems that can be solved using recursion include calculating factorials and generating Fibonacci numbers.

Uploaded by

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

RECURSION

Recursion is a process in which


a method calls itself
continuously.
NB
At first this may seem like a never ending loop,
or like a dog chasing its tail. It can never catch
it.
So too it seems our method will never finish.
This might be true is some cases, but in practice
we can check to see if a certain condition is true
and in that case exit (return from) our method.

The case in which we end our recursion is called


a base case .Additionally, just as in a loop, we
must change some value and incremently
advance closer to our base case.
RECURSIVE METHOD
A method which
calls itself is
called recursive
method.
DEFITION SUMMARY
Simply put, recursion is when a
function calls itself.

That is, in the course of the


function definition there is a
call to that very same function
PROPERTIES OF RECURSION
A recursive function can go infinite like a loop.
To avoid infinite running of recursive function,
there are two properties that a recursive
function must have .
Base Case criteria − There must be at least
one base criteria or condition, such that, when
this condition is met the function stops calling
itself recursively.
Progressive case(recursive case) approach
− The recursive calls should progress in such a
way that each time a recursive call is made it
comes closer to the base criteria.
PROPERTIES OF RECURSION
Every recursion should have the following
characteristics.
1. A simple base case which we have
a solution for and a return value.
2. A way of getting our problem closer to
the base case.
I.e. a way to chop out part of the
problem to get a somewhat simpler
problem.
A recursive call which passes the simpler
problem back into the method.
HOW TO THINK RECURSIVELY
The key to thinking recursively is to see
the solution to the problem as a smaller
version of the same problem.
Identify the base case(s) and what the base
case(s) do.
A base case is the simplest possible problem (or
case) your method could be passed.
Return the correct value for the base case.
Your recursive method will then be comprised of
an if-else statement where the base case
returns one value and the non-base case(s)
recursively call(s) the same method with a
smaller parameter or set of data.
TWO MAIN PARTS OF A
RECURSIVE PROBLEM
 Thus you decompose your problem into two parts:
 (1) The simplest possible case which you can answer
(and return for) the BASE CASE,

 and (2) all other more complex cases which you will
solve by returning the result of a second calling of your
method THE RECURSIVE CURSE.
 This second calling of your method ( recursion ) will
pass on the complex problem but reduced by one
increment.
 This decomposition of the problem will actually be a
complete, accurate solution for the problem for all
cases other than the base case.
 Thus, the code of the method actually has the solution
on the first recursion
Designing Recursive Algorithms

Questions to ask yourself:


How can we reduce the problem to
smaller version of the same
problem?
How does each call make the
problem smaller?
What is the base case?
Will you always reach the base
case?
EXAMPLES OF PROBLEMS THAT
CAN BE SOLVED BY
RECURSION
1. FACTORIAL OF A NUMBER
In mathematics, the factorial of a non-
negative integer n, denoted by n!, is the
product of all positive integers less than or
equal to n.
For example, 5 ! = 5 × 4 × 3 × 2 × 1 = 120. ...
The value of 0! is 1, according to the
convention for an empty product.

So the rule is: n! = n × (n−1)!. Which just says


"the factorial of any number is that number
times the factorial of (1 smaller than that
number)", so 10! = 10 × 9!
1. FACTORIAL OF A NUMBER
1. FACTORIAL OF A NUMBER
static int factorial(int n)
{
 if (n == 1)
{
return 1;
}
else
{
return(n * factorial(n-1));
}
2. FIBONACCI NUMBERS
(SERIES)
The Fibonacci numbers are an infinite
sequence of numbers
0,1,1,2,3,5,8,13,21,... named after
Leonardo of Pisa, aka Leonardo
Fibonacci (1170-1250), in which each
item is formed by adding the previous
two, starting with :
0 and 1, i.e., 0+1->1, 1+1->2, 1+2->3,
2+3->5
ANALYSIS OF FIBONACCI
NUMBERS
The Fibonacci function can be defined
recursively as:
fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib(n-2) + fib(n-1)
Notice that in computing fib n, there are two
recursive calls that are duplicative in the
sense that computing
fib(n-1) necessarily computes fib(n-2) all over
again.
RECURSIVE CODE FOR
FIBONACCI NUMBERS
public int fib(int n) {
if(n <= 1)
{
return n;
}
else {
return fib(n - 1) + fib(n - 2);
}
}

You might also like