<< Chapter < Page Chapter >> Page >

Algorithm 1:   Even(positive integer k)

Input: k , a positive integer

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


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)


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


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


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.


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"


if x<0,   then return "No"


    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"


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

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

Questions & Answers

How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Discrete structures. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10768/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Discrete structures' conversation and receive update notifications?