Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby factorial function

I'm going crazy: Where is the Ruby function for factorial? No, I don't need tutorial implementations, I just want the function from the library. It's not in Math!

I'm starting to doubt, is it a standard library function?

like image 989
rutger Avatar asked Mar 12 '10 17:03

rutger


People also ask

What is the syntax of factorial?

The factorial of n is denoted by n! and calculated by multiplying the integer numbers from 1 to n. The formula for n factorial is n! = n × (n - 1)!.

What is recursion factorial?

A recursive function is a nonleaf function that calls itself. The factorial function can be written as a recursive function call. Recall that factorial(n) = n × (n – 1) × (n – 2) × … × 2 × 1. The factorial function can be rewritten recursively as factorial(n) = n × factorial(n – 1).


2 Answers

There is no factorial function in the standard library.

like image 136
sepp2k Avatar answered Sep 27 '22 23:09

sepp2k


Like this is better

(1..n).inject(:*) || 1 
like image 44
Alexander Revutsky Avatar answered Sep 28 '22 00:09

Alexander Revutsky