{"id":82030,"date":"2008-03-23T10:06:50","date_gmt":"2008-03-23T10:06:50","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73187"},"modified":"2019-05-22T10:13:18","modified_gmt":"2019-05-22T10:13:18","slug":"sequence-table-tricks","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/sequence-table-tricks\/","title":{"rendered":"Sequence Table Tricks"},"content":{"rendered":"<p>Ok, so I am writing about the kinds of things you can do with a sequence table, and I have built the table, I have the typical kinds of things planned (like splitting a string, and for the next section, loading a calendar table) but I wanted to do something interesting.\u00a0 And frankly, sometimes the path to solving a real problem starts with with solving an abstract problem, then reality often tosses you a problem that is really close to this.\u00a0 Admittedly this might not be realistic in my case, but it is possible.<\/p>\n<p>I have just recently watched the Futurama video &#8220;Bender&#8217;s Big Score&#8221; and they have a little math lesson section on the video that was very interesting, but the thing that stuck in my mind was this reference back to a previous episode called the &#8220;<a href=\"http:\/\/www.things.org\/~jym\/y3k\/2ACV06.html\" target=\"_blank\" rel=\"noopener\">Lesser of Two Evils<\/a>&#8220;.\u00a0 The Bender look-alike named Flexo (they are both Bender units) start talking and have the following exchange:<\/p>\n<p><b>Bender: <\/b>Hey, brobot, what&#8217;s your serial number? <br \/>\n<b>Flexo: <\/b>3370318. <br \/>\n<b>Bender: <\/b>No way! Mine&#8217;s 2716057! <br \/>\n<b>Fry: <\/b>I don&#8217;t get it. <br \/>\n<b>Bender: <\/b>We&#8217;re both expressible as the sum of two cubes!<\/p>\n<p>So I figured, the sum of two cubes would be an interesting, and pretty easy abstract utilization of the sequence table.\u00a0 Then I have found this reference also to <a href=\"http:\/\/www.sciencenews.org\/articles\/20020727\/mathtrek.asp\" target=\"_blank\" rel=\"noopener\">&#8220;taxicab&#8221; numbers,<\/a> where the goal is to discover the smallest value that can be expressed as the sum of three cubes in N different ways.<\/p>\n<p>How hard is the query? Turns out, that once you have a sequence table with numbers from 1 to 100000 or so, you can calculate that Taxicab(2) = 1729 very easily (and all of the other numbers that are the sum of two cubes too), and the the sum of two cubes in three different ways also pretty easily (took 3 seconds on my laptop, and that value is 87539319).\u00a0<\/p>\n<p>Here is the code:<\/p>\n<p>declare @level int<\/p>\n<p>set @level = 2 &#8211;sum of two cubes<\/p>\n<p>;with cubes as <br \/>\n(select POWER(i,3) as i3 <br \/>\nfrom\u00a0\u00a0 tools.sequence <br \/>\nwhere\u00a0 i &gt;= 1 and i &lt; 500) &#8211;&lt;&lt;&lt;Vary for performance, and for cheating reasons, max needed value <\/p>\n<p>select c1.i3 + c2.i3 as [sum of 2 cubes in N Ways] <br \/>\nfrom\u00a0\u00a0 cubes as c1 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cross join cubes as c2 <br \/>\nwhere c1.i3 &lt;= c2.i3 <br \/>\ngroup by (c1.i3 + c2.i3) <br \/>\nhaving count(*) = @level <br \/>\norder by 1<\/p>\n<p>Ok, breaking this down the cubes CTE is pretty simple:<\/p>\n<p>(select power(i,3) as i3 <br \/>\nfrom\u00a0\u00a0 tools.sequence\u00a0 &#8211;the table holds 100000 values <br \/>\nwhere\u00a0 i &gt;= 1 and i &lt; 500)<\/p>\n<p>This transforms our values to a table of cubes, so the values would be 1, 8, 27, 64, etc.\u00a0 The query is a bit more interesting. We want the lowest value that meets the criteria that present in a second, so top is used.\u00a0 I sum the two cube values, which I get from cross joining the CTE twice.<\/p>\n<p>select c1.i3 + c2.i3 as [sum of 2 cubes in N Ways] <br \/>\nfrom\u00a0\u00a0 cubes as c1 <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cross join cubes as c2 <br \/>\nwhere c1.i3 &lt;= c2.i3 &#8211;this gets rid of the &#8220;duplicate&#8221; value pairs<\/p>\n<p>The where condition of c1.i3 &lt;= c2.i3 gets rid of the &#8220;duplicate&#8221; value pairs since c1 and c2 have the same values, so without this, for 1729 you would get:<\/p>\n<p>c1.i3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 c2.i3 <br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; <br \/>\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1728 <br \/>\n729\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1000<\/p>\n<p>1000\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 729 <br \/>\n1728\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1<\/p>\n<p>These pairs are the same.\u00a0 I don&#8217;t eliminate equality to allow for the case where both number are equal, because they won&#8217;t be doubled up.\u00a0 With these values:<\/p>\n<p>c1.i3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 c2.i3 <br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; <br \/>\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1728 <br \/>\n729\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1000<\/p>\n<p>You can see that 1729 is the sum of two cubes in two different ways.\u00a0 So, lastly, the question of performance must come up.\u00a0 Reading the articles, it is clear that this is not a terribly easy problem to solve.\u00a0 Values for the sum of three cubes is fairly simple, leaving the sequence values bounded at 500, I get two values in around one second.<\/p>\n<p>[sum of 2 cubes in N Ways] <br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; <br \/>\n87539319 <br \/>\n119824488<\/p>\n<p>Four however, was a &#8220;bit&#8221; more challenging.\u00a0 Knowing the answer from the article, I knew I could bound my numbers using 20000 and get the answer.\u00a0 Using this &#8220;cheat&#8221; on my laptop, I was able to calculate the value of taxicab(4) was 6963472309248 (yea, it only found one) in just 1 hour and 33 minutes, this on a 2.2 Ghz Pentium M laptop with 2 GB of RAM. I tried calculating taxicab(5), but alas, I ran out of space for tempdb (and I had 50 GB available.)\u00a0 For that you had to go up to i being greater than something like 350000&#8230;.<\/p>\n<p>I am thinking that if I turn the CTE into a physical, indexed table I would be able to do this.\u00a0 And for 6, perhaps using the precision of the numeric datatype (38 places!), but not today, I don&#8217;t want to melt my computer trying something this abstract.\u00a0 Well at least not just yet, perhaps when the book is done&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok, so I am writing about the kinds of things you can do with a sequence table, and I have built the table, I have the typical kinds of things planned (like splitting a string, and for the next section, loading a calendar table) but I wanted to do something interesting.\u00a0 And frankly, sometimes the&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82030","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82030"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82030\/revisions"}],"predecessor-version":[{"id":84364,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82030\/revisions\/84364"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82030"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}