<div dir="ltr"><br>I tried to solve the same problem and came out with a O(log n) solution. It is pretty quick, since I keep track of the steps on a map structure.<br><br>Even though it runs really fast, it hogs on memory just like Steve&#39;s version.<br>
<br>Following is the full source, for your consideration:<br><br><br>======================Euler14.hs======================<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import qualified Data.Map as Map</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import qualified Data.List as List</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">type Table=Map.Map Integer Integer</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">rank::Table-&gt;Integer-&gt;(Table,Integer)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">rank s n= (s&#39;,r)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nxt=if even n then (n `div` 2) else (3*n+1)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r=case (Map.lookup n s) of</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Just a -&gt; a</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nothing-&gt; (1 + (snd $ rank s nxt))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s&#39;=Map.insert n r s</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"><br>search::Integer-&gt;Integer</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">search n = case List.findIndex (\a -&gt; a==ms) sw of</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Just a -&gt; toInteger(a) +1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Nothing -&gt; -1</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; where s=Map.singleton 1 1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw=searchWork s [1..n]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ms=maximum sw</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">searchWork::Table-&gt;[Integer]-&gt;[Integer]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">searchWork s []=[]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">searchWork s (i:is)= r:(searchWork s&#39; is)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (s&#39;,r)=rank s i</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main = do&nbsp; </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; print $ search 1000000</span><br style="font-family: courier new,monospace;">
======================Euler14.hs======================<br><br>
</div>