<div dir="ltr">I have a ProjectEuler problem that I wanted to speed up so I thought I would try to speed it up with future / touch.<div><br></div><div>I tried the following:</div><div><br></div><div><div><font face="courier new, monospace">; function that searches for progressive numbers for a given range of b values</font></div>
<div><font face="courier new, monospace">(define (find-progressive-num b-start b-end b-incr lim)</font></div><div><font face="courier new, monospace"> (for/sum ([b (in-range b-start b-end b-incr)])</font></div><div><font face="courier new, monospace"> (let loopa ([a (add1 b)] [suma 0])</font></div>
<div><font face="courier new, monospace"> (cond</font></div><div><font face="courier new, monospace"> [(> (gcd a b) 1) (loopa (add1 a) suma)]</font></div><div><font face="courier new, monospace"> [(>= (* a a a b) lim) suma]</font></div>
<div><font face="courier new, monospace"> [else</font></div><div><font face="courier new, monospace"> (let loopc ([c 1] [sumc 0])</font></div><div><font face="courier new, monospace"> (define n (+ (* a a a c c b) (* c b b)))</font></div>
<div><font face="courier new, monospace"> (cond</font></div><div><font face="courier new, monospace"> [(>= n lim) (loopa (add1 a) (+ suma sumc))]</font></div><div><font face="courier new, monospace"> [(square-number? n) (loopc (add1 c) (+ sumc n))]</font></div>
<div><font face="courier new, monospace"> [else (loopc (add1 c) sumc)]))]))))</font></div><div><br></div><div><font face="courier new, monospace">; ProjectEuler problem #141</font></div><div><font face="courier new, monospace">; n = q * d + r</font></div>
<div><font face="courier new, monospace">; q/d = d/r = a/b (where a and b are relatively prime)</font></div><div><font face="courier new, monospace">; so d = a*r/b and q = a^2 * r / b^2</font></div><div><font face="courier new, monospace">; since a and b are coprime r must be divisible by b^2 or r = c*b^2</font></div>
<div><font face="courier new, monospace">; substituting: d = a*c*b and q = a^2*c</font></div><div><font face="courier new, monospace">; n = a^3 * c^2 * b + c * b^2</font></div><div><font face="courier new, monospace">(define (euler141)<br>
</font></div><div><font face="courier new, monospace"> (define lim 10000000000)</font></div><div><font face="courier new, monospace"> (let ([f1 (future (ë () (find-progressive-num 1 1000 2 lim)))])</font></div><div><font face="courier new, monospace"> (+ (find-progressive-num 2 1000 2 lim) (touch f1))))</font></div>
</div><div><br></div><div>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.</div>
<div><br></div><div>Can anyone give me some pointers to successfully using future / touch?</div><div><br></div><div>Thanks,</div><div>-Joe</div></div>