iteration and invariants
play

Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail - PowerPoint PPT Presentation

CoSc 450: Programming Paradigms 03 Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail recursion: What our author calls iteration is more commonly called tail recursion. A good optimizing compiler can convert a


  1. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� CoSc 450: Programming Paradigms 03 ���� (define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) ���� a (factorial-product (* a b) (- b 1))))) Inductive case ����������������� ������������������ � �� = � ��������������� � ������������������ �� � �� �� � ��� = � �������������������� � ( a · b ) · ( b − 1 ) ! = � ���� � a · b · ( b − 1 ) ! = � ���� � a · b !

  2. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� CoSc 450: Programming Paradigms 03 ���� (define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) ���� a (factorial-product (* a b) (- b 1))))) Inductive case ����������������� ������������������ � �� = � ��������������� � ������������������ �� � �� �� � ��� = � �������������������� � ( a · b ) · ( b − 1 ) ! = � ���� � a · b · ( b − 1 ) ! = � ���� � a · b !

  3. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� CoSc 450: Programming Paradigms 03 ���� (define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) ���� a (factorial-product (* a b) (- b 1))))) Inductive case ����������������� ������������������ � �� = � ��������������� � ������������������ �� � �� �� � ��� = � �������������������� � ( a · b ) · ( b − 1 ) ! = � ���� � a · b · ( b − 1 ) ! = � ���� � a · b !

  4. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� CoSc 450: Programming Paradigms 03 ���� (define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) ���� a (factorial-product (* a b) (- b 1))))) Inductive case ����������������� ������������������ � �� = � ��������������� � ������������������ �� � �� �� � ��� = � �������������������� � ( a · b ) · ( b − 1 ) ! = � ���� � a · b · ( b − 1 ) ! = � ���� � a · b !

  5. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� CoSc 450: Programming Paradigms 03 ���� (define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) ���� a (factorial-product (* a b) (- b 1))))) Inductive case ����������������� ������������������ � �� = � ��������������� � ������������������ �� � �� �� � ��� = � �������������������� � ( a · b ) · ( b − 1 ) ! = � ���� � a · b · ( b − 1 ) ! = � ���� � a · b !

  6. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� CoSc 450: Programming Paradigms 03 ���� (define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) ���� a (factorial-product (* a b) (- b 1))))) Inductive case ����������������� ������������������ � �� = � ��������������� � ������������������ �� � �� �� � ��� = � �������������������� � ( a · b ) · ( b − 1 ) ! = � ���� � a · b · ( b − 1 ) ! = � ���� � a · b !

  7. CoSc 450: Programming Paradigms 03 The power function > (power 4 5) 1024 (power 4 5) returns 4 to the power 5.

  8. CoSc 450: Programming Paradigms 03 The power function > (power 4 5) 1024 (power 4 5) returns 4 to the power 5.

  9. CoSc 450: Programming Paradigms 03 The power function > (power 4 5) 1024 (power 4 5) returns 4 to the power 5.

  10. CoSc 450: Programming Paradigms 03 Not tail recursion: b e = b · b e − 1

  11. CoSc 450: Programming Paradigms 03 Not tail recursion: b e = b · b e − 1 Tail recursion: ( a ) · b e = ( ab ) · b e − 1

  12. CoSc 450: Programming Paradigms 03 Write power and power-product .

  13. CoSc 450: Programming Paradigms 03 Exponentiation is not associative (3 4 ) 5 6 = 3 (4 5 ) 3 20 6 = 3 1024

  14. CoSc 450: Programming Paradigms 03 Exponentiation is not associative (3 4 ) 5 6 = 3 (4 5 ) 3 20 6 = 3 1024

  15. CoSc 450: Programming Paradigms 03 Fermat numbers F n = 2 (2 n ) + 1 The first few Fermat numbers n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 2 2 + 1 = 5 n = 2 ⇒ (2 2 ) 2 + 1 = 17 n = 3 ⇒ ((2 2 ) 2 ) 2 + 1 = 257

  16. CoSc 450: Programming Paradigms 03 Fermat numbers F n = 2 (2 n ) + 1 The first few Fermat numbers n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 2 2 + 1 = 5 n = 2 ⇒ (2 2 ) 2 + 1 = 17 n = 3 ⇒ ((2 2 ) 2 ) 2 + 1 = 257

  17. CoSc 450: Programming Paradigms 03 Fermat numbers F n = 2 (2 n ) + 1 The first few Fermat numbers n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 2 2 + 1 = 5 n = 2 ⇒ (2 2 ) 2 + 1 = 17 n = 3 ⇒ ((2 2 ) 2 ) 2 + 1 = 257

  18. CoSc 450: Programming Paradigms 03 Fermat numbers F n = 2 (2 n ) + 1 The first few Fermat numbers n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 2 2 + 1 = 5 n = 2 ⇒ (2 2 ) 2 + 1 = 17 n = 3 ⇒ ((2 2 ) 2 ) 2 + 1 = 257

  19. CoSc 450: Programming Paradigms 03 Fermat numbers F n = 2 (2 n ) + 1 The first few Fermat numbers n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 2 2 + 1 = 5 n = 2 ⇒ (2 2 ) 2 + 1 = 17 n = 3 ⇒ ((2 2 ) 2 ) 2 + 1 = 257

  20. CoSc 450: Programming Paradigms 03 Fermat numbers F n = 2 (2 n ) + 1 The first few Fermat numbers n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 2 2 + 1 = 5 n = 2 ⇒ (2 2 ) 2 + 1 = 17 n = 3 ⇒ ((2 2 ) 2 ) 2 + 1 = 257

  21. CoSc 450: Programming Paradigms 03 Fermat numbers n = 3 ⇒ ((2 2 ) 2 ) 2 } +1 = 257 | {z 2 repeatedly squared 3 times (repeatedly-square 2 0) should return 2 2 2 = 4 (repeatedly-square 2 1) should return (2 2 ) 2 = 16 (repeatedly-square 2 2) should return

  22. CoSc 450: Programming Paradigms 03 Fermat numbers n = 3 ⇒ ((2 2 ) 2 ) 2 } +1 = 257 | {z 2 repeatedly squared 3 times (repeatedly-square 2 0) should return 2 2 2 = 4 (repeatedly-square 2 1) should return (2 2 ) 2 = 16 (repeatedly-square 2 2) should return

  23. CoSc 450: Programming Paradigms 03 Fermat numbers n = 3 ⇒ ((2 2 ) 2 ) 2 } +1 = 257 | {z 2 repeatedly squared 3 times (repeatedly-square 2 0) should return 2 2 2 = 4 (repeatedly-square 2 1) should return (2 2 ) 2 = 16 (repeatedly-square 2 2) should return

  24. CoSc 450: Programming Paradigms 03 Fermat numbers n = 3 ⇒ ((2 2 ) 2 ) 2 } +1 = 257 | {z 2 repeatedly squared 3 times (repeatedly-square 2 0) should return 2 2 2 = 4 (repeatedly-square 2 1) should return (2 2 ) 2 = 16 (repeatedly-square 2 2) should return

  25. CoSc 450: Programming Paradigms 03 Fermat numbers n = 3 ⇒ ((2 2 ) 2 ) 2 } +1 = 257 | {z 2 repeatedly squared 3 times (repeatedly-square 2 0) should return 2 2 2 = 4 (repeatedly-square 2 1) should return (2 2 ) 2 = 16 (repeatedly-square 2 2) should return

  26. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� ���� ���� ����������������� ������������������ � �� ��������������� ������������������ �� � �� �� � ��� �������������������� ���� CoSc 450: Programming Paradigms 03 ���� Fermat numbers b ( 2 n ) � ����� 2 n = 2 · 2 n − 1 � = b 2 · ( 2 n − 1 ) � ����� x y · z = ( x y ) z � = ( b 2 ) 2 n − 1 b ������������������ n ������������ b 2 ������������������ n − 1 ������

  27. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� ���� ���� ����������������� ������������������ � �� ��������������� ������������������ �� � �� �� � ��� �������������������� ���� CoSc 450: Programming Paradigms 03 ���� Fermat numbers b ( 2 n ) � ����� 2 n = 2 · 2 n − 1 � = b 2 · ( 2 n − 1 ) � ����� x y · z = ( x y ) z � = ( b 2 ) 2 n − 1 b ������������������ n ������������ b 2 ������������������ n − 1 ������

  28. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� ���� ���� ����������������� ������������������ � �� ��������������� ������������������ �� � �� �� � ��� �������������������� ���� CoSc 450: Programming Paradigms 03 ���� Fermat numbers b ( 2 n ) � ����� 2 n = 2 · 2 n − 1 � = b 2 · ( 2 n − 1 ) � ����� x y · z = ( x y ) z � = ( b 2 ) 2 n − 1 b ������������������ n ������������ b 2 ������������������ n − 1 ������

  29. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� ���� ���� ����������������� ������������������ � �� ��������������� ������������������ �� � �� �� � ��� �������������������� ���� CoSc 450: Programming Paradigms 03 ���� Fermat numbers b ( 2 n ) � ����� 2 n = 2 · 2 n − 1 � = b 2 · ( 2 n − 1 ) � ����� x y · z = ( x y ) z � = ( b 2 ) 2 n − 1 b ������������������ n ������������ b 2 ������������������ n − 1 ������

  30. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� ���� ���� ����������������� ������������������ � �� ��������������� ������������������ �� � �� �� � ��� �������������������� ���� CoSc 450: Programming Paradigms 03 ���� Fermat numbers b ( 2 n ) � ����� 2 n = 2 · 2 n − 1 � = b 2 · ( 2 n − 1 ) � ����� x y · z = ( x y ) z � = ( b 2 ) 2 n − 1 b ������������������ n ������������ b 2 ������������������ n − 1 ������

  31. � ����������������� ������� �� ��������������� ������� �� � ��� �������������������� ���� ���� ����������������� ������������������ � �� ��������������� ������������������ �� � �� �� � ��� �������������������� ���� CoSc 450: Programming Paradigms 03 ���� Fermat numbers b ( 2 n ) � ����� 2 n = 2 · 2 n − 1 � = b 2 · ( 2 n − 1 ) � ����� x y · z = ( x y ) z � = ( b 2 ) 2 n − 1 b ������������������ n ������������ b 2 ������������������ n − 1 ������

  32. CoSc 450: Programming Paradigms 03 Write fermat-number and repeatedly-square .

  33. CoSc 450: Programming Paradigms 03 Perfect number • The sum of its divisors is twice the number, or • The number is equal to the sum of its divisors other than itself.

  34. CoSc 450: Programming Paradigms 03 Perfect number • The sum of its divisors is twice the number, or • The number is equal to the sum of its divisors other than itself. � 2 · 6 = 1 + 2 + 3 + 6 ⇒ ������������� 6 = 1 + 2 + 3

  35. CoSc 450: Programming Paradigms 03 Perfect number • The sum of its divisors is twice the number, or • The number is equal to the sum of its divisors other than itself. � 2 · 6 = 1 + 2 + 3 + 6 ⇒ ������������� 6 = 1 + 2 + 3 There are no known odd perfect numbers.

  36. CoSc 450: Programming Paradigms 03 Suppose you have a function sum-of-divisors such that ( sum-of-divisors 4) returns 1+2+4, ( sum-of-divisors 5) returns 1+5, ( sum-of-divisors 6) returns 1+2+3+6, etc.

  37. CoSc 450: Programming Paradigms 03 Suppose you have a function sum-of-divisors such that ( sum-of-divisors 4) returns 1+2+4, ( sum-of-divisors 5) returns 1+5, ( sum-of-divisors 6) returns 1+2+3+6, etc. Write perfect? such that ( perfect? 4) returns #f , ( perfect? 5) returns #f , ( perfect? 6) returns #t , etc.

  38. CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))

  39. CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend sum-from-plus is defined (if (> low n) inside sum-of-divisors . addend ; no divisors of n are greater than n So, it has access to n . (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))

  40. CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend sum-from-plus is defined (if (> low n) inside sum-of-divisors . addend ; no divisors of n are greater than n So, it has access to n . (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  41. CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend sum-from-plus returns the sum of all the divisors (if (> low n) of n that are greater than or equal to low addend ; no divisors of n are greater than n plus the addend . (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  42. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend .

  43. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend . Is 12 perfect?

  44. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend . Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12

  45. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend . Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12 n = 12

  46. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend . Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12 n = 12 low = 6

  47. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend . Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12 addend = 1+2+3+4 n = 12 low = 6

  48. CoSc 450: Programming Paradigms 03 sum-from-plus returns the sum of all the divisors of n that are greater than or equal to low plus the addend . Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12 addend = 1+2+3+4 n = 12 low = 6 (sum-from-plus 6 10)

  49. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  50. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  51. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  52. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  53. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  54. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  55. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  56. CoSc 450: Programming Paradigms 03 sum-of-divisors (define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend

  57. CoSc 450: Programming Paradigms 03 Hallmarks of pure functional programming • A function returns a value. • There is no call by reference. • All parameters are called by value. • There are no loops. • Repetition is achieved by recursion. • There is no assignment statement.

  58. CoSc 450: Programming Paradigms 03 The Golden Ratio

  59. CoSc 450: Programming Paradigms 03 The Golden Ratio

  60. CoSc 450: Programming Paradigms 03 The Golden Ratio A A

  61. CoSc 450: Programming Paradigms 03 The Golden Ratio A A B

  62. CoSc 450: Programming Paradigms 03 The Golden Ratio B = A + B A A = 1 + B A A 1 = 1 + A/B φ = A/B A B φ = 1 + 1 φ

  63. CoSc 450: Programming Paradigms 03 The Golden Ratio B = A + B A A = 1 + B A A 1 = 1 + A/B φ = A/B A B φ = 1 + 1 φ

  64. CoSc 450: Programming Paradigms 03 The Golden Ratio B = A + B A A = 1 + B A A 1 = 1 + A/B φ = A/B A B φ = 1 + 1 φ

  65. CoSc 450: Programming Paradigms 03 The Golden Ratio B = A + B A A = 1 + B A A 1 = 1 + A/B φ = A/B A B φ = 1 + 1 φ

  66. CoSc 450: Programming Paradigms 03 The Golden Ratio B = A + B A A = 1 + B A A 1 = 1 + A/B φ = A/B A B φ = 1 + 1 φ

  67. CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio

  68. CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio φ 0 = 1 φ 1 = 1 + 1 = 2 φ 0 φ 2 = 1 + 1 = 3 φ 1 2 φ 3 = 1 + 1 = 5 φ 2 3 φ 4 = 1 + 1 = 8 φ 3 5

  69. CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio φ 0 = 1 φ 1 = 1 + 1 = 2 φ 0 φ 2 = 1 + 1 = 3 φ 1 2 φ 3 = 1 + 1 = 5 φ 2 3 φ 4 = 1 + 1 = 8 φ 3 5

  70. CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio φ 0 = 1 φ 1 = 1 + 1 = 2 φ 0 φ 2 = 1 + 1 = 3 φ 1 2 φ 3 = 1 + 1 = 5 φ 2 3 φ 4 = 1 + 1 = 8 φ 3 5

Recommend


More recommend