{"id":494,"date":"2017-08-06T07:49:46","date_gmt":"2017-08-06T07:49:46","guid":{"rendered":"http:\/\/perfectionatic.org\/?p=494"},"modified":"2017-08-06T07:49:46","modified_gmt":"2017-08-06T07:49:46","slug":"solving-the-code-lock-riddle-with-julia","status":"publish","type":"post","link":"https:\/\/perfectionatic.org\/?p=494","title":{"rendered":"Solving the code lock riddle with Julia"},"content":{"rendered":"<p>I came  across a neat math puzzle involving counting the number of unique combinations in a hypothetical lock where digit order does not count. Before you continue, please watch at least the first minute of following video:<br \/>\n<iframe loading=\"lazy\" title=\"Can You Solve The Code Lock Riddle? A GENIUS Math Shortcut\" width=\"740\" height=\"416\" src=\"https:\/\/www.youtube.com\/embed\/8vkQ85kTVm4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>The rest of the video describes two related approaches for carrying out the counting. Often when I run into complex counting problems, I like to do a sanity check using brute force computation to make sure I have not missed anything. <code>Julia<\/code> is fantastic choice for doing such computation. It has <code>C<\/code> like speed, and with an expressiveness that rivals many other high level languages.<\/p>\n<p>Without further ado, here is the Julia code I used to verify my solution the problem.<\/p>\n<pre lang=\"julia\"  line=\"1\">\nfunction unique_combs(n=4)\n    pat_lookup=Dict{String,Bool}()\n    for i=0:10^n-1\n        d=digits(i,10,n) # The digits on an integer in an array with padding\n        ds=d |> sort |> join # putting the digits in a string after sorting\n        get(pat_lookup,ds,false) || (pat_lookup[ds]=true)\n    end\n    println(\"The number of unique digits is $(length(pat_lookup))\")\nend\n<\/pre>\n<p>In <code>line 2<\/code> we create a dictionary that we will be using to check if the number fits a previously seen pattern. The loop starting in <code>line 3<\/code>, examines all possible ordered combinations. The <code>digits<\/code> function in <code>line 4<\/code> takes any integer and generate an array of its constituent digits. We generate the unique digit string in <code>line 5<\/code> using pipes, by first sorting the integer array of digits and then combining them in a string. In <code>line 6<\/code> we check if the pattern of digits was seen before and make use of quick short <a href=\"https:\/\/docs.julialang.org\/en\/stable\/manual\/control-flow\/#Short-Circuit-Evaluation-1\">short-circuit evaluation<\/a> to avoid an <code>if-then<\/code> statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I came across a neat math puzzle involving counting the number of unique combinations in a hypothetical lock where digit order does not count. Before you continue, please watch at least the first minute of following video: The rest of the video describes two related approaches for carrying out the counting. Often when I run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74,73],"tags":[92],"class_list":["post-494","post","type-post","status-publish","format-standard","hentry","category-julia","category-julialang","tag-julia"],"_links":{"self":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":14,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":508,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/508"}],"wp:attachment":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}