Euler problem 25

  1. ;What is the first term in the
  2. ;Fibonacci sequence to contain 1000 digits?
  3. (define (square x) (* x x))
  4.  
  5. (define (pow man exp)
  6.   (cond
  7.     ((= exp 0) 1)
  8.     ((< exp 0) (/ 1 (pow exp (- man))))
  9.     ((even? exp) (square (pow man (/ exp 2))))
  10.     ((odd? exp) (* man (square (pow man (/ (- exp 1) 2)))))
  11.     (else #f)))
  12.  
  13. (define (prob25 digits)
  14.   (let ((big-const (pow 10 (- digits 1))))
  15.     (let fib ((first 1) (second 1) (count 1))
  16.       (if (>= first big-const)
  17.           count
  18.           (begin
  19.             ;(display first) (display " ") (display count) (newline)
  20.             (fib second (+ first second) (+ count 1))
  21.             )))
  22.     )
  23.   )
  24.  
  25. (prob25 1000)

Tags:

Comments are closed.