[racket] Using future and touch

From: Joe Gilray (jgilray at gmail.com)
Date: Wed Jul 24 20:26:34 EDT 2013

I have a ProjectEuler problem that I wanted to speed up so I thought I
would try to speed it up with future / touch.

I tried the following:

; function that searches for progressive numbers for a given range of b
values
(define (find-progressive-num b-start b-end b-incr lim)
  (for/sum ([b (in-range b-start b-end b-incr)])
    (let loopa ([a (add1 b)] [suma 0])
      (cond
        [(> (gcd a b) 1) (loopa (add1 a) suma)]
        [(>= (* a a a b) lim) suma]
        [else
         (let loopc ([c 1] [sumc 0])
           (define n (+ (* a a a c c b) (* c b b)))
           (cond
             [(>= n lim) (loopa (add1 a) (+ suma sumc))]
             [(square-number? n) (loopc (add1 c) (+ sumc n))]
             [else (loopc (add1 c) sumc)]))]))))

; ProjectEuler problem #141
; n = q * d + r
; q/d = d/r = a/b (where a and b are relatively prime)
; so d = a*r/b and q = a^2 * r / b^2
; since a and b are coprime r must be divisible by b^2 or r = c*b^2
; substituting: d = a*c*b and q = a^2*c
; n = a^3 * c^2 * b + c * b^2
(define (euler141)
  (define lim 10000000000)
  (let ([f1 (future (λ () (find-progressive-num 1 1000 2 lim)))])
    (+ (find-progressive-num 2 1000 2 lim) (touch f1))))

Unfortunately this runs no faster than the sequential version.  I tried
using the future-visualizer but I couldn't understand what it was telling
me (I guess some operation is blocking).  I also tried finer grained
threads (one for each value of b), but that did no better.

Can anyone give me some pointers to successfully using future / touch?

Thanks,
-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130724/b613d51e/attachment.html>

Posted on the users mailing list.