e1.13.scm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ; to prove via induction, we evaluate the base case and its iteration
  2. ; our assumption is that:
  3. ;
  4. ; fib(n) = (((1 + sqrt(5))/2)^n - ((1 - sqrt(5))/2)^n) / sqrt(5)
  5. ;
  6. ; for better illustration, define phi and psi to be the following:
  7. ;
  8. ; phi = (1 + sqrt(5)) / 2
  9. ; psi = (1 - sqrt(5)) / 2
  10. ;
  11. ; [ they are identical except for their sign in front of the sqrt(5), this is called congruency ]
  12. ;
  13. ; as such, fib(n) is better defined as:
  14. ;
  15. ; fib(n) = (phi^n - psi^n) / sqrt(5)
  16. ;
  17. ; our previous definition of fibonacci, used for our implementation, looked like this:
  18. ;
  19. ; fib(n) = { 0 .. if n = 0,
  20. ; 1 .. if n = 1,
  21. ; fib(n-1) + fib(n-2) .. if n > 0 }
  22. ;
  23. ; filling our two base cases:
  24. ;
  25. ; fib(0) = (phi^0 - psi^0) / sqrt(5) = (1 - 1) / sqrt(5) = 0 / sqrt(5) = 0
  26. ;
  27. ; [ in this case, we needn't even know what phi and psi are supposed to be ]
  28. ;
  29. ; fib(1) = (phi^1 - psi^1) / sqrt(5)
  30. ; fib(1) = ((1 + sqrt(5)) / 2 - (1 - sqrt(5)) / 2) / sqrt(5)
  31. ; 1/sqrt(5) * fib(1) = 1/2 * (1 + sqrt(5) - (1 - sqrt(5)))
  32. ; 1/sqrt(5) * fib(1) = 1/2 * (1 + sqrt(5) - 1 + sqrt(5))
  33. ; 1/sqrt(5) * fib(1) = 1/2 * (2 * sqrt(5)) = sqrt(5)
  34. ; fib(1) = sqrt(5) / sqrt(5) = 1
  35. ;
  36. ; [ thus our second base case (n = 1) is satisfied ]
  37. ;
  38. ; now for the tough part of the assignment, we need to prove that fib(n + 1) = fib(n) + fib(n - 1)
  39. ; to better suit our definition as well as assumption above, we rewrite to fib(n) = fib(n - 1) + fib(n - 2)
  40. ;
  41. ; we beginn by reducing fib to our assumption formula above, replacing fib by it's constituents phi, psi and other constants:
  42. ;
  43. ; fib(n) = fib(n - 1) + fib(n - 2)
  44. ; fib(n) = (phi^n - psi^n) / sqrt(5)
  45. ;
  46. ; (phi^n - psi^n) / sqrt(5) = ((phi^(n - 1) - psi^(n - 1)) / sqrt(5)) + ((phi^(n - 2) - psi^(n - 2)) / sqrt(5))
  47. ;
  48. ; [ the fib(n - 1) and fib(n - 2) parts can be brought under one divisor ]
  49. ;
  50. ; (phi^n - psi^n) / sqrt(5) = (phi^(n - 1) - psi^(n - 1) + phi^(n - 2) - psi^(n - 2)) / sqrt(5)
  51. ;
  52. ; [ we multiply sqrt(5) to both sides, eliminating our common divisor ]
  53. ;
  54. ; (phi^n - psi^n) / sqrt(5) * sqrt(5) = (phi^(n - 1) - psi^(n - 1) + phi^(n - 2) - psi^(n - 2)) / sqrt(5) * sqrt(5)
  55. ; phi^n - psi^n = phi^(n - 1) - psi^(n - 1) + phi^(n - 2) - psi^(n - 2)
  56. ;
  57. ; [ now as we try to prove equality, the right side has to mimic the left, thus we attempt to bring every phi/psi component on the RHS (right hand side) to the same exponent of the LHS (left hand side) ]
  58. ;
  59. ; phi^n - psi^n = phi^(n - 1) - psi^(n - 1) + phi^(n - 2) - psi^(n - 2)
  60. ;
  61. ; phi^n - psi^n = (phi^(n - 1) * phi) / phi - (psi^(n - 1) * psi) / psi + (phi^(n - 2) * phi^2) / phi^2 - (psi^(n - 2) * psi^2) / psi^2
  62. ;
  63. ; [ as the constituents of fib(n - 1) are closer to our target constituents fib(n), we only complement phi/psi once, whereas for the constituents of fib(n - 2), we complement by phi/psi to the power of 2 respectively ]
  64. ; [ our next possible step is to now simplify to a more workable representation ]
  65. ;
  66. ; phi^n - psi^n = phi^n * (1 / phi + 1 / phi^2) - psi^n * (1 / psi + 1 / psi^2)
  67. ;
  68. ; [ in this step, we simply extracted the common numerator phi/psi respetively, and left the fraction in tact in all othe respects ]
  69. ; [ next up is substituting phi and psi values within the brackets ]
  70. ; [ our reason for doing so, notice how the RHS has *almost* exactly the structure we want? ]
  71. ; [ in theory, if LHS and RHS truly equal each other, meaning, if fib(n) truly equals fib(n - 1) + fib(n - 2), the contents of the brackets should evaluate to 1 ]
  72. ; [ such that the RHS assumes the form psi^n * (1) - psi^n * (1) ]
  73. ; [ this is based on the logical deduction that there cannot be equality otherwise, so whatever the brackets evaluate to determines the outcome of our proof attempt ]
  74. ; [ thus we continue on... ]
  75. ;
  76. ; phi^n - psi^n = phi^n * (1 / (...)^1 + 1 / (...)^2) - psi^n * (1 / (...)^1 + 1 / (...)^2)
  77. ;
  78. ; [ our substitution assumes the above form, which is simplified for readability above, and expanded for real below ]
  79. ;
  80. ; phi^n - psi^n = phi^n * (1 / ((1 + sqrt(5)) / 2)^1 + 1 / ((1 + sqrt(5) / 2)^2)) - psi^n * (1 / ((1 - sqrt(5)) / 2)^1 + 1 / ((1 - sqrt(5) / 2)^2))
  81. ;
  82. ; [ looks bad I know ]
  83. ; [ next up, simplify the double brackets (of the expanded phi/psi components) by multiplying each 1/2 or (1/2)^2 upwards ]
  84. ;
  85. ; phi^n - psi^n = phi^n * (2 / (...)^1 + 4 / (...)^2) - psi^n * (2 / (...)^1 + 4 / (...)^2)
  86. ;
  87. ; [ again, a simplified view to better showcase the transformation, expanded for real below ]
  88. ;
  89. ; phi^n - psi^n = phi^n * (2 / (1 + sqrt(5))^1 + 4 / (1 + sqrt(5))^2) - psi^n * (2 / (1 - sqrt(5))^1 + 4 / (1 - sqrt(5))^2)
  90. ;
  91. ; [ next up we complement the fractions by the respective values needed to unify both fractions under a single one, such that the structures 2/(...)^1 and 4/(...)^2 merge into a single fraction ]
  92. ;
  93. ; phi^n - psi^n = phi^n * ((2 * (1 + sqrt(5))) / (1 + sqrt(5))^2 + 4 / (1 + sqrt(5))^2) - psi^n * ((2 * (1 - sqrt(5))) / (1 - sqrt(5))^2 + 4 / (1 - sqrt(5))^2)
  94. ;
  95. ; [ if you looked carefully, you can see each fraction now has a divisor in the form of (...)^2, further reducing to... ]
  96. ;
  97. ; phi^n - psi^n = phi^n * ((2 * (1 + sqrt(5)) + 4) / (1 + sqrt(5))^2) - psi^n * ((2 * (1 - sqrt(5)) + 4) / (1 - sqrt(5))^2)
  98. ;
  99. ; [ multiplying the numerators... ]
  100. ;
  101. ; phi^n - psi^n = phi^n * ((2 + 2sqrt(5) + 4) / (1 + sqrt(5))^2) - psi^n * ((2 - 2sqrt(5) + 4) / (1 - sqrt(5))^2)
  102. ;
  103. ; [ applying the binomial formula to the divisor ]
  104. ;
  105. ; phi^n - psi^n = phi^n * ((2 + 2sqrt(5) + 4) / (1^2 + 2sqrt(5) + sqrt(5)^2)) - psi^n * ((2 - 2sqrt(5) + 4) / (1^2 - 2sqrt(5) + sqrt(5)^2))
  106. ;
  107. ; phi^n - psi^n = phi^n * ((2 + 2sqrt(5) + 4) / (1 + 2sqrt(5) + 5)) - psi^n * ((2 - 2sqrt(5) + 4) / (1 - 2sqrt(5) + 5))
  108. ;
  109. ; [ we notice the denominator and numerator resemble each other closely, only the summands are different ]
  110. ; [ (2 + ... + 4) for the numerator ]
  111. ; [ (1 + ... + 5) for the denominator ]
  112. ; [ the summands add to 6 in both cases! ]
  113. ; [ understanding this, we can finally make our final reductions ]
  114. ;
  115. ;
  116. ; phi^n - psi^n = phi^n * ((6 + 2sqrt(5)) / (6 + 2sqrt(5))) - psi^n * ((6 - 2sqrt(5)) / (6 - 2sqrt(5)))
  117. ; phi^n - psi^n = phi^n * 1 + psi^n * 1
  118. ; phi^n - psi^n = phi^n - psi^n
  119. ;
  120. ; thus, our proof is established, fib(n) does equal fib(n - 1) + fib(n - 2) after all
  121. ; as every fib(n) can be iteratively deduced from its previous components, and as fib(n + 1) is true as well as the base case, we can assume fib(n) to be true for every n (element of positive integers)
  122. #|
  123. 2019-08-26 it seems this lousy programmer finally figured out how to use block comments! (albeit dialect dependent)
  124. |#