Popular recipes tagged "integer"http://code.activestate.com/recipes/tags/integer/2016-04-13T11:53:21-07:00ActiveState Code RecipesHow to build dobble as a Mixed Integer program. (Python) 2016-04-13T11:53:21-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/580641-how-to-build-dobble-as-a-mixed-integer-program/ <p style="color: grey"> Python recipe 580641 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/mixed/">mixed</a>, <a href="/recipes/tags/program/">program</a>). </p> <p>A simple script to replicate the cards and symbols for the dobble game.</p> Easy Bit Arrays using Long Integers (Python) 2013-12-01T02:21:14-08:00Mike Sweeneyhttp://code.activestate.com/recipes/users/4177990/http://code.activestate.com/recipes/578777-easy-bit-arrays-using-long-integers/ <p style="color: grey"> Python recipe 578777 by <a href="/recipes/users/4177990/">Mike Sweeney</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/bit/">bit</a>, <a href="/recipes/tags/bitwise/">bitwise</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/long/">long</a>). </p> <p>Some simple techniques for working with long integers as bit arrays. Python support for long integers allows fast bitwise operations on large bit arrays. Bitwise operations on 100 million bit arrays happen in the blink of an eye. They are also fast and compact when saving and loading to disk. Unfortunately bit set, get, append, pop etc are slow so another bit array technique may be preferred.</p> Determine bytes needed to hold integer (Python3) (Python) 2013-11-03T18:35:18-08:00teddy_khttp://code.activestate.com/recipes/users/4187115/http://code.activestate.com/recipes/578766-determine-bytes-needed-to-hold-integer-python3/ <p style="color: grey"> Python recipe 578766 by <a href="/recipes/users/4187115/">teddy_k</a> (<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/integer/">integer</a>). </p> <p>Convenience function to determine the number of bytes needed for a specified integer (split into multiple lines for clarity's sake ... could just as easily be a one-liner).</p> <p>If this is already a defined function within Python, please let me know--a quick web search turned up nothing.</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>&gt;&gt;&gt; n = 1234567*(10**1000) &gt;&gt;&gt; n2 = n*n &gt;&gt;&gt; math.sqrt(n2) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; OverflowError: long int too large to convert to float &gt;&gt;&gt; isqrt(n2) == n True </code></pre> Special Range Function for Different Kinds of Ranges (int, float, character) (Python) 2011-03-30T16:42:47-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577583-special-range-function-for-different-kinds-of-rang/ <p style="color: grey"> Python recipe 577583 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/character/">character</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/special/">special</a>). Revision 3. </p> <p>This module allows the user to create a more verbose set of ranges. Simple character ranges, and float ranges are supported.</p> <p>Supported Ranges:</p> <ul> <li>Basic Integer Ranges</li> <li>Float Ranges (as accurate as a float range can get)</li> <li>Simple character ranges (lowercase to lowercase, uppercase to uppercase, etc.)</li> </ul> <p>It should work in Python 2 and Python 3.</p> <p><strong>If you tested this for speed, or want to test this for speed, please post the results! (And your system specs)</strong></p> <p><strong>Edit:</strong> Found a really silly error of mine when using range instead of xrange in these functions!</p> Number To Words Converter (100 => One Hundred) (Java) 2010-07-18T11:17:52-07:00st0lehttp://code.activestate.com/recipes/users/4174421/http://code.activestate.com/recipes/577312-number-to-words-converter-100-one-hundred/ <p style="color: grey"> Java recipe 577312 by <a href="/recipes/users/4174421/">st0le</a> (<a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/java/">java</a>, <a href="/recipes/tags/number/">number</a>). </p> <p>Converts Integers to Words.</p> Converty decimal/integer number to binary string in Python (Python) 2009-07-17T09:17:32-07:00Vishal Saprehttp://code.activestate.com/recipes/users/4166412/http://code.activestate.com/recipes/576847-converty-decimalinteger-number-to-binary-string-in/ <p style="color: grey"> Python recipe 576847 by <a href="/recipes/users/4166412/">Vishal Sapre</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>This is a very simple look-up based method for converting integer to binary string. In my tests, using 'timeit', its the fastest around. And with increasing value of input decimal number, it surpases any 'run time computation' based method doing the above.</p>