#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Estonia
    Posts
    4
    Rep Power
    0

    Python error: OverflowError: long int too large to convert to int


    Hey,

    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,
    Sander
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    before the for loop just put something like
    Code:
    i = 0L
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Estonia
    Posts
    4
    Rep Power
    0
    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?


    Thanks,
    Sander
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    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:
    Code:
    class long_int_iterator:
        def __init__(self, start, range, step):
            self.start = start
            self.range = range
            self.step = step
    
        def __iter__(self):
            return self
    
        def next(self):
            if self.range > self.start:
                val = self.start
                self.start += self.step
                return val
            raise StopIteration
    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.

    Use this class like:
    Code:
    for i in long_int_iterator(3, half, 1):
        # do something

    Comments on this post

    • netytan agrees
    • CyBerHigh agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    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

    Code:
    >>> lim = 1219852268062147/2
    >>> i = 1
    >>> while i < lim:
    ... 	i += 1
    for fifteen seconds on my computer, i counts to thirty four million. Which means in a minute, it will get to...

    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Estonia
    Posts
    4
    Rep Power
    0
    Thank you for you replies!

    Why are you counting that high?
    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,
    Sander
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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 )

    grim

IMN logo majestic logo threadwatch logo seochat tools logo