[plt-scheme] Project Euler #4 solution refactor?

From: Grant Rettke (grettke at acm.org)
Date: Sat Jun 16 23:11:50 EDT 2007

Hi folks,

For project euler #4:

http://projecteuler.net/index.php?section=problems&id=4

I've got a solution and I'm wondering if it can be refactored?

It works, and it makes sense, but it is sort of tricky/ugly to read in
that it logically has an inner and outer loop.

(define psum
    (λ ()
      (let ([top 999] [bot 1])
        (let loop ([x top] [y top] [max 0])
          (let* ([prod (* x y)]
                 [str (number->string prod)]
                 [ispal (string=? str (srfi13:string-reverse str))]
                 [newmax (if (and ispal (> prod max)) prod max)])
            (cond [(> newmax (* x x)) newmax]
                  [(= x y bot) newmax]
                  [(> y bot) (loop x (sub1 y) newmax)]
                  [else (loop (sub1 x) top newmax)]))))))

Posted on the users mailing list.