April 7th, 2005, 02:55 AM
Python error: OverflowError: long int too large to convert to int
I'm getting started with Python, so I don't know much yet.
I'm trying to use for, but keep getting error: OverflowError: long int too large to convert to int
Here is the line: for i in xrange(3L,half,1L):
half has the value n/2 where n is 1219852268062147. I tried to convert i into long, nothing.
Thanks in advance,
April 7th, 2005, 06:28 AM
before the for loop just put something like
April 7th, 2005, 07:13 AM
I don't have my computer now, but as I remember, I tried this, didn't help(I could be wrong).
But is there any other possible solutions?
April 7th, 2005, 08:53 AM
The range function does accept plain integers only. This is because the range function returns a list of integers and would crash on most computers for long integers with large values. You can easily calculate the memory requirements for a list of long integers for the value of 'half' from your example (several thousand terabyte).
If you need to iterate over long int, you could either use a while loop or create your own iterator class. This is a very basic example:
In real life this class would be extended to accept negative step values, parameter checking to avoid endless loops, restarting iteration or whatever nice features you want to have.
def __init__(self, start, range, step):
self.start = start
self.range = range
self.step = step
if self.range > self.start:
val = self.start
self.start += self.step
Use this class like:
for i in long_int_iterator(3, half, 1):
# do something
Comments on this post
April 7th, 2005, 06:13 PM
I can see how range() would create a list that's just too big for the available memory, but isn't xrange() generator based? Why doesn't that accept long ints?
Generally I'd agree with sbwiki... except I'm not going to, because you're counting up to six hundred and nine thousand nine hundred and twenty six billion one hundred and thirty four million thirty one thousand and seventy three, and I think you'd be better off putting it into a while loop just to avoid the overhead of calling next(), doing an if test and returning a value each time.
Completely unscientific test: Just running
for fifteen seconds on my computer, i counts to thirty four million. Which means in a minute, it will get to...
>>> lim = 1219852268062147/2
>>> i = 1
>>> while i < lim:
... i += 1
1/4467848th of the total.
So it will take... about three thousand days, or a little over eight and a half years to run.
Whereas using sbwiki's long_int_iterator class, in fifteen seconds i counts up to just nine million three hundred thousand. So it runs about 3 times slower, or an extra 17 years.
Why are you counting that high?
*This post does not account for power cuts, improvements in technology, Moore's law, human error, misreading of numbers, etc.
April 8th, 2005, 02:16 AM
Thank you for you replies!
Because I have to make a script that cracks RSA encryprion, it's for school.
And yeah, it will take too long. I talked to my teacher and what comes out is that i have done it all wrong . It works fine when numbers are small, but with big numbers... The solution is to use sqrt instead, he told that running that script on grid(http://grid.eenet.ee), would crack RSA in half hour.
Thank you once again,
April 8th, 2005, 04:28 AM
Wow, school is more exciting now than in my day
Hacking encryption standards 101
The most exiting thing I did at school with a computer was print ASCII art of Mickey Mouse. But the transistors buzzing "God Save the Queen" as the Mini loaded a punched tape was pretty cool too (at the time )