473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

LARGE numbers

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!

Nov 11 '05 #1
10 4956
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

Nov 11 '05 #2
"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.
Nov 11 '05 #3
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
Nov 11 '05 #4
Already done for next version. Tentatively, there will be a package
called "ar" (Arbitrary Radix) and the module will be called BigInt. I'm
also working on an arbitrary radix BigFloat module.

Case

Nov 11 '05 #5
<ca****@comcast.net> wrote:
...
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.


As the author of gmpy, I'd like to point out that the speed difference
isn't all that large, if all you're doing is ordinary arithmetic -- a
few times at most (it can be better if you need some of GMP's
functionality which gmpy exposes, such as primality testing).
Alex
Nov 11 '05 #6
al***@mail.comcast.net (Alex Martelli) writes:
As the author of gmpy, I'd like to point out that the speed difference
isn't all that large, if all you're doing is ordinary arithmetic -- a
few times at most (it can be better if you need some of GMP's
functionality which gmpy exposes, such as primality testing).


For numbers of this size, won't gmpy use FFT-based multiplication?
That's potentially orders of magnitude faster than ordinary n**2
multiplication.
Nov 11 '05 #7
Paul Rubin wrote:
al***@mail.comcast.net (Alex Martelli) writes:

For numbers of this size, won't gmpy use FFT-based multiplication?
That's potentially orders of magnitude faster than ordinary n**2
multiplication.


But Python is no slouch with its use of Karatsuba multiplication.
(in other words, Python is not N**2 for large numbers).

--Scott David Daniels
sc***********@acm.org
Nov 11 '05 #8

Paul Rubin wrote:
al***@mail.comcast.net (Alex Martelli) writes:
As the author of gmpy, I'd like to point out that the speed difference
isn't all that large, if all you're doing is ordinary arithmetic -- a
few times at most (it can be better if you need some of GMP's
functionality which gmpy exposes, such as primality testing).


For numbers of this size, won't gmpy use FFT-based multiplication?
That's potentially orders of magnitude faster than ordinary n**2
multiplication.


Python's native longs use Karatsuba multiplication with is O(n^1.585).

My early version of DecInt (BigDecimal) uses 4-way Toom-Cook
multiplication which is O(n^1.4). My development version uses
Nussbaumer convolution with is O(n ln(n)).

For multiplicaiton of two 1,000,000 digits numbers and conversion to a
decimal string, here are some times:

GMPY
multiplication: 0.96 seconds
conversion to string: 712.7 seconds

DecInt with GMPY
multiplication: 1.33 seconds
conversion to string: 0.83 seconds

DecInt without GMPY
multiplication: 2.84 seconds
conversion to string: 0.45 seconds

Python (using native long)
multiplication: 8.47 seconds
conversion to string: a really long time

Case

Nov 11 '05 #9
ca****@comcast.net writes:
Python's native longs use Karatsuba multiplication with is O(n^1.585).
My early version of DecInt (BigDecimal) uses 4-way Toom-Cook ...


Wow, cool! Thanks.
Nov 11 '05 #10
Well, as I'll be doing lots of multiplication, guess that GMPY is the
way to go. I'll use DecInt only for converting to strings if I find
anything interesting. This is all just kind of a theoretical aproach,
but, it can be lots of fun. Who knows if Python'll help find the
largest prime number ever? That would sure be cool. Thanks for all of
your help.

Nov 11 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: koray | last post by:
hi, i need to show large numbers seperated by commas. since i'm using variables from speedscript, i cannot know their values, since the user should enter them. how should i code to show these...
3
by: Alex Vinokur | last post by:
Dann Corbit has implemented function int ilog2 (unsigned long) at http://groups.google.com/groups?selm=lkPa4.2165%24I41.1498%40client Is exist a similar C++-function for very large numbers,...
5
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are...
18
by: Brad | last post by:
I have a problem in that I need to change a large decimal value into its hex equivalent. The largest decimal value I need to represent as hex is 25516777215. The problem here is that this number...
18
by: Zero | last post by:
Hi, I am calculating an integer to the pwer of a large integer, e.g. 2^5000. It turns out that the result I always get is zero. I am sure that the result is too large to store in such type...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
3
by: CFonville | last post by:
I was wondering if there is any way to store large numbers in a variable? With this simple script: var bigpi = 1234567890123456789012345678901234567890123456789012345678901234567890;...
57
by: Chris Foote | last post by:
Hi all. I have the need to store a large (10M) number of keys in a hash table, based on a tuple of (long_integer, integer). The standard python dictionary works well for small numbers of keys,...
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.