Posts Tagged ‘euler’

Euler problem 25

Friday, November 21st, 2008
  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)

Euler Problem 1

Monday, November 3rd, 2008

I’ve decided to do a series of posts with my solutions to the Euler problems. They’ll be tagged with “euler”, and once I figure out how I’ll stop them from showing up on the main page.

This is the first Euler Problem
  1. (define sum
  2.   (lambda (x) (apply + x)))
  3. (define product
  4.   (lambda x (apply * x)))
  5. (define sum-1-to-n
  6.   (lambda (x) (* (/ 1 2) x (+ x 1))))
  7. (define (1+ x) (+ x 1))
  8. (define (-1+ x) (- x 1))
  9.  
  10. (let
  11.     ((smalllist '(3 5 6 9 10 12 15))
  12.      (num15s (quotient 1000 15)))
  13.   "(* num15s 15) -> 990, so we add the last few by hand"
  14.   (+ (* 15
  15.         (length smalllist)
  16.         (sum-1-to-n (-1+ num15s)))
  17.      (* num15s (sum smalllist))
  18.      993 995 996 999))