Just to clarify the issue, I will propose the puzzle:<br><br>There is a single 10 digit number that:<br><br>1) uses all ten digits [0..9], with no repetitions<br>2) the number formed by the first digit (right to left, most significant) is divisible by one<br>
3) the number formed by the first 2 digits (again right to left) is divisible by two<br>4) the number formed by the first 3 digits is divisible by three<br> and so on, until:<br>11) the number formed by the first 10 digits (all!) is by 10<br>
<br>Actually this can be solved by a little logic, but I wanted to give a try on brute force search using haskell.<br><br>I am not looking very large lists, but I was expecting a handful of small lists.<br><br>My algorithm follow these steps:<br>
<br>1) start with an list of empty list ([[]]), call it ds<br>2) I cons each member of [0..9] to ds<br>3) filter using:<br> a) noneRepeated<br> b) (listToNum d) `mod` l == 0, where l is the length of each sublist d (not computed, it is an accumulator that is incremented each time I cons)<br>
4) repeat steps 2-3 until l==10<br><br><br>So, I represent each possible number as a reversed list of its digits... It ran REALLY fast (sub-second).<br><br>So, bragging about Haskell with a Smalltalk-lover friend, by showing him how clean was the code and how easy was to profile, I figured out that I spent 99% on noneRepeated.<br>
<br>After changing to the merge sort version, I have 30% on noneRepeated, 30% on listToNum and 30% on putStrLn. Pretty good! <br><br>Besides, I could brag a little more about Hakell to that specific friend!! ;-)<br><br><br>
Best regards to you all!!<br><br>Rafael<br><br><br>PS: Here is the original search code, with the bad noneRepeated and still using length<br><br><br><br>import Data.List<br><br>digits=[0..9]<br><br>noneRepeated::[Integer]-&gt;Bool<br>
noneRepeated=null.(filter
(&gt;1)).(map length).group.sort<br><br>listToNum::[Integer]-&gt;Integer<br>listToNum
= (foldl (\a x-&gt;10*a+x) 0).reverse<br>
<br>check::[Integer]-&gt;Bool<br>check ds= and [noneRepeated ds,
(listToNum ds) `mod` l==0]<br> where l=fromIntegral $ length ds<br>
<br>nextlevel::[[Integer]]-&gt;[[Integer]]<br><div id=":mp" class="ii gt">nextlevel dss=filter
(check) [d:ds | ds&lt;-dss,d&lt;-digits]<br>
<br>main=do<br> dss&lt;-runlevel 10 0 [[]]<br> print $ map
(listToNum) dss<br> <br>runlevel 0 b dds=return dds<br>runlevel a b
dds=do<br> let dds&#39;=nextlevel dds<br> putStrLn $ &quot;Level &quot;++(show
(b+1))++&quot;: &quot;++(show $ length dds&#39;)++&quot; matches&quot;<br>
print $ map (listToNum) dds&#39;<br> runlevel (a-1) (b+1) dds&#39;<br><br><br></div><br>