How to get decimal form of largest known prime?
Claudio Grondi
claudio.grondi at freenet.de
Sun Jun 13 17:47:28 EDT 2004
>>"Tim Peters" <tim.one at comcast.net>
>>The fastest pure-Python solution I've found consumed about 20 CPU minutes
I have already used such approach (see code below), but was not able to
get any improvement, even the opposite because of increase of time needed
forcalculation for each next iteration step.
Is my code not well designed? What is the difference to yours?
(consider please, that I started with Python some weeks ago
and just trying to check out the advantages and the limits)
Claudio
P.S. Here the code:
intExponent = 24036583
intBaseOfNumericSystem = 10**5000
dctPrimeAsIntDigits = {}
import math
intMaxIterExponent = int('%.0f'%( \
round(math.log(intBaseOfNumericSystem,2) - 0.501),))
if(intMaxIterExponent > intExponent):
intMaxIterExponent = intExponent
#: if
intDigitIndx = 0
dctPrimeAsIntDigits[intDigitIndx] = 2**intMaxIterExponent
intShiftValue = 0
intExponentCounter = intMaxIterExponent \
+ intMaxIterExponent
import time
floatBegTime = time.clock()
floatCurrTime = time.clock()
while(intExponentCounter < intExponent):
# loop over the exponent
print intExponentCounter \
, time.clock() - floatBegTime \
, time.clock() - floatCurrTime
floatCurrTime = time.clock()
intDigitIndx = 0
intShiftValue = 0
intLenLstPrime = len(dctPrimeAsIntDigits)
while(intDigitIndx < intLenLstPrime):
# loop over the digits
intDigit = dctPrimeAsIntDigits[intDigitIndx]
if( (intLenLstPrime-1) == intDigitIndx ):
intNewShiftValue, intNewDigitValue = \
divmod( ((2**intMaxIterExponent) * intDigit) \
+ intShiftValue, intBaseOfNumericSystem \
)
dctPrimeAsIntDigits[intDigitIndx] = \
intNewDigitValue
if( intNewShiftValue ):
dctPrimeAsIntDigits[intDigitIndx+1] = \
intNewShiftValue
intShiftValue = 0
#: if
break
#: if
intNewShiftValue, intNewDigitValue = \
divmod( ((2**intMaxIterExponent) * intDigit) \
+ intShiftValue, intBaseOfNumericSystem \
)
dctPrimeAsIntDigits[intDigitIndx] = intNewDigitValue
intShiftValue = intNewShiftValue
intDigitIndx = intDigitIndx + 1
#: while() # loop over digits of the number
intExponentCounter = intExponentCounter \
+ intMaxIterExponent
#: while(intExponentCounter) # loop over exponent value
intExponentCounter = intExponentCounter \
- intMaxIterExponent
intRemainingExponent = intExponent - intExponentCounter
print intRemainingExponent
intDigitIndx = 0
intLenLstPrime = len(dctPrimeAsIntDigits)
while(intDigitIndx < intLenLstPrime):
# loop over the digits of the number
intDigit = dctPrimeAsIntDigits[intDigitIndx]
if( (intLenLstPrime-1) == intDigitIndx ):
intNewShiftValue, intNewDigitValue = \
divmod( ((2**intRemainingExponent) * intDigit) \
+ intShiftValue, intBaseOfNumericSystem \
)
dctPrimeAsIntDigits[intDigitIndx] = intNewDigitValue
if( intNewShiftValue ):
dctPrimeAsIntDigits[intDigitIndx+1] = intNewShiftValue
intShiftValue = 0
#: if
break
#: if
intNewShiftValue, intNewDigitValue = \
divmod( ((2**intRemainingExponent) * intDigit) \
+ intShiftValue, intBaseOfNumericSystem \
)
dctPrimeAsIntDigits[intDigitIndx] = intNewDigitValue
intShiftValue = intNewShiftValue
intDigitIndx = intDigitIndx + 1
#: while(intDigitIndx < len(lstPrimeAsIntDigits)) # loop over digits of the
number
print intExponentCounter \
, time.clock() - floatBegTime \
, time.clock() - floatCurrTime
floatCurrTime = time.clock()
lstPrimeAsStrDigits = []
for intDigitIndx in \
range(len(dctPrimeAsIntDigits)-1, -1, -1):
lstPrimeAsStrDigits.append( \
'%i'%dctPrimeAsIntDigits[intDigitIndx])
print 'append(): ' \
, time.clock() - floatBegTime \
, time.clock() - floatCurrTime
floatCurrTime = time.clock()
print ''.join(lstPrimeAsStrDigits)
print 'join(): ' \
, time.clock() - floatBegTime \
, time.clock() - floatCurrTime
floatCurrTime = time.clock()
And here the first output lines on my machine
(Pentium 4, 2.8 GHz, 400 MHz dual RAM):
Exponent TotalTime TimeForCurrIteration:
33218 1.64825417756e-005 2.40253998762e-005
49827 0.0148541225212 0.0148295383911
66436 0.0447663040973 0.0299105053855
83045 0.0893624748397 0.0445875104238
...
15014536 6956.48563854 23.4220966631
15031145 6978.40485984 21.9192143136
...
22422150 16369.6005045 26.9602413156
22438759 16397.0206864 27.4201684851
...
etc.
(python is still running with 4 hours 17 minutes CPU time)
More information about the Python-list
mailing list