Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.65 KB | None | 0 0
  1. ;; (guess-recursive lon) guesses that the number sequence lon has a
  2. ;; recursive solution and produces the guess.
  3.  
  4. ;; Example:
  5. (check-expect (solution? (guess-recursive '(1 1 2 3 5 8)) '(1 1 2 3 5 8))
  6.               true)
  7. ;; guess-recursive: (listof Int) -> (Nat -> Int)
  8. (define (guess-recursive lon)
  9.   (cond [(>= (length lon) 4)
  10.          (local [(define b
  11.                    (cond
  12.                      [(and (= (second lon) 0)
  13.                            (= (third lon) 0)) 0]
  14.                      [(= (- (sqr (second lon))
  15.                             (* (first lon)(third lon))) 0)
  16.                       0]
  17.                      [(= (second lon) 0)
  18.                       (/ (third lon)(first lon))]
  19.                      [else (/ (- (* (fourth lon)(second lon))
  20.                                  (sqr (third lon)))
  21.                               (- (sqr (second lon))
  22.                                  (* (first lon)(third lon))))]))
  23.                  (define a
  24.                    (cond
  25.                      [(and (= (second lon) 0)
  26.                            (= (third lon) 0)) 0]
  27.                      [(= (second lon) 0)
  28.                       (/ (fourth lon)(third lon))]
  29.                      [else (/ (- (third lon)(* b (first lon)))
  30.                               (second lon))]))
  31.                  (define (fib-recursive i)
  32.                    (cond [(= i 0)(first lon)]
  33.                          [(= i 1)(second lon)]
  34.                          [else
  35.                           (local [(define (fast-fib-recursive
  36.                                            i first-num second-num)
  37.                                     (cond [(= i 0) first-num]
  38.                                           [else (fast-fib-recursive
  39.                                                  (sub1 i) second-num
  40.                                                  (+ (* a second-num)
  41.                                                     (* b first-num)))]))]
  42.                             (fast-fib-recursive
  43.                              i (first lon)(second lon)))]))]            
  44.            (lambda (x)
  45.              (fib-recursive x)))]
  46.         [else (guess-quadratic lon)]))
  47.  
  48. ;; Tests:
  49. (check-expect (guess-recursive '()) 0)
  50. (check-expect (guess-recursive '(1)) 1)
  51. (check-expect (solution? (guess-recursive '(1 2)) '(1 2)) true)
  52. (check-expect (solution? (guess-recursive '(1 2 3)) '(1 2 3 4 5)) true)
  53. (check-expect (solution? (guess-recursive '(2 4 6 10 16 26)) '(2 4 6 10 16 26))
  54.               true)
  55. (check-expect (solution? (guess-recursive '(1 5 7 17 31 65)) '(1 5 7 17 31 65))
  56.               true)
  57. (check-expect (solution? (guess-recursive '(1 0 3 9 36)) '(1 0 3 9 36 135))
  58.               true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement