Gorgeous! Thanks Justin.<div><br></div><div>To speed it up, change the second "101" to "first"</div><div><br></div><div>To speed it up even a little more, you could do this, but it not quite as pretty:</div>
<div><br></div><div><div>(define (euler4e)</div><div> (for*/fold ([greatest 0])</div><div> ([first (in-range 101 1000)]</div><div> [second (in-range first 1000)])</div><div> (let ([prod (* first second)])</div>
<div> (if (palindromic? prod)</div><div> (max greatest prod)</div><div> greatest))))</div></div><div><br></div><div>Just out of curiosity, have you used for/fold with >1 accum? It seems very powerful.</div>
<div><br></div><div>thanks again,</div><div>-Joe<br><br><div class="gmail_quote">On Sun, Mar 11, 2012 at 1:33 PM, Justin Zamora <span dir="ltr"><<a href="mailto:justin@zamora.com">justin@zamora.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is a great situation to use Racket's advanced list<br>
comprehensions. There is no need to use set!. You can keep track of<br>
the maximum as you loop. Here is my solution.<br>
<br>
; Problem 4<br>
; Find the largest palindrome made from the product of two 3-digit numbers.<br>
; 3-digit numbers are the numbers 100-999<br>
(define (pe-004)<br>
(for*/fold ([greatest 0])<br>
([first (in-range 101 1000)]<br>
[second (in-range 101 1000)]<br>
#:when (palindrome? (* first second)))<br>
(max greatest (* first second))))<br>
<br>
Justin<br>
<div><div class="h5"><br>
On Sun, Mar 11, 2012 at 4:12 PM, Joe Gilray <<a href="mailto:jgilray@gmail.com">jgilray@gmail.com</a>> wrote:<br>
> Hi, I'm redoing the ProjectEuler problems to learn Racket (great fun!).<br>
> Below are three solutions to PE#4. Which is most in the spirit of Racket?<br>
> Of course, I'd love to see solutions that are more idiomatic too. Is there<br>
> a better way that doesn't use "for" at all?<br>
><br>
> Thanks!<br>
> -Joe<br>
><br>
> ; function that turns a positive integer into a list of digits (in reverse<br>
> order)<br>
> ; invoke as (listize 234511) or (set-count (list->set (listize 112342)))<br>
> (define (listize num)<br>
> (if (= num 0)<br>
> '()<br>
> (cons (remainder num 10) (listize (quotient num 10)))))<br>
><br>
> ; function that returns #t if the passed number is palindromic<br>
> ; invoke as (palindromic? n)<br>
> (define (palindromic? n)<br>
> (if (equal? (listize n) (reverse (listize n))) #t #f))<br>
> )<br>
><br>
> ; ProjectEuler problem #4<br>
> (define (euler4a)<br>
> (let ([max 100000])<br>
> (for ([i (build-list 899 (lambda (x) (+ 100 x)))])<br>
> (for ([j (build-list (- 999 i) (lambda (y) (+ i y)))])<br>
> (let ([prod (* i j)])<br>
> (when (palindromic? prod)<br>
> (begin<br>
> (when (> prod max) (set! max prod))<br>
> (printf "~a * ~a = ~a~n" i j prod))))))<br>
> (printf "Max palindromic product is ~a~n" max)))<br>
><br>
> ; ProjectEuler problem #4 using for*<br>
> (define (euler4b)<br>
> (let ([max 100000])<br>
> (for* ([i (build-list 899 (lambda (x) (+ 100 x)))]<br>
> [j (build-list 899 (lambda (y) (+ 100 y)))]<br>
> #:when (and (>= j i) (palindromic? (* i j))))<br>
> (let ([prod (* i j)])<br>
> (when (> prod max) (set! max prod))<br>
> (printf "~a * ~a = ~a~n" i j prod)))<br>
> (printf "Max palindromic product is ~a~n" max)))<br>
><br>
> ; ProjectEuler problem #4 - a mix of 4a and 4b<br>
> (define (euler4c)<br>
> (let ([max 100000])<br>
> (for* ([i (build-list 899 (lambda (x) (+ 100 x)))]<br>
> [j (build-list (- 999 i) (lambda (y) (+ i y)))]<br>
> #:when (palindromic? (* i j)))<br>
> (let ([prod (* i j)])<br>
> (when (> prod max) (set! max prod))<br>
> (printf "~a * ~a = ~a~n" i j prod)))<br>
> (printf "Max palindromic product is ~a~n" max)))<br>
><br>
</div></div>> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
</blockquote></div><br></div>