Saturday, January 4, 2014

Le Monde puzzle 847 in Julia


This week I wanted to play around with Julia and exporting the results. I found http://xianblog.wordpress.com/2013/12/29/le-monde-puzzle-847/ to be just the right size to play around with.

Code

A function to check if a triplet has the desired property
In [1]:
function lemonde847(xx)
   a=[xx[1],2]
   b=[xx[2],8]
   c=[xx[3],4]
  sum(kron(c,kron(a,b)))
end
Out[1]:
lemonde847 (generic function with 1 method)
Just a check - the function does indeed result in 1768 for the triplet 6 5 13.
In [2]:
lemonde847([6,5,13])
Out[2]:
1768
Check all combinations of numbers 1 to 60 excluding 2, 4 and 8. These need to be permuted later on. 60 may be a bit much, but how to calculate a bound? After all, imagine a set 1 3 60, that is only 1428. On the other hand, a combination with 59 and 60 should be too large. Hence we make a filter to exclude some. The rows where all three test numbers multiplied are higher than 1768. The remaining combinations are permuted and tested.
In [3]:
lemonde847([1,60,3])
Out[3]:
1428
In [4]:
totest = collect(combinations( symdiff(1:50, [2,4,8]),3))
Out[4]:
16215-element Array{Array{Int64,1},1}:
 [1,3,5]   
 [1,3,6]   
 [1,3,7]   
 [1,3,9]   
 [1,3,10]  
 [1,3,11]  
 [1,3,12]  
 [1,3,13]  
 [1,3,14]  
 [1,3,15]  
 [1,3,16]  
 [1,3,17]  
 [1,3,18]  
 ⋮         
 [45,48,50]
 [45,49,50]
 [46,47,48]
 [46,47,49]
 [46,47,50]
 [46,48,49]
 [46,48,50]
 [46,49,50]
 [47,48,49]
 [47,48,50]
 [47,49,50]
 [48,49,50]
In [5]:
for i in 1:size(totest)[1] 
    if prod(totest[i,])<1768
        test2 = collect(permutations(totest[i,])) 
        for j in 1:6 
            if lemonde847(test2[j,])==1768
               print(test2[j,]')
            end
        end
    end
end
6       5       13
Seems I found the answer; 6 5 13. The only thing left is to export the results. A bit of googling showed http://ipython.org/ipython-doc/stable/interactive/nbconvert.html.

Final Notes

Conversion

In the end I converted the script via:
ipython nbconvert  lemonde847.ipynb
The resulting file was opened in libreoffice and copied pasted into blogger. This gave a bit better results than conversion using
ipython nbconvert --template basic lemonde847.ipynb
and a copy paste of the .html directly in blogger.
Some post editing was done after examination of the preview of the post, but that is fairly normal for me. 

Time usage

The calculation time was minimal. Most of the time was used in understanding Julia syntax, debugging, writing code, writing blog text, installation of pandoc. A lot of these things are one-off, but still, this last sentence took more time than the computation time.

No comments:

Post a Comment