I am seeking ways to make the below minimal code for MaxPower()
function more memory efficient & examples using my code.
Purpose of MaxPower()
: for integers i
and N_remainder
, to return the maximum value of m
such that i^m
divides N_remainder
.
MaxPower()
is only called in the factorise()
function if i
divides N_remainder
Currently the linked code below has the following results for the number to factorise (wrote in standard form):
- 1x10^8 - works fine. Two arrays of results produced and are correct
- 5x10^8 - hangs Linux & OS is totally unresponsive. Computer needs hard restart.
- 1x10^9 - gives memory error in terminal.
Python version used is 2.74 on Linux Mint 17.
I am currently learning python.
def MaxPower(i,N_remainder):
m=1
MxP=1
for n in range (2, N_remainder + 1):
b = i**n
a = N_remainder % b
if a==0:
m = m+1
else:
MxP = m
break
return MxP
Revised code since originally posted:
def MaxPower(i,N_remainder):
m=1
MxP=1
for n in xrange (2, N_remainder + 1):
b = pow(i,n)
a = N_remainder % b
if a==0:
m = m+1
else:
MxP = m
break
return MxP
I am aware of the following (which I haven't tried since new & out of my depth currently);
- Could do more work in compiled C code - converting to "list comprehension"
- Use threading & make sure each thread is deleted after use.