NAME
Math::Big - usefull routines with Big numbers (BigInt/BigFloat)
SYNOPSIS
use Math::Big qw/primes fibonacci hailstone/;
@primes = primes(100); # first 100 primes
$prime = primes(100); # 100th prime
@fib = fibonacci (100); # first 100 fibonacci numbers
$fib_1000 = fibonacci (1000); # 1000th fibonacci number
$hailstone = hailstone (1000); # length of sequence
@hailstone = hailstone (127); # the entire sequence
REQUIRES
perl5.005, Exporter, Math::BigInt, Math::BigFloat
EXPORTS
Exports nothing on default, but can export primes()
, fibonacci()
, hailstone()
.
DESCRIPTION
This module contains some routines that may come in handy when you want to do some math with really really big numbers. These are primarily examples.
METHODS
primes()
@primes = primes($n);
$prime = primes($n);
Calculates the first N primes and returns them as array. In scalar context returns the Nth prime.
This uses an optimzes version of the Sieve of Eratosthenes, which takes half of the time and half of the space, but is still O(N).
fibonacci()
@fib = fibonacci($n);
$fib = fibonacci($n);
Calculates the first N fibonacci numbers and returns them as array. In scalar context returns the Nth number of the serie.
hailstone()
@hail = hailstone($n); # sequence
$hail = hailstone($n); # length of sequence
Calculates the Hailstone sequence for the number N. This sequence is defined as follows:
while (N != 0)
{
if (N is even)
{
N is N /2
}
else
{
N = N * 3 +1
}
}
It is not yet proven wether for every N the sequence reaches 1, but it apparently does so. The number of steps is somewhat chaotically.
base()
($n,$a) = base($number,$base);
Reduces a number to $base to the $nth power plus $a. Example:
use Math::BigInt :constant;
use Math::Big qw/base/;
print base ( 2 ** 150 + 42,2);
This will print 150 and 42.
BUGS
primes()
not implemented yet.Primes and Fibonacci serie take an array of size N and will not be able to calculate big sequences due to memory constraints.
The exception is fibonacci in scalar context, this is able to calculate arbitrarily big numbers in O(N) time:
use Math::Big; use Math::BigInt; $fib = Math::Big::fibonacci( Math::BigInt->new(2) ** 150 );
AUTHOR
If you use this module in one of your projects, then please email me. I want to hear about how my code helps you ;)
This module is (C) Tels http://bloodgate.com 2001.