<div><div><div>&quot;&quot;&quot;</div><div>Mosaics using Python generators...</div><div>by K. Urner</div><div><a href="http://4dsolutions.net">4dsolutions.net</a></div><div>based on a thread on mathfuture:</div><div><a href="http://groups.google.com/group/mathfuture/msg/9d098696c2ea7426?hl=en">http://groups.google.com/group/mathfuture/msg/9d098696c2ea7426?hl=en</a></div>
<div><br></div><div>Just as the Fibonacci sequence generator might be &quot;seeded&quot; by any</div><div>two integers to start off, so might Pascal&#39;s Triangle be seeded with</div><div>any beginning row.</div><div><br>
</div><div>When these sequences are formed into tabular or other areal displays,</div><div>such that information may be gleaned from rows and columns, these</div><div>formats may be called &quot;mosaics&quot; in some of the literature.</div>
<div>&quot;&quot;&quot;</div><div><br></div><div>def fibonacci( a=0, b=1):</div><div>    &quot;&quot;&quot;</div><div>    Generator for a Fibonacci sequence starting from any two integers</div><div><br></div><div>    See:  Online Encyclopedia of Integer Sequences</div>
<div>    <a href="http://oeis.org/A000045">http://oeis.org/A000045</a></div><div>    </div><div>    &gt;&gt;&gt; gen = fibonacci()</div><div>    &gt;&gt;&gt; [next(gen) for i in range(11)]</div><div>    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]</div>
<div>    &gt;&gt;&gt; gen = fibonacci(-1, 2)</div><div>    &gt;&gt;&gt; [next(gen) for i in range(11)]</div><div>    [-1, 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]</div><div>    &quot;&quot;&quot;</div><div><br></div><div>    while True:</div>
<div>        yield a</div><div>        b, a = a + b, b</div><div><br></div><div><br></div><div>def pascal( therow = [1]):</div><div>    &quot;&quot;&quot;</div><div>    Generator for Pascal&#39;s Triangle starting from any top row</div>
<div><br></div><div>    Enter list for top row</div><div><br></div><div>    See:  Number Mosaics by <span class="Apple-style-span" style="font-family: Arial, sans-serif; font-size: 13px; white-space: nowrap; ">Adi R. Kanga</span>  (pg. 29, Fig 20)</div>
<div>    <a href="http://bit.ly/gDmmAo">http://bit.ly/gDmmAo</a></div><div>    </div><div>    &gt;&gt;&gt; gen = pascal([15, 20, 6])</div><div>    &gt;&gt;&gt; next(gen)</div><div>    [15, 20, 6]</div><div>    &gt;&gt;&gt; next(gen)</div>
<div>    [15, 35, 26, 6]</div><div>    &gt;&gt;&gt; next(gen)</div><div>    [15, 50, 61, 32, 6]</div><div>    &gt;&gt;&gt; next(gen)</div><div>    [15, 65, 111, 93, 38, 6]</div><div>    &gt;&gt;&gt; next(gen)</div><div>    [15, 80, 176, 204, 131, 44, 6]</div>
<div>    &gt;&gt;&gt; next(gen)</div><div>    [15, 95, 256, 380, 335, 175, 50, 6]</div><div>    &quot;&quot;&quot;</div><div>    while True:</div><div>        yield therow</div><div>        therow = [ i + j for i, j in zip(therow + [0], [0] + therow) ]</div>
<div>        </div><div>def _test():</div><div>    import doctest</div><div>    doctest.testmod()</div><div><br></div><div>if __name__ == &quot;__main__&quot;:</div><div>        _test()</div><div>    </div></div><div><br>
</div><div>    </div></div>