大数 [英] LARGE numbers

查看:66
本文介绍了大数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑编写一个程序来生成世界上最大的素数,这只是为了它的乐趣。这将需要

能够将8000000位数字保存到内存中(25兆位,或者只需一个变量就可以获得超过3兆内存的
...)I也会
需要几个较小的变量。这是因为我越来越优化了一个主要的
数字生成器,直到我想到尝试使用python尝试找到有史以来最大的
。有任何想法吗?我可能会尝试最终在大型机上运行这个
,虽然他们可能不喜欢它很多...我会在家里运行它计算机首先测试它。无论如何,

让我知道是否有办法让python支持数字如此之高。

谢谢!

解决方案

有关如何找到最大素数的更多信息,请参阅
www.mersenne.org


Python确实支持大数字,但它不是很快这样的

大数。有一个名为GMPY的Python模块,它使用GMP

(Gnu多精度)库来更快地操作大的
数字。


Python和GMP都使用二进制格式来存储大数字。二进制

格式对于计算是最有效的,但是它很慢......

将内部二进制格式的大数字转换为十进制格式

格式。


我写了一个专门设计用于运行巨额

数字的库。它以十进制格式存储数字,因此转换为十进制格式的
非常快。在我不太准备发布的
开发版本中,我可以计算,并转换为

十进制格式的字符串,最大的已知素数(2 ^ 25964951 - 1)少于
$ 10秒。到目前为止我最好的时间是6.6秒。


早期的alpha质量版本可以在
http://home.comcast.net/~casevh/


我也有候选版本GMPY for Windows

页面。


我会尝试在几天内获得下一个版本和一些演示。


案例


" Tuvas" <涂***** @ gmail.com>写道:

我一直在考虑编写一个程序来生成世界上最大的素数,只是为了它的乐趣。这将需要能够将8000000位数字保存到内存中(25兆位,或者仅仅一个变量就可以超过3兆内存......)我还需要几个较小的变量。这是因为我越来越优化了一个主要的数字生成器,直到我想到尝试使用python找到有史以来最大的数字生成器。有任何想法吗?我可能会尝试最终在大型机上运行它,虽然他们可能不喜欢它......我会在我的家用电脑上运行它来首先测试它。无论如何,
让我知道是否有办法让python支持数字如此之高。




Python已经支持大数字:

math.log(10 ** 8000000)
18420680.743952367




然而,你可能想要看看像gmpy这样的东西,因为你会得到更好的性能。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。


ca **** @ comcast.net 写道:

早期的alpha质量版本可在
http://home.comcast.net/~casevh/



给定名为Decimal的模块在Python 2.4中,我建议你重命名

你的图书馆。

-

Giovanni Bajo


I''ve been thinking about writing a program to generate the world''s
largest prime numbers, just for the fun of it. This would require being
able to hold an 8000000 digit number into memory (25 megabits, or a
little over 3 megs of memory for just one variable...) I would also
need several smaller variables. This came about as I optimised a prime
number generator more and more, until I came with the idea to try to
find the largest ever, using python. Any ideas? I''ll probably try to
run this on a mainframe eventually, although they might not like it
very much... I''ll run it on my home computer to first test it. Anyways,
let me know if there''s a way to make python support numbers so high.
Thanks!

解决方案

For more information on how the largest prime number was found, see
www.mersenne.org.

Python does support large numbers, but it''s not very fast for such
large numbers. There is a Python module called GMPY that uses the GMP
(Gnu Multiple Precision) library for faster operations on large
numbers.

Both Python and GMP use a binary format to store large numbers. Binary
format is most efficient for computation, but it is very slow to
convert huge numbers from the internal binary format to a decimal
format.

I have written a library designed specifically to operate with huge
numbers. It stores the numbers in a decimal format so conversion to
decimal format is very fast. On my not-quite-ready-to-release
development version, I can calculate, and convert to a string in
decimal format, the largest known prime (2^25964951 - 1) in less than
10 seconds. My best time so far is 6.6 seconds.

An early alpha-quality release is available at
http://home.comcast.net/~casevh/

I also have release candidate versions of GMPY for Windows on that
page.

I''ll try to get the next release and some demos up in a few days.

Case


"Tuvas" <tu*****@gmail.com> writes:

I''ve been thinking about writing a program to generate the world''s
largest prime numbers, just for the fun of it. This would require being
able to hold an 8000000 digit number into memory (25 megabits, or a
little over 3 megs of memory for just one variable...) I would also
need several smaller variables. This came about as I optimised a prime
number generator more and more, until I came with the idea to try to
find the largest ever, using python. Any ideas? I''ll probably try to
run this on a mainframe eventually, although they might not like it
very much... I''ll run it on my home computer to first test it. Anyways,
let me know if there''s a way to make python support numbers so high.



Python already supports numbers that large:

math.log(10 ** 8000000) 18420680.743952367



However, you probably want to look into something like gmpy, as you''ll
get better performance out of it.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


ca****@comcast.net wrote:

An early alpha-quality release is available at
http://home.comcast.net/~casevh/


Given the module named "Decimal" in Python 2.4, I''d suggest you to rename
your library.
--
Giovanni Bajo


这篇关于大数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆