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?