<div dir="ltr"><br><div class="gmail_quote">On Mon, Jul 28, 2008 at 12:15 PM, Niels Aan de Brugh <span dir="ltr"><<a href="mailto:nielsadb@gmail.com" target="_blank">nielsadb@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><br></div>
Because you're not providing the right number. :-)<div></div></blockquote><div><br>You know, I realized that this morning after I woke up....ha ha. Sometimes a good night's sleep is all it takes...<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I've tested your code with strictness annotations and it appears to not make a difference. GHC employs several optimization techniques, one of those being strictness analysis, so maybe it is already using a strict, unboxed integer.<br>
</blockquote><div><br>Ah. I figured that I probably didn't have to worry about it manually...<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
The real speed-up (a non-linear one) here is not to re-calculate every sequence over and over again, but keep it in a map/array (as suggested by Rafael and me). I've found some Euler puzzles are impossible to solve without this technique.<br>
<font color="#888888">
<br>
Niels<br>
</font><br>
P.S.: If you're really going for speed, compile (not interpret) the code (using -O -fvia-C, and there's some more stuff in the manual) using the latest greatest version of GHC.<br>
</blockquote></div><br><br>I'll keep this in mind. Right now I'm going for just doing it, but I'll heed your advice for future problems.<br><br><br><br>Finally, what I ended up doing:<br><br><blockquote>f :: Integer -> Integer -> Integer <br>
f acc x <br> | x == 1 = acc <br> | even x = f (acc + 1) (x `div` 2) <br> | otherwise = f (acc + 1) (3 * x + 1)<br><br>g :: Integer -> (Integer, Integer)<br>g x = (f 1 x, x)<br><br>answer = (foldl' max (0,0)) $ map g [1 .. 999999]<br>
<br><br>main = putStrLn( show answer)<br></blockquote><br>Again, not the most efficient, but pretty clean. Runs in just under a minute on my machine. Thanks again!<br></div>