MIT-Scheme-Developers,

I was using mit-scheme 12.1 to solve Project Euler Problem 10. The code I
wrote is pasted below. The call to (solve 2000000) at the REPL does take
more than 10 seconds on my laptop (In fact I always had to CTRL+C after a
while). Once I compile the file and then load the compiled .com file, it
takes less than 2 seconds for the correct answer to show up in the REPL.
Why is there such a huge discrepancy between interpreted code and compiled
code?

Thanks,
Vamsi

(define (is_prime a)
  (define (loop_a b c)
    (cond ((> c a) #t)
          ((= 0 (remainder a b)) #f)
          (else (let* ((d (+ b 2))
                       (e (* d d)))
                  (loop_a d e)))))
  (cond ((< a 2) #f)
        ((< a 4) #t)
        ((even? a) #f)
        (else (loop_a 3 9))))

(define (solve a)
  (define (loop_a b c)
    (cond ((< b a) (let ((d (is_prime b))
                         (e (+ b 2))
                         (f (+ b c)))
                     (cond (d (loop_a e f))
                           (else (loop_a e c)))))
          (else c)))
  (cond ((< a 3) 0)
        (else (loop_a 3 2))))

(solve 2000000)

Reply via email to