BobserLuck
BobserLuck

Reputation: 441

How do you Visualize a Huge Integer in Python?

So, just for fun, I wanted to see the largest prime we have discovered yet with my own eyes (2^277,232,917 − 1 according to this) which is a 23,249,425 digit number. Oh boy. So I started off with manually calculating the number in Python: 2**277232917-1 which would give me an answer...eventually...some day. After waiting a half an hour while one of my cores was throttled for the entire time, I started to look for a faster solution to solve exponents. I found this gem on Wikipedia known as

Exponentiation by Squaring

def exp_by_squaring(x, n):
    if n<0:
        return exp_by_squaring(1 / x, -n)
    elif x==0:
        return 1
    elif x==1:
        return x
    elif n%2==0:
        return exp_by_squaring(x * x,  n / 2)
    elif not n%2==0:
        return x * exp_by_squaring(x * x, (n - 1) / 2)

After plugging this into a python3 console and inputting t=exp_by_squaring(2, 277232917)-1 and waiting for an..oh wait it's done! I love this concept. Now with this number I can print(str(t)) and it's frozen again. Suppose I can let it write to a file over night f=open("LargestPrime", "w") f.write(str(t)) f.close(). Next morning with a single 23.2 MB text file, trying to open it up it just freezes and throttles a core again. I guess it's too much to even display.

How would you accomplish this? Could you split the int into separate portions and then convert them to strings to write them to separate files? Would I save it in a different format? How could I shorten the time it takes to convert this 23M+ digit int to a string? How could I practically display such a large number? Am I completely missing something here?

Upvotes: 3

Views: 262

Answers (1)

oppressionslayer
oppressionslayer

Reputation: 7214

From wikipedia, https://en.wikipedia.org/wiki/Largest_known_prime_number, the current record holder has been made this month and they show the first and last digits of the number, here is a snippet from Wikipedia:

Current record
The record is currently held by 2^82,589,933 − 1 with 24,862,048 digits, found by GIMPS in December 2018.[1] Its value is:

148894445742041325547806458472397916603026273992795324185271289425213239361064475310309971132180337174752834401423587560 ...

(24,861,808 digits omitted)

... 062107557947958297531595208807192693676521782184472526640076912114355308311969487633766457823695074037951210325217902591[6]

The first and last 120 digits are shown above.

You can see the digits yourself in python like this:


In [304]: t = pow(2,82589933)-1                                                                                                                                 

In [305]: n = 24862048-1000                                                                                                                                     

In [306]: a = pow(10,n)                                                                                                                                         

In [310]: f = t // a                                                                                                                                            

In [311]: len(str(f))                                                                                                                                           
Out[311]: 1000

In [312]: f                                                                                                                                                     
Out[312]: 1488944457420413255478064584723979166030262739927953241852712894252132393610644753103099711321803371747528344014235875600519775183265856491842931959708229506343343451097313699205342310641140595264767876746819332211781849375477107986211226534792788629942124472358169794644246737226991115661546889834987857788089927363336356512975433528625745217905541113567854803029538259231829040461918808066672007922224457105930988153887394047699962279207194319396507712065726965912877889178044489321452540526892581106697213587260581303968314495108439814585421184420014843770161064290389581708297705941888994879327016081279727414348185908077459964865519006267229417152151375452828119103082446114401235115945685219674703882657903762551993641583352385315154281845586882595358954721029880984778088370168635141972524013277223153442722574718130614762581537465586626911838102926072292274274159167780554098619357220471593661193199616071805842054109436528998477753168262245190870602541591290575551503401919575208699092280595

Upvotes: 1

Related Questions