fork download
  1. ; recognizing fibonacci numbers
  2.  
  3. (define (square? n)
  4. (define (isqrt n)
  5. (if (not (and (positive? n) (integer? n)))
  6. (error 'isqrt "must be positive integer")
  7. (let loop ((x n))
  8. (let ((y (quotient (+ x (quotient n x)) 2)))
  9. (if (< y x) (loop y) x)))))
  10. (let ((x (isqrt n)))
  11. (= (* x x) n)))
  12.  
  13. (define (fibonacci? x)
  14. (if (< x 1) #f
  15. (let loop ((prev 1) (fib 1))
  16. (if (< fib x) (loop fib (+ fib prev))
  17. (= fib x)))))
  18.  
  19. (display (fibonacci? 13)) (newline)
  20. (display (fibonacci? 15)) (newline)
  21.  
  22. (define (fibonacci? x)
  23. (let ((5xx (* 5 x x)))
  24. (or (square? (+ 5xx 4))
  25. (square? (- 5xx 4)))))
  26.  
  27. (display (fibonacci? 13)) (newline)
  28. (display (fibonacci? 15)) (newline)
Success #stdin #stdout 0.02s 8344KB
stdin
Standard input is empty
stdout
#t
#f
#t
#f