Manipulating Long Ints
I'm brand new to Python (and, by extension Pythonista). In fact, I'm new to coding with an iOS device altogether, so bear with me:
I'm trying to perform operations (like sqrt) on long integers (100 to 200 digits). I get an error saying that the long integer is too long to convert to float.
I think there are packages out there which will get me where I'm trying to go (gmpy?), but I don't know exactly what they are or, indeed, how I would import them in Pythonista and make good use of them.
Omega0 last edited by
How exactly are you doing the square root operation?
I got it to work by doing this:
long_example = 10**200 long_example_root = long_example**0.5
^is a bitwise operator in python
**is the power sign so
10**200is equivalent to 10^200 in normal writing. Also python doesn't have a normal square root function so
long_example**(1/2)is equivalent to a square root operation.
Gmpy is a c module. Pythonista can use anything that is pure python.
Check out forums for pipista, which lets you download packages from pypy, or shellista, which also includes that functionality in a bash like shell.
If you are looking for integer square roots of integers, check out the accepted answer here, which does not require floats.
Thanks, Omega0 and JonB.
I'm actually looking at about 300 digits (roughly 1024 bits, or 2**1024). I'm also able to take the square root of closer to 200 digits using the method described by Omega0.
JonB - I didn't quite follow you. I picked up pipista and shellista (brilliant, by the way), but I wasn't sure if you were advising that I download and install gmpy (or gmpy2, it seems), or telling me that I couldn't because Pythonista won't support it.
I used pipista to download gmpy (pipista.pypi_download('gmpy')), unzipped it in the shell, and then attempted to run setup.py without success.
My point was you cannot use c libraries, other than those that come with pythonista, since Apple doesn't allow dynamic linking.
Pipista will work for oure pythin modules, with pure python dependencies, at least for core functionality...some packages depend on c but only for some minor features which can be stripped out.
Setup.py will never work, since pythinista directory structure is non standard. But since setup.py for pure python generally is just copying the main script somewhere, you can just import or run that main script after you unzip, or copy it to site-packages, etc.
For your specific problem, checkout:
I'll mention the above is an approximation, but you can check whether it is exact by performing the square on the answer
Thanks. The integer square root looks like it will be good enough for what I need.
I'd really like to perform
And it looks like isqrt basically outputs
if the root is not an integer.
So I can just add one, get myself to the right place, and be content.
I'm still surprised there isn't a python based module for performing standard math functions on (very) large integers. Or is there and I just haven't found it?
ccc last edited by
Have you looked thru NumPy which is preinstalled in Pythonista?