<div dir="ltr">Just had some fun using the new math library to solve ProjectEuler problems #108 and #110.<div><br></div><div style>Here's one of my functions:</div><div style><br></div><div style><div><font face="courier new, monospace">; function which solves for the lowest number which has more than the passed number of diophantine reciprocals</font></div>
<div><font face="courier new, monospace">; n is multiplied by 2 to account for symmetrical (equivalent) solutions </font></div><div><font face="courier new, monospace">(define (min-diophantine-recip n)</font></div><div>
<font face="courier new, monospace"> (define limit (* 2 n))</font></div><div><font face="courier new, monospace"> (define (findmax) ; find the largest candidate that meets the criteria</font></div><div><font face="courier new, monospace"> (let maxlp ([p 2] [prod 1])</font></div>
<div><font face="courier new, monospace"> (if (> (num-diophantine-reciprocals (factorize prod)) limit) prod (maxlp (next-prime p) (* p prod)))))</font></div><div><font face="courier new, monospace"> </font></div>
<div><font face="courier new, monospace"> (let lp ([max (findmax)]) ; strip off highest prime factor then search for a smaller possibility</font></div><div><font face="courier new, monospace"> (let* ([lst (factorize max)] [l (take lst (sub1 (length lst)))] [multlim (first (last lst))])</font></div>
<div><font face="courier new, monospace"> (let inclp ([cmult 2])</font></div><div><font face="courier new, monospace"> (if (>= cmult multlim) max ; found the answer</font></div><div><font face="courier new, monospace"> (let ([newmax (* cmult (defactorize l))])</font></div>
<div><font face="courier new, monospace"> (if (> (num-diophantine-reciprocals (factorize newmax)) limit)</font></div><div><font face="courier new, monospace"> (lp newmax) ; found a smaller answer, iterate</font></div>
<div><font face="courier new, monospace"> (inclp (add1 cmult)))))))))</font></div><div><br></div><div style>It utilizes factorize and defactorize from the math library - very useful.</div><div style><br></div>
<div style>I absolutely love writing racket code, the named let especially allows so much freedom of expression. Do functional purists find it easier to use helper functions? Maybe because I come from an "imperative" background, the named let feels more natural to me.</div>
<div style><br></div><div style>In case you want to try out the problem, I'll let you write (num-diophantine-reciprocals lst) yourself, good luck, it's a bit of a challenge.</div><div style><br></div><div style>-joe</div>
</div></div>