Created
December 6, 2018 19:56
-
-
Save cleoold/969cf2cc73658cf7b79c1062b9228e5f to your computer and use it in GitHub Desktop.
Fibonacci sequence with offsets and weights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Fibonacci with offsets and weights | |
;; F(0) = A | |
;; F(1) = B | |
;; F(n) = xF(n-2) + yF(n-1) | |
(define (weighted-fibonacci n A B x y) | |
(cond | |
[(= n 0) A] | |
[(= n 1) B] | |
[else | |
(local | |
[(define (fib-adder a b counter) | |
(cond [(= n counter) (+ (* x a) (* y b))] | |
[else (fib-adder b (+ (* x a) (* y b)) (add1 counter))]))] | |
(fib-adder A B 2))])) | |
; (build-list 20 | |
; (lambda (n) (weighted-fibonacci n 4 6 2 -1))) | |
; -> '(4 6 2 10 -6 26 -38 90 -166 346 -678 1370 -2726 5466 -10918 21850 -43686 87386 -174758 349530) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment