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

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    very large integer


    I am messing around with something and i wanted to see how big of a number python could handle i thought it would be the same as what unsigned long was in c but it seems to be even larger

    here is what i did

    Code:
     x = 10 ** 10000000
      if x mod 2:
      	  print "hey"
    i couldn't get it to print x but it seems to be abel to do the math is this right or is there just an error that is making it seem liek it does the math?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I don't know if there’s a defined limit to how big Pythons long type can be, however I would assume it would be the same as in C - not sure what that is . The problem might end up being speed. Everything goes fine up to10**100000, after that it just takes way too long to get the answer.

    Edit: It’s been 10 minutes now and I’m still waiting .

    Anyway, if you run the code above it will hand for a while, and eventually give you a SyntaxError since the name ‘mod’ isn’t a valid operator: try the modulus operator - %. After this you should be able to print the value of x however the number will be a little big to fit on the screen. You might want to write to a file instead .

    Hope this helps,

    Mark.
    Last edited by netytan; February 12th, 2005 at 01:12 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    lol ya sorry not mod, i ment % sorry i was thinking vb


    anyway, it should just print 0 since 2 goes into that number evenly. I know it does, but i was just wondering if it actually was workign.

    also it doesn't write it to a file i tried that second. i even tryed using cpickle to do it and it didn't
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could do something like:

    Code:
    print 10 ** 10000000 % 2
    You can probably guess what this does but just in case: it will print 0 if the number divides equally in half or 1 if it doesn't.

    Just a shortcut ,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    ya that is what i ment.

    So you think it is really workign with that large number?

    but c long isn't close to 10 ** 10000000
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Python long types are arbitrary precision ints - they do not use a fixed number of bytes, and are only limited by the amount of memory your computer has. You could conceivably have a long int that was millions of bits long. Doing maths with numbers that big would be rather slow, though.

    Here is a smaller example: 2 to the power 8000 - i.e. a number that needs 1000 bytes to store it:
    Code:
    >>> 1L << 8000
    173766203193809456599982445949435627061939786100117250547173286503262376022458008465094333630120854338003194362163007597
    987225472483598640843335685441710193966274131338557192586399006789292714554767500194796127964596906605976605873665859580
    600161998556511368530960400907199253450604168622770350228527124626728538626805418833470107651091641919900725415994689920
    112219170907023561354484047025713734651608777544579846111001059482132180956689444108315785401642188044178788629853592228
    467331730519810763559577944882016286493908631503101121166109571682295769470379514531105239965209245314082665518579335511
    291525230373316486697786532335206274149240813489201828773854353041855598709390675430960381072270432383913542702130202430
    186637321862331068861776780211082856984506050024895394320139435868484643843368002496089956046419964019877586845530207748
    994394501505588146979082629871366088121763790555364513243984244004147636040219136443410377798011608722717131323621700159
    335786445601947601694025107888293017058178562647175461026384343438874861406516767158373279032321096262126551620255666605
    185789463207944391905756886829667520553014724372245300878786091700563444079107099009003380230356461989260377273986023281
    444076082783406824471703499844642915587790146384758051663547775336021829171033411043796977042190519657861762804226147480
    755555085278062866268677842432851421790544407006581148631979148571299417963950579210719961422405768071335213324842709316
    205032078384168750091017964584060285240107161561019930505687950233196051962261970932008838279760834318101044311710769457
    048672103958655016388894770892065267451228938951370237422841366052736174160431593023473217066764172949768821843606479073
    866252864377064398085101223216558344281956767163876579889759124956035672317578122141070933058555310274598884089982879647
    974020264495921703064439532898207943134374576254840272047075633856749514044298135927611328433323640657533550512376900773
    273703275329924651465759145114579174356770593439987135755889403613364529029604049868233807295134382284730745937309910703
    657676103447124097631074153287120040247837143656624045055614076111832245239612708339272798262887437416818440064925049838
    443370805645609424314780108030016683461562597569371539974003402697903023830108053034645133078208043917492087248958344081
    026378788915528519967248989338592027124423914083391771884524464968645052058218151010508471258285907685355807229880747677
    634789376L
    Dave - The Developers' Coach
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Well, the CPU usage jumps to 100% when I run the line and stays there so I'd guess it's working it out; I just dont want to wait hours for it to print something .

    I'm sure there must be a better way to find the maximum size of a Long in Python.

    Anyone?

    Edit: Like Dave . Nice reply!

    Mark.
    Last edited by netytan; February 12th, 2005 at 03:33 PM.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Incidentally, since 10 is about 3.3 bits, 10**10,000,000 requires over 33 million bits to represent it, or over 4 million bytes. It is not surprising it takes a while to calculate.

    Dave - The Developers' Coach
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    how does python make it in c so it can actually handle that num?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    88
    Rep Power
    11
    When calculating, my CPU usuage stays at about 33% for 5 minutes, while it was calculating a very large number.
    I'm using an Intel P4 Prescott @ 3.2 GHz, and clue why this is?
    http://www.ionicdomain.com/
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by CyBerHigh
    how does python make it in c so it can actually handle that num?
    Use the source, Luke

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo