{"id":1782,"date":"2015-02-23T09:12:38","date_gmt":"2015-02-23T09:12:38","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1782"},"modified":"2018-05-21T13:27:56","modified_gmt":"2018-05-21T13:27:56","slug":"solving-the-2014-apl-problem-solving-competition-its-all-right","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/02\/solving-the-2014-apl-problem-solving-competition-its-all-right\/","title":{"rendered":"Solving the 2014 APL Problem Solving Competition \u2013 It&#8217;s All Right"},"content":{"rendered":"<p>This post is the continuation of the series where we examine some of the problems selected for the 2014 <a href=\"http:\/\/www.dyalogaplcompetition.com\/\" target=\"_blank\" rel=\"noopener\">APL Problem Solving Competition<\/a>.<\/p>\n<p>The problems presented in <a href=\"https:\/\/www.dyalog.com\/uploads\/files\/student_competition\/2014_problems_phase1.pdf\" target=\"_blank\" rel=\"noopener\">Phase 1<\/a> of the competition were selected because they could be solved succinctly, generally in a single line of APL code. This makes them well suited for experimentation on <a title=\"TryAPL.org\" href=\"http:\/\/tryapl.org\" target=\"_blank\" rel=\"noopener\">TryAPL.org<\/a>.<\/p>\n<p><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2015\/02\/pyth-e1422913758443.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2079\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2015\/02\/pyth-e1422913758443-255x300.png\" alt=\"pyth\" width=\"255\" height=\"300\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2015\/02\/pyth-e1422913758443-255x300.png 255w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2015\/02\/pyth-e1422913758443.png 369w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/a>Problem 1 of Phase 1, entitled &#8220;It&#8217;s all right&#8221; reads,<\/p>\n<p>&#8220;Write a dfn that takes the length of the legs of a triangle as its left argument, and the length of the hypotenuse as its right argument and returns 1 if the triangle is a right triangle, 0 otherwise.&#8221;<\/p>\n<pre>Test cases:<code class=\"language-apl\">\r\n      3 4 {your_solution} 5\r\n1\r\n      2 3 {your_solution} 4\r\n0\r\n<\/code><\/pre>\n<p>This uses the Pythagorean theorem &#8211; A\u00b2 + B\u00b2 = C\u00b2. It&#8217;s trivial to implement an almost direct translation of this in APL \u2013 in a dfn, using <code class=\"language-apl\">\u237a[1]<\/code> for A, <code class=\"language-apl\">\u237a[2]<\/code> for B and <code class=\"language-apl\">\u2375<\/code> for C yields:<\/p>\n<pre><a title=\"Click to try this on TryAPL.org\" href=\"http:\/\/tryapl.org\/?a=right%u2190%7B%28%28%u237A%5B1%5D*2%29+%28%u237A%5B2%5D*2%29%29%3D%u2375*2%7D&amp;run\" target=\"_blank\" rel=\"noopener\"><code class=\"language-apl\">right\u2190{((\u237a[1]*2)+(\u237a[2]*2))=\u2375*2}<\/code><\/a><\/pre>\n<p>This seems rather clunky though&#8230; what if we consider the problem as &#8220;Are\u00a0the sums of the squares of each argument equal?&#8221; To get the sum of the squares, first we can use the composition <code class=\"language-apl\">*\u22182<\/code> (composing the power function <code class=\"language-apl\">*<\/code> with the constant 2) to mean &#8220;square&#8221; and <code class=\"language-apl\">+\/<\/code> to mean &#8220;sum&#8221;, and combine them in a 2-item function train (also known as an &#8220;atop&#8221;): <code class=\"language-apl\">((+\/)*\u22182)<\/code><\/p>\n<p>then apply this to each argument: \u00a0\u00a0<code class=\"language-apl\">((+\/)*\u22182)\u00a8\u237a \u2375<\/code><\/p>\n<p>and compare the results for equality, resulting in the dfn:<\/p>\n<pre><a title=\"Click to try this on TryAPL.org\" href=\"http:\/\/tryapl.org\/?a=right1%u2190%7B%3D\/%28%28+\/%29*%u22182%29%A8%u237A%20%u2375%7D&amp;run\" target=\"_blank\" rel=\"noopener\"><code class=\"language-apl\">right1\u2190{=\/((+\/)*\u22182)\u00a8\u237a \u2375}<\/code><\/a><\/pre>\n<p>Still this seems clunky to me. Let&#8217;s see&#8230;squaring a number is just multiplying the number by itself, so, if we use the monadic commute operator with multiplication, \u00a0\u00a0<code class=\"language-apl\">\u00d7\u2368<\/code><br \/>\nwe get the equivalent of squaring. Then we can use that function in an inner product with addition to also get &#8220;sum of the squares&#8221;: \u00a0\u00a0<code class=\"language-apl\">+.\u00d7\u2368<\/code><\/p>\n<p>The rest is essentially the same as in <code class=\"language-apl\">right1<\/code> above:<\/p>\n<pre><a title=\"Click to try this on TryAPL.org\" href=\"http:\/\/tryapl.org\/?a=right2%u2190%7B%3D\/+.%D7%u2368%A8%u237A%20%u2375%7D&amp;run\" target=\"_blank\" rel=\"noopener\"><code class=\"language-apl\">right2\u2190{=\/+.\u00d7\u2368\u00a8\u237a \u2375}<\/code><\/a><\/pre>\n<p>All three of these solutions solve the contest problem. The first one, <code class=\"language-apl\">right<\/code>, is not commutative though \u2013 the legs of the triangle must be supplied as the left argument. However, <code class=\"language-apl\">right1<\/code> and <code class=\"language-apl\">right2<\/code> are commutative and the order of arguments is not significant.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is the continuation of the series where we examine some of the problems selected for the 2014 APL Problem Solving Competition. The problems presented in Phase 1 of the competition were selected because they could be solved succinctly, &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/02\/solving-the-2014-apl-problem-solving-competition-its-all-right\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-1782","post","type-post","status-publish","format-standard","hentry","category-student-competition"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1782"}],"version-history":[{"count":16,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1782\/revisions"}],"predecessor-version":[{"id":5180,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1782\/revisions\/5180"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}