How do I print very large value of N%M using Python? - python

For example if I give input 5 and 18. I want to convert 5 to five ones i.e. (11111)%18 = 5. I can do this using print(int(('1'*N))%M)
but I want same with very large numbers i.e. N=338692981500, M=1838828
now my N should be converted in 111111111111111111........1111 (Ntimes)%1838828 = 482531. When I did this I'm getting memory error.
N,M=map(int,input().split())
print(int(('1'*N))%M)
338692981500 1838828
Traceback (most recent call last):
File "testing.py", line 687, in <module>
result=int ('1'*N)%M;
MemoryError

Related

Unable to perform scatter execution in python

I am planning to use MPI scatter and gather for one of my requirement.
I have to include MPI scatter and gather in python to parse a json file and then scatter the data and use that to to filter out the required properties.
I have no issues in parsing the json.
if rank == 0 :
for x in newData1:
for j in range(size):
result[j].append(x)
data = result
print(data)
else:
data = None
chunks = comm.scatter(chunks, root=0)
newData is a list of dictionaries in python.
cntr = fuction_name(chunks)
is the function call to manipulate the scattered data.
when I tried to print the scattered data I am getting below output:
rank 0
chunks 0
rank 1
chunks 1
Below is the error I get when I try to run my program
Traceback (most recent call last):
File "mpi.py", line 192, in <module>
cntr = function_name(chunks)
File "mpi.py", line 60, in make_grid_list
for i in range(len(data)):
TypeError: object of type 'int' has no len()
Traceback (most recent call last):
File "mpi.py", line 192, in <module>
cntr = function_name(chunks)
File "mpi.py", line 60, in make_grid_list
for i in range(len(data)):
TypeError: object of type 'int' has no len()

Why does pd.to_numeric not work with large numbers?

Let's say I have a large number in a string, like '555555555555555555555'. One could choose to convert it to an int, float or even a numpy float:
int('555555555555555555555')
float('555555555555555555555')
np.float('555555555555555555555')
However, when I use the pandas function pd.to_numeric, things go wrong:
pd.to_numeric('555555555555555555555')
With error:
Traceback (most recent call last):
File "pandas/_libs/src/inference.pyx", line 1173, in pandas._libs.lib.maybe_convert_numeric
ValueError: Integer out of range.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\path_to_conda\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-34-6a735441ab7b>", line 1, in <module>
pd.to_numeric('555555555555555555555')
File "C:\path_to_conda\lib\site-packages\pandas\core\tools\numeric.py", line 133, in to_numeric
coerce_numeric=coerce_numeric)
File "pandas/_libs/src/inference.pyx", line 1185, in pandas._libs.lib.maybe_convert_numeric
ValueError: Integer out of range. at position 0
What's going wrong? Why can't pandas to_numeric handle larger values? Are there any use cases why you would use pd.to_numeric instead of functions like np.float?
Because your number is larger that the maximum size of an integer that your system is capable of saving:
In [4]: import sys
In [5]: sys.maxsize
Out[5]: 9223372036854775807
In [6]: 555555555555555555555 > sys.maxsize
Out[6]: True
Here is part of the source code that raises the ValueError:
if not (seen.float_ or as_int in na_values):
if as_int < oINT64_MIN or as_int > oUINT64_MAX:
raise ValueError('Integer out of range.')
As you can see, because your number is not a float it treats it as an integer and checks if the number is in the proper range oINT64_MIN, oUINT64_MAX. If you've passed a float number instead it'd gave you the proper result:
In [9]: pd.to_numeric('555555555555555555555.0')
Out[9]: 5.5555555555555554e+20

OverflowError: range() result has too many items, although it hasn't

I have this for-loop:
for i in range(1000000000, 1000000030):
foo(i)
When I execute it this error is given:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 11, in <module>
for i in range(1000000000, 10000000030):
OverflowError: range() result has too many items.
As far as I know, this range-object should have exactly 30 elements...
Where is the problem?
Edit:
I have removed the extra zero, now I get this:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 12, in <module>
factorizeInefficient(i)
MemoryError
Edit 2:
def factorizeInefficient(n):
teiler = list()
for i in range(n):
if i != 0:
if (n%i)==0:
teiler.append(i)
print teiler
Just found the solution myself: There is a range(n) object in this as well and this causes the memory Error...
An extra question: How did you guys know this was python 2? (Btw you were right...)
Copy/pasting the range() part of your code:
>>> len(range(1000000000, 10000000030))
9000000030
So there are actually about 9 billion elements in the range. Your first argument is presumably missing a zero, or second argument has a zero too many ;-)
count your zeros once again ;) I'd say it's one too much.

pagerank python implementation

I want to convert PageRank MATLAB/Octave implementation to python, but when it comes to:
a=array([[inf]])
last_v = dot(ones(N,1),a)
there is a TypeError.
Traceback (most recent call last):
File "/home/googcheng/page_rank.py", line 18, in <module>
pagerank(0,0)
File "/home/googcheng/page_rank.py", line 14, in pagerank
last_v = dot(ones(N,1),a)
File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 1819, in ones
a = empty(shape, dtype, order)
TypeError: data type not understood
some code https://gist.github.com/3722398
The first argument to ones, the shape, should be a tuple. Change ones(N,1) to ones((N,1)).

Python cdecimal InvalidOperation

I am trying to read financial data and store it. The place I get the financial data from stores the data with incredible precision, however I am only interested in 5 figures after the decimal point. Therefore, I have decided to use t = .quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP) on the Decimal I create, but I keep getting an InvalidOperation exception. Why is this?
>>> import cdecimal
>>> c = cdecimal.getcontext()
>>> c.prec = 5
>>> s = '45.2091000080109'
>>> # s = '0.257585003972054' works!
>>> t = cdecimal.Decimal(s).quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cdecimal.InvalidOperation: [<class 'cdecimal.InvalidOperation'>]
Why is there an invalid operation here? If I change the precision to 7 (or greater), it works. If I set s to be '0.257585003972054' instead of the original value, that also works! What is going on?
Thanks!
decimal version gives a better description of the error:
Python 2.7.2+ (default, Feb 16 2012, 18:47:58)
>>> import decimal
>>> s = '45.2091000080109'
>>> decimal.getcontext().prec = 5
>>> decimal.Decimal(s).quantize(decimal.Decimal('.00001'), rounding=decimal.ROUND_UP)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/decimal.py", line 2464, in quantize
'quantize result has too many digits for current context')
File "/usr/lib/python2.7/decimal.py", line 3866, in _raise_error
raise error(explanation)
decimal.InvalidOperation: quantize result has too many digits for current context
>>>
Docs:
Unlike other operations, if the length of the coefficient after the
quantize operation would be greater than precision, then an
InvalidOperation is signaled. This guarantees that, unless there is an
error condition, the quantized exponent is always equal to that of the
right-hand operand.
But i must confess i don't know what this means.

Categories

Resources