Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
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.
Any help?
-
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
Since
^
is a bitwise operator in python**
is the power sign so10**200
is equivalent to 10^200 in normal writing. Also python doesn't have a normal square root function solong_example**0.5
orlong_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.
http://stackoverflow.com/questions/15390807/integer-square-root-in-python -
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:
http://code.activestate.com/recipes/577821-integer-square-root-function/ -
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
ceil(sqrt(N))
And it looks like isqrt basically outputs
floor(sqrt(N))
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?
-
Have you looked thru NumPy which is preinstalled in Pythonista?
http://omz-software.com/pythonista/docs/numpy