Practice 1.11
Function f is defined as follows: if n <3, then f (n) = N; otherwise F (n) = f (n-1) + 2 * F (n-2) * 3 * F (n-3 ). write a recursion and an iteration to solve this problem.
Recursion:
(Define (f n) (if (<n 3) N (+ (f (-N 1) (* 2 (f (-N 2 ))) (* 3 (f (-N 3 ))))))
Iteration:
(Define (f n) (if (<n 3) N (F-iter 2 1 0 n) (define (F-iter a B C COUNT) (If (= count 2) A (F-ITER (+ a (* 2 B) (* 3 C) a B (-count 1 ))))
Practice 1.12
Write a Pascal function, input rows and columns, and calculate the value of this row and column in the Pascal triangle.
The answer is clever enough to put the Pascal triangle in a table. the first number of rows in the table starts from the first column of the table, the column col of the row can be vividly described as the sum of the column col of Row-1 and row-1 and column Col, the best thing about this method is that it indicates that the first number of each row and the last number is 1.
(Define (Pascal row col) (cond (= Row 1) 1); the first row value is 1 (= row col) 1 ); the same item value of the row and column is 1 (= col 1) 1); the first column value is 1 (else (+ (Pascal (-Row 1) COL) (Pascal (-Row 1) (-col 1 ))))))
Practice 1.13
It is proved that the number of N in the Fibonacci series fib (n) is the nearest integer to Phi N/root number 5, where Phi = (1 + root number 5)/2.
This question is the answer to prove that the process is very intuitive and clear.
Set Fi = (1 + root number 5)/2, PSI = (1-root number 5)/2
The first step is to prove fib (n) = (Fin-pSIN)/root number 5, using mathematical induction:
When n = 0, FIB (0) = (fi0-psi0)/root number 5 = 0, true
When n = 1, FIB (1) = (fi1-psi1)/root number 5 = 1, true
When promoted to N, FIB (n) = fib (n-1) + fib (n-2) = (Fin-1-psin-1 + FIN-2-psin-2)/root number 5
= (FI + 1) Fin-2-(PSI + 1) psin-2)/root number 5, type 1
Because fi2 = Fi + 1, psi2-psi + 1
So Formula 1 = (Fin-pSIN)/root number 5 proves the first step.
When N tends to be infinite, because psi =-0.618, so pSIN tends to 0, we can prove that fib (n) is the nearest integer of Phi N/root number 5.