How do I handle huge numbers?
Possible duplicate:
Most efficient implementation of a large number of classes
Suppose I needed to calculate 2 ^ 150000. Obviously the number will be larger than int, float or double. How can I create a data type that allows normal math functions but outperforms the basic number types?
If it "depends on what language you use," that's the deal. I'll say C #.
source to share
If C # isn't out of the blue and you want something that just works out of the box, then there are several options. I know Python best , but I think languages like Scheme and Ruby also support large numbers.
Python: 2**150000
. Prints out the result after about 1 second.
If you want free math software, look at Maxima or Sage.
source to share
You might also consider using Frink, which is a unit-capable language. It easily calculates 2 ^ 150,000, deals with fractions (e.g. 1/3 + 2/5 → 11/15), calculates 3 meters + 2 inches → 3.0508 m, and is a complete programming language.
Frink - Copyright 2000-2008 Alan Eliasen , eliasen@mindspring.com http://futureboy.us/frinkdocs/
Several languages are built to support arbitrary large numbers. For example, you can use Mathematica. I tried your example in Mathematica and the result is 45155 digits. I tried the same example with bc
on a Unix machine. bc supports extended precision but not extended; he bombed by example.
source to share
It is very difficult for me to use a language without arbitrarily large numbers: it seems pointless to be able to use ordinary operators such as addition on most numbers, but I have to switch to method calls on the BigInt instance simply because of its size.
The whole group of languages has more complete number towers and seamlessly enforces when needed; for example Allegro Common Lisp evaluates and prints all 45155 digits (expt 2 150000) in 1ms.
cl-user(2): (time (expt 2 150000))
; cpu time (non-gc) 0 msec user, 0 msec system
; cpu time (gc) 0 msec user, 0 msec system
; cpu time (total) 0 msec user, 0 msec system
; real time 1 msec
; space allocation:
; 2 cons cells, 18,784 other bytes, 0 static bytes
source to share
C calls calc, which is an arbitrary precision calculator. I used it once when I was working as a researcher and found it quite easy to use ...
http://sourceforge.net/projects/calc/
It can be programmed for complex or lengthy computations, and can take command line arguments. Interactively, it takes one command at a time and displays the response.
Usually commands are just expressions such as:
3 * (4 + 1)
and calc will output:
15
Calc performs the arithmetic operators +, -, /, *, as well as ^ (exponential),% (modulus), and // (integer division).
For example:
3 * 19 ^ 43 - 1
will produce:
29075426613099201338473141505176993450849249622191102976
Calc values can be VERY large. For example:
2 ^ 23209 - 1
will print:
402874115778988778181873329071 ... loads of digits ... 3779264511
Hope it helps ...
source to share