您如何在Python中可视化一个巨大的整数?

时间:2018-10-24 05:40:12

标签: python string int largenumber

所以,为了娱乐,我想用我自己的眼睛(根据this,发现2 277,277,917-1的数字)来发现最大的素数,它是23,249,425的数字。好家伙。因此,我首先在Python中手动计算了数字:2**277232917-1,这最终会给我答案...最终……一天。在等待一个半小时的时间限制整个核心的时间之后,我开始寻找一种更快的解决方案来解决指数问题。我在

的维基百科上发现了this gem
  

通过平方求幂

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)

将其插入python3控制台并输入t=exp_by_squaring(2, 277232917)-1并等待an..oh,等它完成后!我喜欢这个概念。现在有了这个数字,我可以print(str(t)),它又被冻结了。假设我可以让它在一整夜f=open("LargestPrime", "w") f.write(str(t)) f.close()内写入文件。第二天早上,只有一个 23.2 MB 文本文件,试图打开它只会冻结并限制内核。我想这甚至无法显示。

您将如何实现?您可以将int分成几个部分,然后将它们转换为字符串以将它们写入单独的文件吗?我可以将其保存为其他格式吗?如何缩短将这23M +位数字int转换为字符串所需的时间?实际上如何显示这么大的数字?我在这里完全错过了什么吗?

1 个答案:

答案 0 :(得分:1)

来自维基百科,https://en.wikipedia.org/wiki/Largest_known_prime_number,目前的记录保持者是本月创造的,他们显示了数字的第一位和最后一位数字,以下是维基百科的片段:

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.

你可以像这样自己在python中看到数字:


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