NAME
Math::Big - usefull routines with Big numbers (BigInt/BigFloat)
SYNOPSIS
use Math::Big qw/primes fibonacci hailstone factors wheel/;
@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
$wheel = wheel (4); # prime number wheel of 2,3,5,7
print $wheel->[0],$wheel->[1],$wheel->[2],$wheel->[3],"\n";
# factor 19*71*59*3 into it's factors using a wheel of order 3
@factors = factors (19*71*59*3,3);
$fak = fak(1000); # faktorial 1000!
REQUIRES
perl5.005, Exporter, Math::BigInt, Math::BigFloat
EXPORTS
Exports nothing on default, but can export primes()
, fibonacci()
, hailstone()
and factorial
.
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 whether 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.
wheel()
$wheel = wheel($o);
Returns a reference to a prime wheel of order $o. This is used for factoring numbers into prime factors, see factors() for a detailed description.
factors()
Factor a number into it's prime factors and return a list of factors.
factorial()
$n = factorial($number,$factorial);
Calculate n! for n >= 1 and returns the result.
BUGS
Primes and the Fibonacci serie use 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 qw/:constant/; $fib = Math::Big::fibonacci( 2 ** 320 );
LICENSE
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
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 ;)
Quite a lot of ideas from other people, especially D. E. Knuth, have been used, thank you!
Tels http://bloodgate.com 2001.