{"id":839,"date":"2011-02-25T00:51:18","date_gmt":"2011-02-25T05:51:18","guid":{"rendered":"http:\/\/jsomers.net\/blog\/?p=839"},"modified":"2014-02-06T21:11:48","modified_gmt":"2014-02-07T02:11:48","slug":"project-euler-problem-191-or-how-i-learned-to-stop-counting-and-love-induction","status":"publish","type":"post","link":"https:\/\/jsomers.net\/blog\/project-euler-problem-191-or-how-i-learned-to-stop-counting-and-love-induction","title":{"rendered":"Project Euler problem 191, or, how I learned to stop counting and love induction"},"content":{"rendered":"<p>Of the 142 <a href=\"http:\/\/projecteuler.net\">Project Euler problems<\/a> I've battled so far, it may just be that <a href=\"http:\/\/projecteuler.net\/index.php?section=problems&amp;id=191\">#191<\/a> has put up the most interesting fight, not because the problem itself was all that difficult or unusual or enlightening, but because the road to solving it---seven to ten hours of work, <a href=\"http:\/\/jsomers.net\/pe-191-work.pdf\">seven sheets of paper<\/a>, several trips to the <a href=\"http:\/\/oeis.org\">online encyclopedia of integer sequences<\/a>, three or four minor insights, and one major breakthrough---was for me almost a paradigm of puzzle-solving, or, closer to the point, a paradigm of how a mathematically crippled mistake-prone dabbler like myself solves puzzles.<\/p>\n\n<p>Here's the problem statement that kicked it all off:<\/p>\n\n<blockquote>\n  <p>A particular school offers cash rewards to children with good attendance and punctuality. If they are absent for three consecutive days or late on more than one occasion then they forfeit their prize.<\/p>\n  \n  <p>During an n-day period a trinary string is formed for each child consisting of L's (late), O's (on time), and A's (absent).<\/p>\n  \n  <p>Although there are eighty-one trinary strings for a 4-day period that can be formed, exactly forty-three strings would lead to a prize:<\/p>\n\n<pre><code>  OOOO OOOA OOOL OOAO OOAA OOAL OOLO OOLA OAOO OAOA\n  OAOL OAAO OAAL OALO OALA OLOO OLOA OLAO OLAA AOOO\n  AOOA AOOL AOAO AOAA AOAL AOLO AOLA AAOO AAOA AAOL\n  AALO AALA ALOO ALOA ALAO ALAA LOOO LOOA LOAO LOAA\n  LAOO LAOA LAAO\n<\/code><\/pre>\n  \n  <p>How many \"prize\" strings exist over a 30-day period?<\/p>\n<\/blockquote>\n\n<p>I encourage you to give it a shot. If you've solved lots of puzzles like this in the past, you'll probably be able to cook up a solution in under an hour. If not, expect to spend an afternoon or two scratching out all sorts of cases and conditions and combinatoric formulations. Either way it should be a great deal of fun.<\/p>\n\n<p>When I first saw the problem I had only two approaches in mind:<\/p>\n\n<ol>\n<li><p>If we let <code>n<\/code> be the number of days, we know that there are 3<sup>n<\/sup> possible strings of <code>O<\/code>s and <code>A<\/code>s and <code>L<\/code>s. To find the number of \"prize\" strings, then, all we have to do is <em>subtract<\/em> from 3<sup>n<\/sup> the number of strings that <em>couldn't<\/em> be winners.<\/p><\/li>\n<li><p>Or, we could go the constructive route and try to only count prize strings.<\/p><\/li>\n<\/ol>\n\n<p>Intuition told me that the first way would be a lot easier, so that's the way I went.<\/p>\n\n<h3>A first crack<\/h3>\n\n<p>The problem statement helps us out by giving the solution to a small case, <code>n<\/code> = 4, which we can use to validate our approach before ramping up to the much trickier case where <code>n<\/code> = 30.<\/p>\n\n<p>What we're trying to do is enumerate all of the strings with either the substring <code>AAA<\/code> or two or more not-necessarily-adjacent <code>L<\/code>s, because these are the strings that are <em>ineligible<\/em> for a prize. We know that we have 3<sup>4<\/sup> = <strong>81<\/strong> possible strings to begin with, and from the problem statement we've been told that <strong>43<\/strong> of these are prize strings, so whatever rules or program we come up with to prune them should wipe out exactly <strong>38<\/strong> (=81 - 43).<\/p>\n\n<p>At a first pass I mustered the following rough (and wildly incorrect) count:<\/p>\n\n<ul>\n<li>There are exactly <strong>two<\/strong> strings containing <code>AAA<\/code>.<\/li>\n<li><strong>Six<\/strong> strings contain two <code>L<\/code>s, a number I arrived at by taking \"4 choose 2\", or 4!\/2!2!<\/li>\n<li><strong>Four<\/strong> strings have three <code>L<\/code>s (by the same argument as the two-<code>L<\/code> case).<\/li>\n<li><strong>One<\/strong> string has four <code>L<\/code>s (ditto).<\/li>\n<\/ul>\n\n<p>Unfortunately that left me with only <strong>thirteen<\/strong> candidates for elimination, far short of required <strong>43<\/strong>. Can you see what I was doing wrong?<\/p>\n\n<p>I left out a whole lot of permutations! Each of the cases above is actually <em>under-specified<\/em>, in the sense that there are multiple strings that could satisfy the given condition. For example, although I said there were only two strings containing <code>AAA<\/code>, there are actually a lot more, because each template---<code>AAA_<\/code> or <code>_AAA<\/code>---can be completed with any one of <code>O<\/code>, <code>L<\/code>, or <code>A<\/code>. Similarly, in cases where we're counting <code>L<\/code>s, we need to remember that the spots <em>without<\/em> <code>L<\/code>s can have either an <code>O<\/code> or an <code>A<\/code>, so that each <code>L<\/code>-template really has 2<sup>k<\/sup> possible fillings, where <code>k<\/code> is just the number of non-<code>L<\/code> spots.<\/p>\n\n<h3>Trouble looms<\/h3>\n\n<p>Are you beginning to smell the complexity?<\/p>\n\n<p>Consider the rule for counting strings with an <code>AAA<\/code> in them. Even if you multiply 2 (for the two templates <code>AAA_<\/code> and <code>_AAA<\/code>) by 3 (because in each template you can have three possible fillings: <code>O<\/code>, <code>L<\/code>, or <code>A<\/code>), you're still incorrectly counting the number of ineligible strings. In particular, you've <em>double-counted<\/em> the case where an <code>A<\/code> fills the blank, because regardless of which template you use, you end up with the same string <code>AAAA<\/code>.<\/p>\n\n<p>At first this might not seem like a big deal, but that little <code>AAAA<\/code> snag and others like it will turn out to be major wrenches in the works, hitches enough to drive me into what an impartial observer might have described as hours of psychotic numerology.<\/p>\n\n<p>The trick is to see what happens when you crank <code>n<\/code> up to 30. Remember what we're trying to do: ultimately, we want the number of eligible prize strings. To wit, we want to first count the <em>non<\/em>-prize strings---the number of strings with three or more <code>A<\/code>s in a row (<code>LOOAAAO...<\/code>, <code>...AAAAO<\/code>, <code>L...AAAAAAOO...O<\/code>, etc.) or two or more <code>L<\/code>s (<code>OAAL...AOLO<\/code>, <code>AOOLLALLL...<\/code>, etc.)---and then subtract that number from 3<sup>30<\/sup>, the number of total possible strings.<\/p>\n\n<p>Suppose we start by trying to count the first type of string, the ones with a bunch of <code>AAA<\/code>s in them. At some point we're going to run into a string that <em>also<\/em> has a few <code>L<\/code>s, and it's important that we somehow keep track of when this happens. If not, we'll end up counting such a string twice, first when we go through looking for <code>AAA<\/code>-style strings and second when we look for strings with multiple <code>L<\/code>s.<\/p>\n\n<p>This is exactly the kind of bookkeeping you don't want to run into when hacking on a combinatorics problem, because it almost always means you've taken a wrong turn. Project Euler is careful to choose problems that <em>can<\/em> be solved elegantly, usually with less than ten lines of code, so it's a major red flag anytime you've got <a href=\"http:\/\/jsomers.net\/pe-191-work.pdf\">pages<\/a> of branching cases and ad hoc \"rules.\"<\/p>\n\n<h3>Divide and conquer?<\/h3>\n\n<p>At this point I probably should have gone back to square one. Instead, though, I hatched what I thought was a workable game plan: I'd avoid the double-counting issue by breaking those non-prize strings into mutually exclusive sets.<\/p>\n\n<p>I'd proceed in three steps:<\/p>\n\n<ol>\n<li>First, count the strings of length 30 with two or more <code>L<\/code>s in them. The non-<code>L<\/code> spots in these strings can filled any whichway. They can even have runs of three or more <code>A<\/code>s in a row---it doesn't matter.<\/li>\n<li>Second, count all of the strings with runs of <code>A<\/code>s in them that <em>also<\/em> don't have any <code>L<\/code>s. So we're just considering the length-30 strings with some mix of <code>O<\/code>s and <code>A<\/code>s, and counting only the ones that include three or more <code>A<\/code>s in a row.<\/li>\n<li>Finally, count all of the strings with three or more <code>A<\/code>s in a row that <em>also<\/em> have <em>exactly one<\/em> <code>L<\/code> in them.<\/li>\n<\/ol>\n\n<p>Do you see how that works? The trick is that we're taking care of all the messy stuff in Step 1, and restricting ourselves in Steps 2 and 3 to small precise collections of ineligible strings---strings where <em>only<\/em> the <code>AAA<\/code>s matter. So our three sets are guaranteed, by construction, not too overlap with one another.<\/p>\n\n<p>So much for the theory---at a certain point one has to actually <em>count<\/em> these things, either with a formula or a simple program. Will my sets be well enough behaved, or will they too have all sorts of quirks and caveats?<\/p>\n\n<p>I tamed the first set rather quickly. It turned out that counting those strings, the ones with with two or more <code>L<\/code>s, was simply a matter of taking a sum from <em>i<\/em> = 2 to <em>i<\/em> = 30 of \"30 choose <em>i<\/em>\" * 2<sup>(30 - <em>i<\/em>)<\/sup>.<\/p>\n\n<p>That first term, \"30 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Combination\">choose<\/a> <em>i<\/em>,\" just counts the number of ways of arranging <em>i<\/em> <code>L<\/code>s in a string with 30 different spots. For <em>i<\/em> = 3 it would count the number of templates that look like <code>__L...L_..._L<\/code> or <code>LL__...L...<\/code>, i.e., templates with exactly three <code>L<\/code>s in them. The second term just counts the number of possible fillings-in of each template: for each spot there are by assumption two choices (<code>O<\/code> or <code>A<\/code>), and each template has 30 - <em>i<\/em> open spots (spots without an <code>L<\/code> in them).<\/p>\n\n<p>The second set is a lot more complicated, but once I thought about it I was immediately reminded of another Project Euler problem, one I'd worked on---without solving---just a few weeks before. It was problem <a href=\"http:\/\/projecteuler.net\/index.php?section=problems&amp;id=114\">#114<\/a>.<\/p>\n\n<h3>Old friends and new<\/h3>\n\n<p>One way to see the correspondence is to imagine the red blocks in problem #114 as <code>A<\/code>s and the black ones as <code>O<\/code>s. If you solve it for n = 30, then, you've implicitly solve my Step 2 above, because you've counted the number of strings with at least one run of three or more <code>A<\/code>s and no <code>L<\/code>s.<\/p>\n\n<p>Or so I thought. What you've actually done is <em>undercounted<\/em> those strings by a huge margin, because you've left out strings where an <code>A<\/code>---a red block---fills one of the spots not already covered by a <em>run<\/em> of <code>A<\/code>s. Problem #114 explicitly requires that the <em>only<\/em> red blocks you can have must appear in runs of three or more. You can't, in other words, have stragglers.<\/p>\n\n<p>You can imagine what I did next. I tried not only to solve #114, a problem I couldn't crack weeks earlier (even with the help of a mathematically-inclined friend who wanted to chip in), but to do so knowing full well it'd only get me partway toward a solution to the problem at hand---thanks to those straggler <code>A<\/code>s. It cost me about two hours of mostly unnecessary work, an expense I would have avoided if I had never seen #114 in the first place. It was an unlikely lesson: drawing on experience can sometimes do more harm than good.<\/p>\n\n<p>After scrapping that angle I decided to do something a bit ignoble. I decided to write some code to exhaustively spit out trinary strings, not of length 30---such a program would take weeks to run---but for small <code>n<\/code>s. For each <code>n<\/code> I'd keep a count of the number of strings that would fall into each of the sets I named above, with a specific focus on the strings in Step 2: strings with zero <code>L<\/code>s and at least one run of three or more <code>A<\/code>s in a row. I'd then take that partial series and plug it into the <a href=\"http:\/\/oeis.org\">Online Encyclopedia of Integer Sequences<\/a>. If I hit a match I might be able to steal a nice clean formula.<\/p>\n\n<p>(I've raved <a href=\"https:\/\/jsomers.net\/blog\/pe-oeis\">elsewhere<\/a> about the OEIS, so I won't do that again here. Suffice it to say it's an invaluable resource and a constant temptation when working on Project Euler problems. One must use it sparingly and as a last resort.)<\/p>\n\n<p>Much to my delight, the very sequence I had generated was chronicled in great detail on the OEIS as sequence <a href=\"http:\/\/oeis.org\/A050231\">#A050231<\/a>, \"the number of n-tosses having a run of 3 or more heads for a fair coin.\"<\/p>\n\n<p>Yoink!<\/p>\n\n<h3>Descent into madness<\/h3>\n\n<p>&#35;A050231 was a huge boon---rather than computing the count in Step 2 above, I could literally copy and paste the correct value from a list someone else had already generated and posted to the OEIS. And with that I was only one term away from a full solution. All that was left was to work through Step 3: counting strings just like the ones in Step 2 that also include a single <code>L<\/code>.<\/p>\n\n<p>When you think about it, it actually seems like a trivial modification could get you from the strings that A050231 is counting to the strings that also include a single <code>L<\/code> (call them <code>L<\/code>-strings). To get the number of <code>L<\/code>-strings, all you have to do is to take each A050231-string---each string of <code>A<\/code>s and <code>O<\/code>s---and multiply it by the number of <code>O<\/code>-spots (as opposed to <code>A<\/code>-spots), those being precisely the spots where you can put the <code>L<\/code>. As an example, for the A050231-string <code>OAAAOAOAAAA<\/code> I would have three corresponding <code>L<\/code>-strings, <code>LAAAOAOAAAA<\/code>, <code>OAAALAOAAAA<\/code>, and <code>OAAAOALAAAA<\/code>, one for each <code>O<\/code>-spot.<\/p>\n\n<p>But since I wasn't man enough to generate A050231 myself, and so didn't understand its structure at anything but the shallowest level, I had no way of doing that simple multiplication, because for each <code>n<\/code> I only knew the number of A050231-strings, without knowing how many <code>O<\/code>-spots each of them contained.<\/p>\n\n<p>So after fumbling around the OEIS for just over an hour trying to find some code or sequence or math that might get me those numbers, I was left seemingly without a way out. But instead of dropping the idea, I decided to work it out in painstaking detail. I decided to work out a sequence that tracked, for each string of length <code>n<\/code>, (a) the number of ways of arranging <code>A<\/code>s, with the proviso that each \"way\" must include a run of three or more, and (b) the number of empty spots (<code>O<\/code>-spots) in each of these arrangements. I'd then look for patterns.<\/p>\n\n<p>You can see the insane work that came out of this insane project by looking at pages 1 and 2 <a href=\"http:\/\/jsomers.net\/pe-191-work.pdf\">in this PDF of scratch paper<\/a>. The \"crazy-person triangles\" that resulted include a list of comma-separated terms for each <code>n<\/code>, where the large numbers specify the number of <code>O<\/code>-spots we're working with (call it <em>k<\/em>) and the subscripts count the number possible arrangements of Type 2 strings---<code>A<\/code>s and <code>O<\/code>s and at least one run of three or more---for each of those <em>k<\/em>s. (I'm pretty sure only the second triangle is correct, which is why I went on to larger <code>n<\/code>s with it. It's been long enough since I did this work, though, that I don't really know what I was thinking or what's actually going on in the first triangle.)<\/p>\n\n<p>I used a helper script to compute those subscripts as <code>n<\/code> and <em>k<\/em> increased. I had to stop after <code>n<\/code> = 14 or so, both because I was running out of space on the page and because the subscripts were taking too long to generate. But even with eleven different rows of numbers, I wasn't able to find any meaningful patterns. I did find all sorts of <em>un<\/em>meaningful patterns, mostly by subtracting sequences of numbers and then recursively subtracting the differences until I hit some sort of constant or steady state; but I'm pretty sure <em>any<\/em> sequence can degrade that way after enough \"diffs of diffs.\" You can see on page 2 of my scrap work that I came upon a few promising generating formulas, both horizontally (as <em>k<\/em> varies) and vertically (as <code>n<\/code> varies), but nothing panned out.<\/p>\n\n<h3>The clouds part<\/h3>\n\n<p>After all that---after <em>all<\/em> that---I decided to scrap the work I'd done so far and take another tack. Because it hit me like a ton of bricks: I should be solving this problem inductively.<\/p>\n\n<p>Think about it this way. Suppose I know how exactly how many prize strings there are for a given <code>n<\/code>. Now increment <code>n<\/code> by one. What changes?<\/p>\n\n<p>Well, there are only three letters that can be added to a given string: <code>O<\/code>, <code>A<\/code>, and <code>L<\/code>. If I add an <code>L<\/code> to a string that already <em>has<\/em> an <code>L<\/code>, all of a sudden that string becomes ineligible; same thing if I add an <code>A<\/code> to a prize string ending in <code>AA<\/code>. If I had an <code>O<\/code> to <em>any<\/em> string, it has no effect. If I add any letter to an ineligible string, it stays ineligible. Etc.<\/p>\n\n<p>It's starting to sound like all we need to do, then, is keep counts of certain string \"types\"---strings ending with <code>AA<\/code>, strings with one <code>L<\/code>, ineligible strings, etc.---and formulate rules for how these counts change each time we increment <code>n<\/code>.<\/p>\n\n<p>That's exactly what I did. After lots of trial and error, I determined that I needed to track <strong>six<\/strong> different types of special strings:<\/p>\n\n<ol>\n<li>Eligible strings ending in <code>AA<\/code>.<\/li>\n<li>Eligible strings with zero <code>L<\/code>s.<\/li>\n<li>Eligible strings ending in <code>A<\/code> (and <em>only<\/em> <code>A<\/code>).<\/li>\n<li>Eligible strings ending in <code>AA<\/code> that also have exactly one <code>L<\/code>.<\/li>\n<li>Eligible strings ending in <code>A<\/code> that also have exactly one <code>L<\/code>.<\/li>\n<li>Eligible strings with one <code>L<\/code> that don't end in <code>A<\/code>.<\/li>\n<\/ol>\n\n<p>It turns out that just this set of strings, call them <em>a<\/em> through <em>f<\/em>, are enough to keep an accurate accounting of prize strings as you increment <code>n<\/code> all the way up to 30.<\/p>\n\n<p>An example should illustrate the method. Take <em>c<\/em>-strings, strings that end in a single <code>A<\/code>. Suppose that when <code>n<\/code> = 3 we have exactly <strong>five<\/strong> of them (we do: they're <code>OOA<\/code>, <code>AOA<\/code>, <code>LOA<\/code>, <code>OLA<\/code>, and <code>ALA<\/code>). What happens to that count (<strong>5<\/strong>) when we increment <code>n<\/code>? Well, we have to consider three cases, one for each of the possible new letters <code>O<\/code>, <code>A<\/code>, and <code>L<\/code>.<\/p>\n\n<p>(Keep in mind that what we're doing in each case is essentially generating 3<sup>n + 1<\/sup> new strings---that is, we're taking each of the 3<sup>n<\/sup> strings we have and appending one letter (<code>O<\/code>, for instance) to all of them. Do that three times and we have our full round of strings for the <code>n<\/code> + 1 case, because 3<sup>n<\/sup> * 3 is 3<sup>n + 1<\/sup>. I say \"essentially\" above because what we're <em>really<\/em> doing is generating 3 * <code>t<\/code> new strings, where <code>t<\/code> is the number of <em>eligible<\/em> (prize) strings for a given <code>n<\/code>. We ignore ineligible strings.<\/p>\n\n<p>Either way, the question is how many of these new <code>-O<\/code> strings and <code>-L<\/code> strings and <code>-A<\/code> strings are of type <em>c<\/em>.)<\/p>\n\n<ul>\n<li>Case <code>O<\/code>: If we add an <code>O<\/code> then all of the five eligible strings that used to end in a single <code>A<\/code> no longer end in a single <code>A<\/code>. And all of the strings that <em>didn't<\/em> end in a single <code>A<\/code> still don't. So the net effect of the <code>O<\/code>s is to decrease our count by 5.<\/li>\n<li>Case <code>L<\/code>: This works the same as the <code>O<\/code>, obviously. A net loss of 5 <em>c<\/em>-strings.<\/li>\n<li>Case <code>A<\/code>: If we add an <code>A<\/code> to one of the five <em>c<\/em>-strings, it'll no longer be a <em>c<\/em>-string. But if we add an <code>A<\/code> to any of the <em>other<\/em> strings it will <em>become<\/em> a <em>c<\/em>-string. So there's a net gain here of <code>t<\/code> - 5.<\/li>\n<\/ul>\n\n<p>So we have a total number of ((<code>t<\/code> - 5) - 5) - 5 <em>c<\/em>-strings in our next round, which for <code>n<\/code> = 3 is ((27 - 5) - 5) - 5 = <strong>12<\/strong>. If you were to enumerate all of the eligible trinary strings for <code>n<\/code> = 4, you'd see that there are in fact exactly 12 of them that end in a single <code>A<\/code>. Our method works!<\/p>\n\n<p>What's nice is that we now have an exact formula that tells us how to get the number of <em>c<\/em>-strings in round <code>n<\/code> + 1 if we know the number of <em>c<\/em>-strings in round <code>n<\/code>. And what's more, that formula depends only on <code>t<\/code>, the total number of eligible prize strings in a given round; <em>c<\/em>; and <em>a<\/em>, where <em>a<\/em> tracks the number of strings ending in <em>two<\/em> <code>A<\/code>s. (Where does that <em>a<\/em> come from? Well, when you add an <code>A<\/code> to a string that already ends in two <code>A<\/code>s, you \"kill\" it (render it ineligible). So you need to know how many <em>a<\/em>-strings are floating around.)<\/p>\n\n<p>Similarly, all the rest of <em>a<\/em>-<em>f<\/em> depend only on <code>t<\/code> and the values of <em>a<\/em>-<em>f<\/em> themselves. That's the sense in which the set <em>a<\/em>-<em>f<\/em> is complete.<\/p>\n\n<p>The upshot of all this is that we can now write an obnoxiously simple program to find <code>t<\/code> when <code>n<\/code> = 30. We simply seed a list of values for <code>n<\/code>, <code>t<\/code>, <em>a<\/em>, <em>b<\/em>, <em>c<\/em>, <em>d<\/em>, <em>e<\/em>, and <em>f<\/em>---<code>[1, 3, 0, 2, 1, 0, 0, 1]<\/code>---and loop over it thirty times, applying our various formulas (like the one for <em>c<\/em>-strings above) with each iteration. Here are the simplified formulas for each element:<\/p>\n\n<ul>\n<li><code>n<\/code> -> <code>n + 1<\/code><\/li>\n<li><code>t<\/code> -> <code>2 * t + b - a<\/code><\/li>\n<li><code>a<\/code> -> <code>c<\/code><\/li>\n<li><code>b<\/code> -> <code>2 * b - a + d<\/code><\/li>\n<li><code>c<\/code> -> <code>t - (a + c)<\/code><\/li>\n<li><code>d<\/code> -> <code>e<\/code><\/li>\n<li><code>e<\/code> -> <code>f<\/code><\/li>\n<li><code>f<\/code> -> <code>t<\/code><\/li>\n<\/ul>\n\n<p>I encourage you to try to derive these yourself. You can consult <a href=\"http:\/\/jsomers.net\/pe-191-work.pdf\">my scratch paper<\/a> for hints.<\/p>\n\n<p>We're left with this wonderful, short, extremely fast solution:<\/p>\n\n<div class=\"highlight highlight-rb\"><pre><span class=\"c1\"># [n, tot, -AA, 0Ls, -A, 1L-AA, 1L-A, 1LnotA]<\/span>\n<span class=\"n\">lst<\/span> <span class=\"o\">=<\/span> <span class=\"o\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"mi\">3<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"o\">]<\/span>\n<span class=\"k\">while<\/span> <span class=\"n\">lst<\/span><span class=\"o\">[<\/span><span class=\"mi\">0<\/span><span class=\"o\">]<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">30<\/span>\n  <span class=\"n\">n<\/span><span class=\"p\">,<\/span> <span class=\"n\">t<\/span><span class=\"p\">,<\/span> <span class=\"n\">a<\/span><span class=\"p\">,<\/span> <span class=\"n\">b<\/span><span class=\"p\">,<\/span> <span class=\"n\">c<\/span><span class=\"p\">,<\/span> <span class=\"n\">d<\/span><span class=\"p\">,<\/span> <span class=\"n\">e<\/span><span class=\"p\">,<\/span> <span class=\"n\">f<\/span> <span class=\"o\">=<\/span> <span class=\"n\">lst<\/span>\n  <span class=\"n\">lst<\/span> <span class=\"o\">=<\/span> <span class=\"o\">[<\/span><span class=\"n\">n<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2<\/span> <span class=\"o\">*<\/span> <span class=\"n\">t<\/span> <span class=\"o\">+<\/span> <span class=\"n\">b<\/span> <span class=\"o\">-<\/span> <span class=\"n\">a<\/span><span class=\"p\">,<\/span> <span class=\"n\">c<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2<\/span> <span class=\"o\">*<\/span> <span class=\"n\">b<\/span> <span class=\"o\">-<\/span> <span class=\"n\">a<\/span> <span class=\"o\">+<\/span> <span class=\"n\">d<\/span><span class=\"p\">,<\/span> <span class=\"n\">t<\/span> <span class=\"o\">-<\/span> <span class=\"p\">(<\/span><span class=\"n\">a<\/span> <span class=\"o\">+<\/span> <span class=\"n\">c<\/span><span class=\"p\">),<\/span> <span class=\"n\">e<\/span><span class=\"p\">,<\/span> <span class=\"n\">f<\/span><span class=\"p\">,<\/span> <span class=\"n\">t<\/span><span class=\"o\">]<\/span>\n<span class=\"k\">end<\/span>\n<span class=\"nb\">p<\/span> <span class=\"n\">lst<\/span><span class=\"o\">[<\/span><span class=\"mi\">1<\/span><span class=\"o\">]<\/span> <span class=\"c1\"># =&gt; 1918080160<\/span>\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Of the 142 Project Euler problems I've battled so far, it may just be that #191 has put up the most interesting fight, not because the problem itself was all that difficult or unusual or enlightening, but because the road to solving it---seven to ten hours of work, seven sheets of paper, several trips to [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-839","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/posts\/839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/comments?post=839"}],"version-history":[{"count":7,"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/posts\/839\/revisions"}],"predecessor-version":[{"id":1098,"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/posts\/839\/revisions\/1098"}],"wp:attachment":[{"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/media?parent=839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/categories?post=839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jsomers.net\/blog\/wp-json\/wp\/v2\/tags?post=839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}