Is there any built-in factorial function in c++?

  • A+
Category:Languages

I have searched but I was not able to find this. Would anyone please tell me that if there is any built-in factorial function in c++ ?


Although there is no C function defined specifically for computing factorials, C math library lets you compute gamma function. Since Г(n) = (n-1)! for positive integers, using tgamma of i+1 yields i!.

If you use a user-defined factorial function, this would print identical numbers:

for (int i = 1 ; i != 10 ; i++) {     printf("%lld %f/n", factorial(i), tgamma(i+1)); } 

Demo.

1 1.000000 2 2.000000 6 6.000000 24 24.000000 120 120.000000 720 720.000000 5040 5040.000000 40320 40320.000000 362880 362880.000000 

Note: Considering how easy it is to code up factorial function, using gamma to compute factorials is a lot like killing a fly with a sledgehammer.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: