Popular recipes tagged "maths"http://code.activestate.com/recipes/tags/maths/2015-03-05T15:59:30-08:00ActiveState Code RecipesSimple way to find number of perfect square numbers in a range. (Python)
2015-03-05T15:59:30-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/579031-simple-way-to-find-number-of-perfect-square-number/
<p style="color: grey">
Python
recipe 579031
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/complex/">complex</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/interview/">interview</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>).
</p>
<p>The strategy here is not to iterate through the set of possible integer values and check for is_perfect_square()
each time but to translate the upper and lower values to either complex or real space of square numbers.</p>
<pre class="prettyprint"><code> # O(n) complexity
len([x for x in range(0, 100) if x!= 0 and float(math.sqrt(x)).is_integer()])
so if a and b positive then we count the number of integer values between upper and lower sqrt() values
if either a or b are negative then we need to use the complex number space for the sqrt() results. In this case
we are still counting integer values either along the imaginary or real axis, the result is then a simple sum
if both a and b are negative we need to make sure that when walking down the same imaginary axis we dont count
the same inteters twice, in this case we can take the min(a,b) to get max distinct set of integer values.
</code></pre>
Taxi Fare Splitter (Python)
2012-04-27T02:10:56-07:00James Coliinshttp://code.activestate.com/recipes/users/4167379/http://code.activestate.com/recipes/578115-taxi-fare-splitter/
<p style="color: grey">
Python
recipe 578115
by <a href="/recipes/users/4167379/">James Coliins</a>
(<a href="/recipes/tags/barganing/">barganing</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/optimisation/">optimisation</a>).
</p>
<p>A method for allocating costs 'fairly' amongst a group of friends who cooperate to their mutual advantage.</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>
Approximately Equal (Python)
2010-03-17T17:05:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577124-approximately-equal/
<p style="color: grey">
Python
recipe 577124
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/comparisons/">comparisons</a>, <a href="/recipes/tags/distance/">distance</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/numeric/">numeric</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Generic "approximately equal" function for any object type, with customisable error tolerance.</p>
<p>When called with float arguments, approx_equal(x, y[, tol[, rel]) compares x and y numerically, and returns True if y is within either absolute error tol or relative error rel of x, otherwise return False. The function defaults to sensible default values for tol and rel.</p>
<p>For any other pair of objects, approx_equal() looks for a method __approx_equal__ and, if found, calls it with arbitrary optional arguments. This allows types to define their own concept of "close enough".</p>
Floating point range (Python)
2010-02-24T07:12:55-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577068-floating-point-range/
<p style="color: grey">
Python
recipe 577068
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>).
</p>
<p>Generator that produces floats, equivalent to range for integers, minimising rounding errors by using only a single multiplication and addition for each number, and no divisions.</p>
<p>This generator takes an optional argument controlling whether it produces numbers from the open, closed, or half-open interval.</p>
Spline demonstrator (Python)
2009-08-08T15:21:06-07:00James Coliinshttp://code.activestate.com/recipes/users/4167379/http://code.activestate.com/recipes/576871-spline-demonstrator/
<p style="color: grey">
Python
recipe 576871
by <a href="/recipes/users/4167379/">James Coliins</a>
(<a href="/recipes/tags/c_a_d/">c_a_d</a>, <a href="/recipes/tags/maths/">maths</a>).
</p>
<p>An ordered set of points is created using the mouse. A smooth curve(spline) is created following the points with either an open or closed(cyclic) path.</p>