c programming functions recursion examples of … · c programming functions recursion examples of...

9
C Programming Functions Recursion Examples of Recursive Functions GCD Using Euclid’s method (m n> 0): GCD(m, n) = ( n, if m%n =0 GCD(n, m%n), otherwise Dijkstra’s method (assuming m>n> 0) GCD(m, n) is same as GCD(m - n, n): GCD(m, n) = n, if m = n GCD(m - n, n), if m>n GCD(m, n - m), if n>m R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7/8

Upload: nguyenduong

Post on 07-Jul-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

GCD

Using Euclid’s method (m ≥ n > 0):

GCD(m, n) =

{n, if m%n = 0GCD(n, m%n), otherwise

Dijkstra’s method (assuming m > n > 0) GCD(m, n) is same asGCD(m− n, n):

GCD(m, n) =

n, if m = n

GCD(m− n, n), if m > n

GCD(m, n−m), if n > m

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 2: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

GCD: Euclid’s Method#i n c l u d e <s t d i o . h>i n t GCD( i n t m, i n t n ) {

i f ( (m % n) == 0)r e t u r n n ;

r e t u r n GCD(n , m % n ) ;}

i n t main ( ) {i n t m, n ;

p r i n t f ( ” Ente r m, n ” ) ;s c a n f ( ”%d %d” , &m, &n ) ;

i f (m < n )p r i n t f ( ”GCD(%d , %d ) = %d\n” , m, n , GCD(n , m) ) ;

e l s ep r i n t f ( ”GCD(%d , %d ) = %d\n” , m, n , GCD(m, n ) ) ;

}

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 3: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

GCD: Dijkstra’ Method#i n c l u d e <s t d i o . h>i n t GCD( i n t m, i n t n ) {

i f (m == n)r e t u r n m;

i f (m > n )r e t u r n GCD(m−n , n ) ;

r e t u r n GCD(m, n−m) ;}

i n t main ( ) {i n t m, n ;p r i n t f ( ” Ente r m and n : ” ) ;s c a n f ( ”%d %d” , &m, &n ) ;p r i n t f ( ”GCD(%d , %d ) = %d\n” , m, n , GCD(m, n ) ) ;

}

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 4: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

Binomial Coefficient

(n

r

)=

1, if r = 0

1, if n = r(n−1

r

)+(n−1r−1

)otherwise

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 5: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

Binomial Coefficient#i n c l u d e <s t d i o . h>i n t binom ( i n t n , i n t r ) {

i f ( r == 0 | | n == r )r e t u r n 1 ;

r e t u r n binom (n−1, r ) + binom (n−1, r −1);}i n t main ( ) {

i n t n , r ;p r i n t f ( ” Ente r n , r : ” ) ;s c a n f ( ”%d %d” , &n , &r ) ;p r i n t f ( ”binom(%d , %d ) = %d\n” , n , r , binom (n , r ) ) ;

}

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 6: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

Pascal’s Trianglei n t main ( ) {

i n t i , j , n ;p r i n t f ( ” Ente r n : ” ) ;s c a n f ( ”%d” , &n ) ;

f o r ( i = 0 ; i < n ; i++) {f o r ( j = 0 ; j <= i ; j++) {

p r i n t f ( ”%6d” , binom ( i , j ) ) ;}p r i n t f ( ”\n” ) ;

}}

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 7: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

Tower of Hanoi

1

2

A B C

A B C

A B C

3

Two recursive problems of size n− 1 to besolved.

Base case is moving the disk with largestdiameter.

So, spec of tower(n, A, B, C):

If n = 1 thenmove disk n from A to CElse execute following steps:

1 tower(n-1, A, C, B),2 move disk n from A to C,3 tower(n-1, B, A, C)

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 8: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Examples of Recursive Functions

Tower of Hanoi#i n c l u d e <s t d i o . h>v o i d tower ( i n t n , char A, char B, char C) {

i f ( n == 1) {p r i n t f ( ”Move d i s k 1 from %c to %c\n” , A, C ) ;r e t u r n ;

}tower (n−1, A, C , B ) ;p r i n t f ( ”Move d i s k %d from %c to %c\n” , n , A, C ) ;tower (n−1, B, A, C ) ;r e t u r n ;

}i n t main ( ) {

i n t n ;p r i n t f ( ” Ente r n : ” ) ;s c a n f ( ”%d” , &n ) ;tower (n , ’A ’ , ’B ’ , ’C ’ ) ;

}

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 7 / 8

Page 9: C Programming Functions Recursion Examples of … · C Programming Functions Recursion Examples of Recursive Functions Tower of Hanoi #include  void tower ( int n

C Programming

Functions

Recursion

Merge Sort

Merging

1

2

5

7

9

10

14

15

0

3

4

6

8

12

11

13

1

2

5

7

9

10

14

15

0

3

4

6

8

12

11

13

1

5

7

9

10

14

15

0

3

4

6

8

12

11

13

1

2

5

7

9

10

14

15

2

comp

smallersmaller

smaller

com

p

comp

com

p

A B A B A B A B

0

3

4

6

8

12

11

13

0 1 2 3sm

alle

r

auxiliary array (double the size of A or B)

R. K. Ghosh (IIT-Kanpur) C Programming March 2, 2011 8 / 8