Hi,
Here is the code that tries to compute factorial based on Numerical Recipes using Inline::C.
My question is, how can I improve my code below so that it can handle large number? Does Inline::C support malloc for typdef?
I've tried "Math::Pari qw(:int)" but of no avail. So I thought malloc should be the better way to go.
#!/usr/bin/perl w
use strict;
use Inline qw(C);
use Data::Dumper;
my $n = factrl(1000);
print "$n\n";
__END__
__C__
/* Here I attempt to increase the size of variable
with malloc such that it can handle BIG number
But is there anything wrong with it? */
typedef (malloc(2*sizeof(long double))) Big_Double;
Big_Double factrl;
/* Code below is to compute factorial */
double factrl(int n)
{
double gammln(double xx);
void nerror(char error_text[]);
static int ntop=4;
static double a[33]={1.0,1.0,2.0,6.0,24.0};
int j;
if (n<0) nerror("Negative factorial in routine factrl");
if (n>32) return exp(gammln(n+1.0));
while (ntop<n){
j = ntop++;
a[ntop]=a[j]*ntop;
}
return a[n];
}
double gammln(double xx)
{
double x,y, tmp,ser;
static double cof[6] = {76.18009172947146, 86.50532032941677,
24.01409824083091, 1.231739572450155,
0.12086509738661e2, 0.5395239384953e5};
int j;
y = x = xx;
tmp= x+5.5;
tmp = (x+0.5)*log(tmp);
ser=1.000000000190015;
for (j=0;j<=5;j++) ser += cof[j]/++y;
return tmp+log(2.5066282746310005*ser/x);
}
Regards,
Edward
Update:
I've also tried "use Math::GMP qw(:constant);" also can't handle big N.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
 a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)

For: 

Use: 
 &   & 
 <   < 
 >   > 
 [   [ 
 ]   ] 
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.

