{"id":4039,"date":"2016-09-30T07:50:31","date_gmt":"2016-09-30T07:50:31","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4039"},"modified":"2016-09-30T07:51:49","modified_gmt":"2016-09-30T07:51:49","slug":"apl-exercises","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2016\/09\/apl-exercises\/","title":{"rendered":"APL Exercises"},"content":{"rendered":"<p>These exercises are designed to introduce APL to a high school senior adept in mathematics. The entire set of APL Exercises has the following sections:<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">Introduction<br \/>\n0. Beginnings<br \/>\n1. Utilities<br \/>\n2. Recursion<br \/>\n3. Proofs<br \/>\n4. Rank Operator<br \/>\n5. Index-Of<\/td>\n<td style=\"text-align: left;\">6. Key Operator<br \/>\n7. Indexing<br \/>\n8. Grade and Sort<br \/>\n9. Power Operator<br \/>\n10. Arithmetic<br \/>\n11. Combinatorial Objects<br \/>\nThe Fine Print<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Sections 2 and 10 are the most interesting ones and are as follows. The full set is too lengthy (and too boring?) to be included here. Some students may be able to make up their own exercises from the list of topics. As well, it may be useful to know what the &#8220;experts&#8221; think are important aspects of APL.<\/p>\n<h2>2. Recursion<\/h2>\n<p><code class=\"language-apl\">\u2395io\u21900<\/code> throughout.<\/p>\n<p><strong>2.0 Factorial<\/strong><\/p>\n<p>Write a function to compute the factorial function on integer <code class=\"language-apl\">\u2375<\/code>, <code class=\"language-apl\">\u2375\u22650<\/code>.<\/p>\n<pre><code class=\"language-apl\">      fac\u00a8 5 6 7 8\r\n120 720 5040 40320\r\n\r\n      n\u21901+?20\r\n      (fac n) = n\u00d7fac n-1\r\n1\r\n      fac 0\r\n1<\/code><\/pre>\n<p><strong>2.1 Fibonacci Numbers<\/strong><\/p>\n<p>Write a function to compute the <code class=\"language-apl\">\u2375<\/code>-th Fibonacci number, <code class=\"language-apl\">\u2375\u22650<\/code>.<\/p>\n<pre><code class=\"language-apl\">      fib\u00a8 5 6 7 8\r\n5 8 13 21    \r\n\r\n      n\u21902+?20\r\n      (fib n) = (fib n-2) + (fib n-1)\r\n1\r\n      fib 0\r\n0\r\n      fib 1\r\n1<\/code><\/pre>\n<p>If the function written above is multiply recursive, write a version which is singly recursive (only one occurrence of <code class=\"language-apl\">\u2207<\/code>). Use <code class=\"language-apl\">cmpx<\/code> to compare the performance of the two functions on the argument <code class=\"language-apl\">35<\/code>.<\/p>\n<p><strong>2.2 Peano Addition<\/strong><\/p>\n<p>Let <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code> be natural numbers (non-negative integers). Write a function <code class=\"language-apl\">padd<\/code> to compute <code class=\"language-apl\">\u237a+\u2375<\/code> without using <code class=\"language-apl\">+<\/code> itself (or <code class=\"language-apl\">-<\/code> or <code class=\"language-apl\">\u00d7<\/code> or <code class=\"language-apl\">\u00f7<\/code> &#8230;). The only functions allowed are comparison to <code class=\"language-apl\">0<\/code> and the functions <code class=\"language-apl\">pre\u2190{\u2375-1}<\/code> and <code class=\"language-apl\">suc\u2190{\u2375+1}<\/code>.<\/p>\n<p><strong>2.3 Peano Multiplication<\/strong><\/p>\n<p>Let <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code> be natural numbers (non-negative integers). Write a function <code class=\"language-apl\">ptimes<\/code> to compute <code class=\"language-apl\">\u237a\u00d7\u2375<\/code> without using <code class=\"language-apl\">\u00d7<\/code> itself (or <code class=\"language-apl\">+<\/code> or <code class=\"language-apl\">-<\/code> or <code class=\"language-apl\">\u00f7<\/code> &#8230;). The only functions allowed are comparison to 0 and the functions <code class=\"language-apl\">pre\u2190{\u2375-1}<\/code> and <code class=\"language-apl\">suc\u2190{\u2375+1}<\/code> and <code class=\"language-apl\">padd<\/code>.<\/p>\n<p><strong>2.4 Binomial Coefficients<\/strong><\/p>\n<p>Write a function to produce the binomal coefficients of order <code class=\"language-apl\">\u2375<\/code>, <code class=\"language-apl\">\u2375\u22650<\/code>.<\/p>\n<pre><code class=\"language-apl\">      bc 0\r\n1\r\n      bc 1\r\n1 1\r\n      bc 2\r\n1 2 1\r\n      bc 3\r\n1 3 3 1\r\n      bc 4\r\n1 4 6 4 1<\/code><\/pre>\n<p><strong>2.5 Cantor Set<\/strong><\/p>\n<p>Write a function to compute the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cantor_set\" target=\"_blank\">Cantor set<\/a> of order <code class=\"language-apl\">\u2375<\/code>, <code class=\"language-apl\">\u2375\u22650<\/code>.<\/p>\n<pre><code class=\"language-apl\">      Cantor 0\r\n1\r\n      Cantor 1\r\n1 0 1\r\n      Cantor 2\r\n1 0 1 0 0 0 1 0 1\r\n      Cantor 3\r\n1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1<\/code><\/pre>\n<p>The classical version of the Cantor set starts with the interval <code class=\"language-apl\">[0,1]<\/code> and at each stage removes the middle third from each remaining subinterval:<\/p>\n<pre><code class=\"language-apl\">[0,1] \u2192\r\n[0,1\/3] \u222a [2\/3,1] \u2192\r\n[0,1\/9] \u222a [2\/9,1\/3] \u222a [2\/3,7\/9] \u222a [8\/9,1] \u2192 \u2026.\r\n\r\n      {(+\u233f\u00f7\u2262)Cantor \u2375}\u00a8 3 5\u2374\u237315\r\n1         0.666667  0.444444   0.296296   0.197531  \r\n0.131687  0.0877915 0.0585277  0.0390184  0.0260123 \r\n0.0173415 0.011561  0.00770735 0.00513823 0.00342549\r\n\r\n      (2\u00f73)*3 5\u2374\u237315\r\n1         0.666667  0.444444   0.296296   0.197531  \r\n0.131687  0.0877915 0.0585277  0.0390184  0.0260123 \r\n0.0173415 0.011561  0.00770735 0.00513823 0.00342549<\/code><\/pre>\n<p><strong>2.6 Sierpinski Carpet<\/strong><\/p>\n<p>Write a function to compute the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sierpinski_carpet\" target=\"_blank\">Sierpinski Carpet<\/a> of order <code class=\"language-apl\">\u2375<\/code>, <code class=\"language-apl\">\u2375\u22650<\/code>.<\/p>\n<pre><code class=\"language-apl\">      SC 0\r\n1\r\n      SC 1\r\n1 1 1\r\n1 0 1\r\n1 1 1\r\n      SC 2\r\n1 1 1 1 1 1 1 1 1\r\n1 0 1 1 0 1 1 0 1\r\n1 1 1 1 1 1 1 1 1\r\n1 1 1 0 0 0 1 1 1\r\n1 0 1 0 0 0 1 0 1\r\n1 1 1 0 0 0 1 1 1\r\n1 1 1 1 1 1 1 1 1\r\n1 0 1 1 0 1 1 0 1\r\n1 1 1 1 1 1 1 1 1\r\n\r\n      ' \u2339'[SC 3]\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\r\n\u2339 \u2339   \u2339 \u2339\u2339 \u2339   \u2339 \u2339\u2339 \u2339   \u2339 \u2339\r\n\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339         \u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339 \u2339\u2339 \u2339\u2339 \u2339         \u2339 \u2339\u2339 \u2339\u2339 \u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339         \u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339\u2339\u2339   \u2339\u2339\u2339         \u2339\u2339\u2339   \u2339\u2339\u2339\r\n\u2339 \u2339   \u2339 \u2339         \u2339 \u2339   \u2339 \u2339\r\n\u2339\u2339\u2339   \u2339\u2339\u2339         \u2339\u2339\u2339   \u2339\u2339\u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339         \u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339 \u2339\u2339 \u2339\u2339 \u2339         \u2339 \u2339\u2339 \u2339\u2339 \u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339         \u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\r\n\u2339 \u2339   \u2339 \u2339\u2339 \u2339   \u2339 \u2339\u2339 \u2339   \u2339 \u2339\r\n\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\u2339\u2339\u2339   \u2339\u2339\u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\r\n\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\u2339 \u2339\r\n\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339\u2339<\/code><\/pre>\n<p>3-D analogs of the Cantor set and the Sierpinski carpet are the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sierpinski_sponge\" target=\"_blank\">Sierpinski sponge<\/a> or the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Menger_sponge\" target=\"_blank\">Menger sponge<\/a>.<\/p>\n<p><strong>2.7 Extended H<\/strong><\/p>\n<p>Write a function for the extend H algorithm for <code class=\"language-apl\">\u2375<\/code>, <code class=\"language-apl\">\u2375\u22650<\/code>, which embeds the complete binary tree of depth <code class=\"language-apl\">\u2375<\/code> on the plane. In <code class=\"language-apl\">\u00d7H \u2375<\/code> there are <code class=\"language-apl\">2*\u2375<\/code> leaves, instances of the letter <code class=\"language-apl\">o<\/code> with exactly one neighbor (or no neighbors for <code class=\"language-apl\">0=\u2375<\/code>). The root is at the center of the matrix.<\/p>\n<pre><code class=\"language-apl\">      xH\u00a8 \u23735\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502o\u2502o-o-o\u2502o   o\u2502o-o-o   o-o-o\u2502o   o   o   o\u2502\r\n\u2502 \u2502     \u2502|   |\u2502  |       |  \u2502|   |   |   |\u2502\r\n\u2502 \u2502     \u2502o-o-o\u2502  o---o---o  \u2502o-o-o   o-o-o\u2502\r\n\u2502 \u2502     \u2502|   |\u2502  |       |  \u2502| | |   | | |\u2502\r\n\u2502 \u2502     \u2502o   o\u2502o-o-o   o-o-o\u2502o | o   o | o\u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502  |       |  \u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502  o---o---o  \u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502  |       |  \u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502o | o   o | o\u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502| | |   | | |\u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502o-o-o   o-o-o\u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502|   |   |   |\u2502\r\n\u2502 \u2502     \u2502     \u2502             \u2502o   o   o   o\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/code><\/pre>\n<p>Write a function that has the same result as <code class=\"language-apl\">xH<\/code> but checks the result using the <code class=\"language-apl\">assert<\/code> utility. For example:<\/p>\n<pre><code class=\"language-apl\">assert\u2190{\u237a\u2190'assertion failure' \u22c4 0\u220a\u2375:\u237a \u2395SIGNAL 8 \u22c4 shy\u21900}\r\n\r\nxH1\u2190{\r\n  h\u2190xH \u2375\r\n  assert 2=\u2374\u2374h:\r\n  assert h\u220a'\u25cb -|':\r\n  assert (\u00af1+2*1+\u2375)=+\/'o'=,h:\r\n  ...\r\n  h\r\n}<\/code><\/pre>\n<p><strong>2.8 Tower of Hanoi<\/strong><\/p>\n<div class=\"fleft\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/6\/60\/Tower_of_Hanoi_4.gif\" alt=\"\" width=\"300\" height=\"117\" \/><\/div>\n<p><em>(By Andr\u00e9 Karwath aka Aka (Own work) [CC BY-SA 2.5 (<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/2.5\" target=\"_blank\">http:\/\/creativecommons.org\/licenses\/by-sa\/2.5<\/a>)], via <a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:Tower_of_Hanoi_4.gif\" target=\"_blank\">Wikimedia Commons<\/a>)<\/em><\/p>\n<div class=\"fclear\"><\/div>\n<p>The Tower of Hanoi problem is to move a set of <code class=\"language-apl\">\u2375<\/code> different-sized disks from one peg to another, moving one disk at a time, using an intermediate peg if necessary. At all times no larger disk may sit on top of a smaller disk.<\/p>\n<p>Write a function Hanoi <code class=\"language-apl\">\u2375<\/code> to solve the problem of moving <code class=\"language-apl\">\u2375<\/code> disks from peg 0 to peg 2. Since it&#8217;s always the disk which is at the top of a peg which is moved, the solution can be stated as a 2-column matrix with column 0 indicating the source peg and column 1 the destination peg.<\/p>\n<pre><code class=\"language-apl\">      Hanoi\u00a8 \u23735\r\n\u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u2502   \u25020 2\u25020 1\u25020 2\u25020 1\u2502\r\n\u2502   \u2502   \u25020 2\u25020 1\u25020 2\u2502\r\n\u2502   \u2502   \u25021 2\u25022 1\u25021 2\u2502\r\n\u2502   \u2502   \u2502   \u25020 2\u25020 1\u2502\r\n\u2502   \u2502   \u2502   \u25021 0\u25022 0\u2502\r\n\u2502   \u2502   \u2502   \u25021 2\u25022 1\u2502\r\n\u2502   \u2502   \u2502   \u25020 2\u25020 1\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25020 2\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25021 2\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25021 0\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25022 0\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25021 2\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25020 1\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25020 2\u2502\r\n\u2502   \u2502   \u2502   \u2502   \u25021 2\u2502\r\n\u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2518<\/code><\/pre>\n<p>Prove that <code class=\"language-apl\">Hanoi \u2375<\/code> has <code class=\"language-apl\">\u00af1+2*\u2375<\/code> rows.<\/p>\n<h2>10. Arithmetic<\/h2>\n<p><code class=\"language-apl\">\u2395io\u21900<\/code> throughout.<\/p>\n<p><strong>10.0 Primality Testing<\/strong><\/p>\n<p>Write a function <code class=\"language-apl\">prime \u2375<\/code> which is 1 or 0 depending on whether non-negative integer <code class=\"language-apl\">\u2375<\/code> is a prime number. For example:<\/p>\n<pre><code class=\"language-apl\">      prime 1\r\n0\r\n      prime 2\r\n1\r\n      prime 9\r\n0\r\n      prime\u00a8 10 10\u2374\u2373100\r\n0 0 1 1 0 1 0 1 0 0\r\n0 1 0 1 0 0 0 1 0 1\r\n0 0 0 1 0 0 0 0 0 1\r\n0 1 0 0 0 0 0 1 0 0\r\n0 1 0 1 0 0 0 1 0 0\r\n0 0 0 1 0 0 0 0 0 1\r\n0 1 0 0 0 0 0 1 0 0\r\n0 1 0 1 0 0 0 0 0 1\r\n0 0 0 1 0 0 0 0 0 1\r\n0 0 0 0 0 0 0 1 0 0<\/code><\/pre>\n<p><strong>10.1 Primes Less Than n<\/strong><\/p>\n<p>Write a function <code class=\"language-apl\">primes \u2375<\/code> using the sieve method which produces all the primes less than <code class=\"language-apl\">\u2375<\/code>. Use <code class=\"language-apl\">cmpx<\/code> to compare the speed of <code class=\"language-apl\">primes n<\/code> and <code class=\"language-apl\">(prime\u00a8\u2373n)\/\u2373n<\/code>. For example:<\/p>\n<pre><code class=\"language-apl\">      primes 7\r\n2 3 5\r\n      primes 50\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47\r\n\r\n      (prime\u00a8\u237350)\/\u237350\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47<\/code><\/pre>\n<p><strong>10.2 Factoring<\/strong><\/p>\n<p>Write a function factor <code class=\"language-apl\">\u2375<\/code> which produces the prime factorization of <code class=\"language-apl\">\u2375<\/code>, such that <code class=\"language-apl\">\u2375 = \u00d7\/factor \u2375<\/code> and <code class=\"language-apl\">\u2227\/ prime\u00a8 factor \u2375<\/code> is 1. For example:<\/p>\n<pre><code class=\"language-apl\">      factor 144\r\n2 2 2 2 3 3\r\n      \u00d7\/ factor 144\r\n144\r\n      \u2227\/ prime\u00a8 factor 144\r\n1\r\n\r\n      factor 1\r\n\r\n      factor 2\r\n2<\/code><\/pre>\n<p><strong>10.3 pco<\/strong><\/p>\n<p>Read about the <code class=\"language-apl\">pco<\/code> function in <a href=\"http:\/\/dfns.dyalog.com\/n_pco.htm\" target=\"_blank\">http:\/\/dfns.dyalog.com\/n_pco.htm<\/a> and experiment with it.<\/p>\n<pre><code class=\"language-apl\">      )copy dfns pco\r\n\r\n      pco \u23737\r\n2 3 5 7 11 13 17\r\n\r\n      1 pco \u237310\r\n0 0 1 1 0 1 0 1 0 0<\/code><\/pre>\n<p>G.H. Hardy states in <em><a href=\"http:\/\/http:\/\/www.math.ualberta.ca\/~mss\/misc\/A%20Mathematician's%20Apology.pdf\" target=\"_blank\">A Mathematician&#8217;s Apology<\/a><\/em> (chapter 14, page 23) that the number of primes less than <code class=\"language-apl\">1e9<\/code> is <code class=\"language-apl\">50847478<\/code>. Use <code class=\"language-apl\">pco<\/code> to check whether this is correct.<\/p>\n<p><strong>10.4 GCD<\/strong><\/p>\n<p>Write a function <code class=\"language-apl\">\u237a gcd \u2375<\/code> to compute the greatest common divisor of non-negative integers <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code> using the Euclidean algorithm.<\/p>\n<p>Write a function <code class=\"language-apl\">\u237a egcd \u2375<\/code> to compute the GCD of <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code> as coefficients of <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code>, such that <code class=\"language-apl\">(\u237a gcd \u2375) = (\u237a,\u2375)+.\u00d7\u237a egcd \u2375<\/code>.<\/p>\n<pre><code class=\"language-apl\">      112 gcd 144\r\n16\r\n      112 egcd 144\r\n4 \u00af3\r\n      112 144 +.\u00d7 112 egcd 144\r\n16<\/code><\/pre>\n<p><strong>10.5 Ring Extension<\/strong><\/p>\n<p><code class=\"language-apl\">Z[\u221ak]<\/code> is the ring of integers <code class=\"language-apl\">Z<\/code> extended with <code class=\"language-apl\">\u221ak<\/code> where <code class=\"language-apl\">k<\/code> is not a perfect square. The elements of <code class=\"language-apl\">Z[\u221ak]<\/code> are ordered pairs <code class=\"language-apl\">(a,b)<\/code> which can be interpreted as <code class=\"language-apl\">a+b\u00d7\u221ak<\/code> (<code class=\"language-apl\">a+b\u00d7k*0.5<\/code>).<\/p>\n<p>Write a d-operator <code class=\"language-apl\">\u237a(\u237a\u237a rplus)\u2375<\/code> which does addition in <code class=\"language-apl\">Z[\u221a\u237a\u237a]<\/code>.<\/p>\n<pre><code class=\"language-apl\">      3 4 (5 rplus) \u00af2 7\r\n1 11<\/code><\/pre>\n<p>Write a d-operator <code class=\"language-apl\">\u237a(\u237a\u237a rtimes)\u2375<\/code> which does multiplication in <code class=\"language-apl\">Z[\u221a\u237a\u237a]<\/code>.<\/p>\n<pre><code class=\"language-apl\">      3 4 (5 rtimes) \u00af2 7\r\n134 13<\/code><\/pre>\n<p>Both of the above can be done using integer operations only.<\/p>\n<p><strong>10.6 Repeated Squaring I<\/strong><\/p>\n<p>Write a function <code class=\"language-apl\">\u237a pow \u2375<\/code> that computes <code class=\"language-apl\">\u237a*\u2375<\/code> using repeated squaring. <code class=\"language-apl\">\u237a<\/code> is a number and <code class=\"language-apl\">\u2375<\/code> a non-negative integer. <em>Hint<\/em>: the binary representation of an integer <code class=\"language-apl\">n<\/code> obtains as <code class=\"language-apl\">2\u22a5\u2363\u00af1\u22a2n<\/code>.<\/p>\n<p><strong>10.7 Repeated Squaring II<\/strong><\/p>\n<p>Write a d-operator <code class=\"language-apl\">\u237a(\u237a\u237a rpow)\u2375<\/code> that computes <code class=\"language-apl\">\u237a<\/code> raised to the power <code class=\"language-apl\">\u2375<\/code> using repeated squaring. <code class=\"language-apl\">\u237a<\/code> is in <code class=\"language-apl\">Z[\u221a\u237a\u237a]<\/code> and <code class=\"language-apl\">\u2375<\/code> is a non-negative integer.<\/p>\n<pre><code class=\"language-apl\">      \u2283 (5 rtimes)\/ 13 \u2374 \u2282 1 1\r\n2134016 954368\r\n\r\n      1 1 (5 rpow) 13\r\n2134016 954368\r\n      1 \u00af1 (5 rpow) 13\r\n2134016 \u00af954368<\/code><\/pre>\n<p>The last two expressions are key steps in an <code class=\"language-apl\">O(\u235fn)<\/code> computation of the <code class=\"language-apl\">n<\/code>-th Fibonacci number using integer operations, using Binet&#8217;s formula:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-4256\" style=\"padding: none!important; border: none!important; text-decoration: none;\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2016\/07\/Binet_formula-300x52.png\" alt=\"\" width=\"300\" height=\"52\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2016\/07\/Binet_formula-300x52.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2016\/07\/Binet_formula-500x88.png 500w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2016\/07\/Binet_formula.png 663w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>These exercises are designed to introduce APL to a high school senior adept in mathematics. The entire set of APL Exercises has the following sections: Introduction 0. Beginnings 1. Utilities 2. Recursion 3. Proofs 4. Rank Operator 5. Index-Of 6. &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2016\/09\/apl-exercises\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-4039","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4039","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=4039"}],"version-history":[{"count":8,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4039\/revisions"}],"predecessor-version":[{"id":4284,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4039\/revisions\/4284"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}