fork download
  1. ; an integer formula for fibonacci numbers
  2.  
  3. (define (range . args)
  4. (case (length args)
  5. ((1) (range 0 (car args) (if (negative? (car args)) -1 1)))
  6. ((2) (range (car args) (cadr args) (if (< (car args) (cadr args)) 1 -1)))
  7. ((3) (let ((le? (if (negative? (caddr args)) >= <=)))
  8. (let loop ((x(car args)) (xs '()))
  9. (if (le? (cadr args) x)
  10. (reverse xs)
  11. (loop (+ x (caddr args)) (cons x xs))))))
  12. (else (error 'range "unrecognized arguments"))))
  13.  
  14. (define (bitwise-and a b)
  15. (if (or (zero? a) (zero? b)) 0
  16. (+ (* (bitwise-and (floor (/ a 2)) (floor (/ b 2))) 2)
  17. (if (or (even? a) (even? b)) 0 1))))
  18.  
  19. (define (ash int cnt)
  20. (if (negative? cnt)
  21. (let ((n (expt 2 (- cnt))))
  22. (if (negative? int)
  23. (+ -1 (quotient (+ 1 int) n))
  24. (quotient int n)))
  25. (* (expt 2 cnt) int)))
  26.  
  27. (define (fib n)
  28. (bitwise-and
  29. (quotient
  30. (ash 4 (* n (+ 3 n)))
  31. (- (ash 4 (* 2 n)) (ash 2 n) 1))
  32. (- (ash 2 n) 1)))
  33.  
  34. (display (map fib (range 25)))
Success #stdin #stdout 0.05s 8792KB
stdin
Standard input is empty
stdout
(0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025)