{"id":200,"date":"2004-06-09T22:31:51","date_gmt":"2004-06-09T20:31:51","guid":{"rendered":"http:\/\/blogs.bl0rg.net\/neingeist-neu\/2004\/06\/09\/the-nine-nines\/"},"modified":"2006-10-10T03:44:33","modified_gmt":"2006-10-10T01:44:33","slug":"the-nine-nines","status":"publish","type":"post","link":"https:\/\/blogs.bl0rg.net\/neingeist\/2004\/06\/09\/the-nine-nines\/","title":{"rendered":"The Nine Nines"},"content":{"rendered":"<p>Another nice toy problem I found on<br \/>\n<a href=\"http:\/\/www.itasoftware.com\/careers\/programmers-archive.php\"><br \/>\nthe career page of ITA Software<\/a> (which has some more problems for breakfast \ud83d\ude09 is the &#8220;Nine Nines&#8221; problem:<\/p>\n<p><em><br \/>\nCombining nine 9s with any number of the operators +, -, *, \/, (, ) , what is the smallest positive integer that cannot be expressed?<br \/>\n<\/em><\/p>\n<p>I already had a look on <a href=\"http:\/\/www.cathouse.bofh.se\/pipermail\/small-cl-src\/2004-May\/000002.html\"><br \/>\nthe Lisp solution by Luke Gorrie<\/a> by way of the <a href=\"http:\/\/www.hexapodia.net\/mailman\/listinfo\/small-cl-src\">Small-cl-src<\/a><br \/>\nmailing list, a mailing list intended for small Common Lisp source snippets, so my solution might look a little like his. But I tried not to copy his code, maybe that&#8217;s the reason why mine is quite a bit slower (actually, it&#8217;s a lot slower&#8230;). I guess<br \/>\nit&#8217;s because of my excessive use of lists and the pushnew statement instead of<br \/>\na hash table, as he uses one. Maybe the Lisp wizards in the neighbourhood will enlighten me sometime, and I&#8217;ll post a new faster version.<\/p>\n<pre>(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_defun.html\">defun<\/a> nines (n)\r\n\"Gives the solution to the n-nines problem.\"\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/speope_letcm_letst.html\">let<\/a> ((solutions (make-array (1+ n) :initial-element nil)))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/stagenfun_doc_umentationcp.html\">setf<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/acc_aref.html\">aref<\/a> solutions 1) '(9))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_docm_dost.html\">do<\/a> ((k 2 (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_1plcm_1-.html\">1+<\/a> k)))\r\n((<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_eqcm_sleq__lteqcm_gteq.html\">&gt;<\/a> k n))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_format.html\">format<\/a> t \"solve for ~G ~%\" k)\r\n(solve solutions k))\r\n(find-first-missing-integer solutions n)))\r\n\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_defun.html\">defun<\/a> solve (solutions n)\r\n\"Find all possible combinations for n nines, using the already\r\ncalculated combinations of 1 to n-1 nines\"\r\n\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_docm_dost.html\">do<\/a> ((k 1 (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_1plcm_1-.html\">1+<\/a> k)))\r\n((<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_eqcm_sleq__lteqcm_gteq.html\">&gt;<\/a> k (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_slcm_slslcm_slslsl.html\">\/<\/a> n 2)))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_format.html\">format<\/a> t \"inner loop ~G ~G ~%\" n k)\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_dolist.html\">dolist<\/a> (solution (combine (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/acc_aref.html\">aref<\/a> solutions k) (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/acc_aref.html\">aref<\/a> solutions (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_-.html\">-<\/a> n k))))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_pushnew.html\">pushnew<\/a> solution (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/acc_aref.html\">aref<\/a> solutions n)))))\r\n\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_defun.html\">defun<\/a> find-first-missing-integer (solutions n)\r\n\"Find the first missing integer, our solution.\"\r\n\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_docm_dost.html\">do<\/a> ((k 1 (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_1plcm_1-.html\">1+<\/a> k)))\r\n((<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/typspe_not.html\">not<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/typspe_member.html\">member<\/a> k (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/acc_aref.html\">aref<\/a> solutions n))) k)))\r\n\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_defun.html\">defun<\/a> combine (a b)\r\n\"Gives arithmetic combinations of the members of a and b\"\r\n\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/speope_letcm_letst.html\">let<\/a> ((foo nil))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_dolist.html\">dolist<\/a> (x a)\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_dolist.html\">dolist<\/a> (y b)\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_pushnew.html\">pushnew<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_plcm_plplcm_plplpl.html\">+<\/a> x y) foo)\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_pushnew.html\">pushnew<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_abs.html\">abs<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_-.html\">-<\/a> x y)) foo)\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_pushnew.html\">pushnew<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_stcm_ststcm_ststst.html\">*<\/a> x y) foo)\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_whencm_unless.html\">unless<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_zerop.html\">zerop<\/a> y) (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_pushnew.html\">pushnew<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_slcm_slslcm_slslsl.html\">\/<\/a> x y) foo))\r\n(<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_whencm_unless.html\">unless<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/fun_zerop.html\">zerop<\/a> x) (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/mac_pushnew.html\">pushnew<\/a> (<a href=\"http:\/\/www.lisp.org\/HyperSpec\/Body\/var_slcm_slslcm_slslsl.html\">\/<\/a> y x) foo))))\r\nfoo))<\/pre>\n<p>Other solutions which I found on the web: Luke&#8217;s <a href=\"http:\/\/www.bluetail.com\/~luke\/misc\/haskell\/NineNines.hs\">Haskell <\/a>version, Karl Zilles&#8217; <a href=\"http:\/\/www.zilles.com\/karl\/ninenines.ml\">port to ML<\/a> and Nicolas Minutillo&#8217;s <a href=\"http:\/\/minutillo.com\/thinkdothink\/nn.pdf\">solution in Java (22 pages!)<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another nice toy problem I found on the career page of ITA Software (which has some more problems for breakfast \ud83d\ude09 is the &#8220;Nine Nines&#8221; problem: Combining nine 9s with any number of the operators +, -, *, \/, (, ) , what is the smallest positive integer that cannot be expressed? I already had [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/posts\/200"}],"collection":[{"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":0,"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.bl0rg.net\/neingeist\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}