{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect10a - DS CS 11002 Computer Sc Engg IIT Kharagpur $ 1...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % From Inductive Definition to Loop Lect 10A Goutam Biswas
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % Definition Consider the factorial function n ! . It may be viewed as a sequence 0! , 1! , 2! , 3! , 4! , · · · = 1 , 1 , 2 , 6 , 24 , · · · . The n th term of the sequence t n can be defined as follows: t n = 1 if n = 0 ( basis ) , nt n - 1 if n > 0 . Lect 10A Goutam Biswas
Background image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % Converting the Definition to Code 1. Initialize a variable fact to the value of the basis . Also initialize a variable i as the sequence index of the next element to compute. 2. Repeat the following steps until i exceeds the input n . (a) multiply fact by i , (b) increment i to the next index. Lect 10A Goutam Biswas
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % A Code fact = i = 1 ; while(i<=n) fact *= i++ ; Lect 10A Goutam Biswas
Background image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % Definition The n th term t n of the Fibonacci sequence 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , · · · is defined inductively as follows: t n = n if n = 0 , 1 ( basis ) , t n - 1 + t n - 2 if n > 1 . Lect 10A Goutam Biswas
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % Converting the Definition to Code 1. Initialize two variables f0 and f1 to zero and
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}