# 0.6 Discrete structures recursion  (Page 4/8)

 Page 4 / 8

Algorithm 1:   Even(positive integer k)

Input: k , a positive integer

Output: k-th even natural number (the first even being 0)

Algorithm:

if k = 1, then return 0;

else return Even(k-1) + 2 .

Here the computation of Even(k) is reduced to that of Even for a smaller input value, that is Even(k-1). Even(k) eventually becomes Even(1) which is 0 by the first line. For example, to compute Even(3), Algorithm Even(k) is called with k = 2. In the computation of Even(2), Algorithm Even(k) is called with k = 1. Since Even(1) = 0, 0 is returned for the computation of Even(2), and Even(2) = Even(1) + 2 = 2 is obtained. This value 2 for Even(2) is now returned to the computation of Even(3), and Even(3) = Even(2) + 2 = 4 is obtained.

As can be seen by comparing this algorithm with the recursive definition of the set of nonnegative even numbers, the first line of the algorithm corresponds to the basis clause of the definition, and the second line corresponds to the inductive clause.

By way of comparison, let us see how the same problem can be solved by an iterative algorithm.

Algorithm 1-a:   Even(positive integer k)

Input: k, a positive integer

Output: k-th even natural number (the first even being 0)

Algorithm:

int   i, even;

i := 1;

even := 0;

while( i<k ) {

even := even + 2;

i := i + 1;

}

return even .

Example 2: Algorithm for computing the k-th power of 2

Algorithm 2   Power_of_2(natural number k)

Input: k , a natural number

Output: k-th power of 2

Algorithm:

if k = 0, then return 1;

else return 2*Power_of_2(k - 1) .

By way of comparison, let us see how the same problem can be solved by an iterative algorithm.

Algorithm 2-a   Power_of_2(natural number k)

Input: k , a natural number

Output: k-th power of 2

Algorithm:

int   i, power;

i := 0;

power := 1;

while( i<k ) {

power := power * 2;

i := i + 1;

}

return power .

The next example does not have any corresponding recursive definition. It shows a recursive way of solving a problem.

Example 3: Recursive Algorithm for Sequential Search

Algorithm 3   SeqSearch(L, i, j, x)

Input: L is an array, i and j are positive integers, i ≤j, and x is the key to be searched for in L.

Output: If x is in L between indexes i and j, then output its index, else output 0.

Algorithm:

if i ≤j , then

{

if L(i) = x, then return i ;

else return SeqSearch(L, i+1, j, x)

}

else return 0.

Recursive algorithms can also be used to test objects for membership in a set.

Example 4: Algorithm for testing whether or not a number x is a natural number

Algorithm 4   Natural(a number x)

Input: A number x

Output: "Yes" if x is a natural number, else "No"

Algorithm:

if x<0,   then return "No"

else

if x = 0,   then return "Yes"

else return Natural( x - 1 )

Example 5: Algorithm for testing whether or not an expression w is a proposition (propositional form)

Algorithm 5   Proposition( a string w )

Input: A string w

Output: "Yes" if w is a proposition, else "No"

Algorithm:

if w is 1(true), 0(false), or a propositional variable, then return "Yes"

else if w = ~w1, then return Proposition(w1)

Preparation and Applications of Nanomaterial for Drug Delivery
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
I only see partial conversation and what's the question here!
what about nanotechnology for water purification
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
what is Nano technology ?
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
why we need to study biomolecules, molecular biology in nanotechnology?
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
why?
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
hi
Loga
Got questions? Join the online conversation and get instant answers! By Madison Christian By Briana Hamilton By OpenStax By OpenStax By Sheila Lopez By Janet Forrester By OpenStax By OpenStax By Cath Yu By