Popular Python recipes tagged "root"http://code.activestate.com/recipes/langs/python/tags/root/2014-09-10T02:21:27-07:00ActiveState Code RecipesPerfect Square Checker (Python)
2014-09-10T02:21:27-07:00Ethan Hannhttp://code.activestate.com/recipes/users/4190746/http://code.activestate.com/recipes/578931-perfect-square-checker/
<p style="color: grey">
Python
recipe 578931
by <a href="/recipes/users/4190746/">Ethan Hann</a>
(<a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/checker/">checker</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/square/">square</a>).
</p>
<p>This program will take input from the user and either check if a number is a perfect square or square a number, depending on user's choice.</p>
Polynomial Factoring Using Rational Root Theorem (Python)
2011-12-03T23:51:07-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577974-polynomial-factoring-using-rational-root-theorem/
<p style="color: grey">
Python
recipe 577974
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/poly/">poly</a>, <a href="/recipes/tags/polynomial/">polynomial</a>, <a href="/recipes/tags/polyroots/">polyroots</a>, <a href="/recipes/tags/poly_roots/">poly_roots</a>, <a href="/recipes/tags/rational/">rational</a>, <a href="/recipes/tags/rational_root_theorem/">rational_root_theorem</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/roots/">roots</a>, <a href="/recipes/tags/theorem/">theorem</a>).
</p>
<p>This algorithm factors a polynomial but will only factor it by giving the rational roots. For instance if one of the roots in the polynomial was irrational, the polynomial would not be factored correctly. </p>
Integer square root function (Python)
2011-08-04T05:29:16-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577821-integer-square-root-function/
<p style="color: grey">
Python
recipe 577821
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/isqrt/">isqrt</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/square/">square</a>).
</p>
<p>The <em>integer square root</em> function, or isqrt, is equivalent to floor(sqrt(x)) for non-negative x. For small x, the most convenient way to calculate isqrt is by calling int(x**0.5) or int(math.sqrt(x)), but if x is a large enough integer, the sqrt calculation overflows.</p>
<p>You can calculate the isqrt without any floating point maths, using just pure integer maths, allowing the function to operate with numbers far larger than possible with floats:</p>
<pre class="prettyprint"><code>>>> n = 1234567*(10**1000)
>>> n2 = n*n
>>> math.sqrt(n2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
>>> isqrt(n2) == n
True
</code></pre>