2009-03-08 21:28:53

mfactorial

Recently, I was playing around with the GNU Multiple Precision Arithmetic Library. While most of the resulting code snippets are pretty much useless, the attached C program might be worth a blog entry. It calculates the factorial of a given number. At first glance, nothing special at all, but it does this task rather fast:

/* Naive algorithm*/
time ./fak  500000 > /dev/null 

real    2m4.756s
user    2m4.637s
sys     0m0.094s


/*mfactorial*/
time ./mfactorial 500000 > /dev/null 

real    0m7.855s
user    0m13.226s
sys     0m0.050s
Maybe I'll re-implement the whole thing using a real clever algorithm (e.g. split-recursive) some day, but for now, I'm content with the current speed. :-)


BTW: The compiling instructions are included in the *.c file

Posted by haui | Permanent Link | Categories: misc, linux