<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Triangular number - Page 6 - Wikipedia">
<p>
<a accesskey="1" href="page.php?w=triangular_number&amp;p=5">1.Previous</a><br />
<a accesskey="3" href="page.php?w=triangular_number&amp;p=7">3.Next</a>
</p>
<p>about 816 in his <a href="page.php?w=Computus">Computus</a>. An English translation of Dicuil's account is available.</p>

<p>Occasionally it is necessary to compute large triangular numbers where the standard formula <code>t = n*(n+1)/2</code> would suffer <a href="page.php?w=integer_overflow">integer overflow</a> before the final division by 2.  For example,  = 210 < 256, so will fit into an <a href="page.php?w=8-bit_byte">8-bit byte, but not the intermediate product 420.  This can be solved by dividing either  or  by 2 before the multiplication, whichever is even.  This does not require a <a href="page.php?w=conditional_branch">conditional branch</a> if implemented as <code>t = (n|1) * ((n+1)/2)</code>.  If <code>n</code> is odd, the <a href="page.php?w=binary_OR">binary OR</a> operation <code>n|1</code> has no effect, so this is equivalent to <code>t = n * ((n+1)/2)</code> and thus correct.  If <code>n</code> is even, setting the low bit with <code>n|1</code> is the same as adding 1, while the 1 added before the division is <a href="page.php?w=Division_%28mathematics%29">truncated away</a>, so this is equivalent to <code>t = (n+1) * (n/2)</code> and also correct.</></p><p>
<a accesskey="1" href="page.php?w=triangular_number&amp;p=5">1.Previous</a><br />
<a accesskey="3" href="page.php?w=triangular_number&amp;p=7">3.Next</a>
</p>

<do type="prev" label="Search">
        <go href="search.wml"/>
</do>

</card>
</wml>
