recursion and induction
play

Recursion and Induction - PowerPoint PPT Presentation

CoSc 450: Programming Paradigms 02 Recursion and Induction


  1. CoSc 450: Programming Paradigms 02 Recursion and Induction

  2. ���������������������� ��� ��������� ������������ �� � �� � ���� ������ � CoSc 450: Programming Paradigms 02 ���� ������ � � ����������� � �� Recursive definition of factorial ������������������������������������������������������������������������������������� ����������������������� � �� n = 0 , 1 n ! = n · ( n − 1 ) ! �� n > 0 . ����� � ���������� ���������������� � ������������������� �������������������� �� ������������������������������ � ��������������� ��������������������� � ������������������� �������������������� � ������������������������������ � ��������������������������������������������������������������� ���������������������� ������������������������������������������������������������� ������������ ������������ ������������������������������������������������������������������������� ����� ��������������������������������������� ������ �� �� � �� � ���� ������ � ���� ������ ������ � � �� � ��� � �� �������������������� �������� ������� ��������������������������������������������������������� ����� ����������� ����� � ���������� ���������������� � ��������������������������������� �� ���������������� ����������������������� ������������������������������������������� ����������������������������������������������������������� ���������������������������� ������������������������������������������ ���������� ����� ����������������� ������������������������������ ��������������� ���������� �������� ������� ��������� � � ����� ������������������������������������� �������� ���������� ����

  3. CoSc 450: Programming Paradigms 02 Squaring a number recursively without multiplication.

  4. CoSc 450: Programming Paradigms 02 Squaring a number recursively without multiplication. n 2 ( n − 1) 2 Compute given

  5. CoSc 450: Programming Paradigms 02 Squaring a number recursively without multiplication. n 2 ( n − 1) 2 Compute given ( n − 1) 2 = n 2 − 2 n + 1 n 2 = ( n − 1) 2 + 2 n − 1

  6. CoSc 450: Programming Paradigms 02 Squaring a number recursively without multiplication. n 2 ( n − 1) 2 Compute given ( n − 1) 2 = n 2 − 2 n + 1 n 2 = ( n − 1) 2 + 2 n − 1

  7. CoSc 450: Programming Paradigms 02 Squaring a number recursively without multiplication. n 2 ( n − 1) 2 Compute given ( n − 1) 2 = n 2 − 2 n + 1 n 2 = ( n − 1) 2 + 2 n − 1

  8. CoSc 450: Programming Paradigms 02 Squaring a number recursively without multiplication. n 2 ( n − 1) 2 Compute given ( n − 1) 2 = n 2 − 2 n + 1 n 2 = ( n − 1) 2 + 2 n − 1 Program this in Scheme

  9. CoSc 450: Programming Paradigms 02 Prove square is correct. (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1)))))

  10. CoSc 450: Programming Paradigms 02 Prove square is correct. (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) The correctness proof of a recursive function is by mathematical induction.

  11. CoSc 450: Programming Paradigms 02 Mathematical induction: • Base case corresponds to base case in code. • Inductive case corresponds to recursive call in code. • Use code inspection to convert from Scheme to traditional infix notation in both cases.

  12. CoSc 450: Programming Paradigms 02 Mathematical induction: • Base case corresponds to base case in code. • Inductive case corresponds to recursive call in code. • Use code inspection to convert from Scheme to traditional infix notation in both cases.

  13. CoSc 450: Programming Paradigms 02 Mathematical induction: • Base case corresponds to base case in code. • Inductive case corresponds to recursive call in code. • Use code inspection to convert from Scheme to traditional infix notation in both cases.

  14. CoSc 450: Programming Paradigms 02 Mathematical induction: • Base case corresponds to base case in code. • Inductive case corresponds to recursive call in code. • Use code inspection to convert from Scheme to traditional infix notation in both cases.

  15. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Base case Code inspection: (square 0) returns 0. 0 2 = 0 Math: Therefore, correct in base case.

  16. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Base case Code inspection: (square 0) returns 0. 0 2 = 0 Math: Therefore, correct in base case.

  17. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Base case Code inspection: (square 0) returns 0. 0 2 = 0 Math: Therefore, correct in base case.

  18. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Base case Code inspection: (square 0) returns 0. 0 2 = 0 Math: Therefore, correct in base case.

  19. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Inductive case Prove that n 2 (square n) terminates with value assuming that ( n − 1) 2 (square (- n 1)) terminates with value as the inductive hypothesis.

  20. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Inductive case Prove that n 2 (square n) terminates with value assuming that ( n − 1) 2 (square (- n 1)) terminates with value as the inductive hypothesis.

  21. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) (- (+ n n) 1))))) Inductive case Prove that n 2 (square n) terminates with value assuming that ( n − 1) 2 (square (- n 1)) terminates with value as the inductive hypothesis.

  22. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) � Inductive case (- (+ n n) 1))))) ����������������� ������� �� = ⟨ ��������������� ⟩ ������� �� � ��� +( n + n ) − 1 = ⟨ �������������������� ⟩ ( n − 1 ) 2 +( n + n ) − 1 = ⟨ ���� ⟩ n 2 − 2 n + 1 + 2 n − 1 = ⟨ ���� ⟩ n 2

  23. CoSc 450: Programming Paradigms 02 (define square (lambda (n) (if (= n 0) 0 (+ (square (- n 1)) � Inductive case (- (+ n n) 1))))) ����������������� ������� �� = ⟨ ��������������� ⟩ ������� �� � ��� +( n + n ) − 1 = ⟨ �������������������� ⟩ ( n − 1 ) 2 +( n + n ) − 1 = ⟨ ���� ⟩ n 2 − 2 n + 1 + 2 n − 1 = ⟨ ���� ⟩ n 2

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend