# Factorials

**URL:** <https://www.ruby-forum.com/t/factorials/142965>\
**Category:** Ruby\
**Created:** [July 14, 2008, 1:57am UTC](https://www.ruby-forum.com/t/factorials/142965 "2008-07-14T01:57:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 14, 2008, 1:57am UTC](https://www.ruby-forum.com/t/factorials/142965/1 "2008-07-14T01:57:49Z")

</div>

I just started using ruby, and no matter how hard i try i cant do  
factorials. how to a get the factorial of an imported number?

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 14, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/factorials/142965/2 "2008-07-14T14:59:19Z")

</div>

On Sun, Jul 13, 2008 at 8:04 PM, Serabe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts factorial(5)
> 
> It does also print 120.
> 
> Regards,
> 
> Serabe
> 
> –

> [http://www.serabe.com](http://www.serabe.com)

The “Rubish” version of your factorial solutions appear to be much  
faster!  
Although I find it more confusing than the “traditional” recursive  
solution.  
I guess my problem is understanding the role of the _inject_ method.  
I use 100 instead of 5 to make it run a bit longer.

def factorial(n)  
if n==1  
1  
else  
n\*factorial(n-1)  
end  
end

puts factorial(100)

timex ruby factorial01  
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

# real 0.05 user 0.00 sys 0.00

def factorial(n)  
(1…n).inject(1) {|product, n| product \* n }  
end

puts factorial(100)

timex ruby factorial02  
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

real 0.02  
user 0.00  
sys 0.00

Regards,

Victor

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 14, 2008, 2:09am UTC](https://www.ruby-forum.com/t/factorials/142965/3 "2008-07-14T02:09:34Z")

</div>

The easy way:

def factorial(n)  
if n==1  
1  
else  
n\*factorial(n-1)  
end  
end

puts factorial(5)

It prints 120.

A more ruby way method:

def factorial(n)  
(1…n).inject(1) {|product, n| product \* n }  
end

puts factorial(5)

It does also print 120.

Regards,

Serabe

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 14, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/factorials/142965/4 "2008-07-14T16:57:08Z")

</div>

Victor R. wrote:

> I guess my problem is understanding the role of the _inject_ method.

Same problem here. But I figured out it’s ideal for calculating  
summations and products, where you’d use a big Sigma or Pi in  
mathematics. 🙂

Surprisingly Fortran never had Sigma or Pi constructs; we had to roll  
our own using a DO loop. Same for subsequent languages. Ruby is the  
first language I’ve come across with it built in.

Maybe someone would explain why it’s called “inject” though?

Dave

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 14, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/factorials/142965/5 "2008-07-14T17:22:18Z")

</div>

On Mon, Jul 14, 2008 at 10:52 AM, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Maybe someone would explain why it’s called “inject” though?

I believe that Matz took the name from Smalltalk, where the equivalent  
to  
Ruby’s

collection.inject(0) {|sum, element| sum + element }

would be:

collection inject: 0 into: [sum, element | sum + element]

The idea is that this method ‘injects’ the first value into the  
sequence,  
which is handy in some cases.

This is an ‘enhanced’ version of what is called reduce in many other  
functional and functional-influenced languages. Ruby allows the  
injection  
argument to be omitted in the call, which makes the name a bit more  
mysterious. Without the injection argument Ruby’s inject works just  
like  
reduce, and Ruby 1.9 makes reduce an alias for inject.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 14, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/factorials/142965/6 "2008-07-14T22:32:36Z")

</div>

2008/7/14 Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The “Rubish” version of your factorial solutions appear to be much faster!  
> Although I find it more confusing than the “traditional” recursive solution.  
> I guess my problem is understanding the role of the _inject_ method.  
> I use 100 instead of 5 to make it run a bit longer.

Copied from ri documentation for Enumerable#inject:

## ------------------------------------------------------ Enumerable#inject enum.inject(initial) {| memo, obj | block } =\> obj enum.inject {| memo, obj | block } =\> obj

```
 Combines the elements of _enum_ by applying the block to an
 accumulator value (_memo_) and each element in turn. At each step,
 _memo_ is set to the value returned by the block. The first form
 lets you supply an initial value for _memo_. The second form uses
 the first element of the collection as a the initial value (and
 skips that element while iterating).

    # Sum some numbers
    (5..10).inject {|sum, n| sum + n } #=> 45
    # Multiply some numbers
    (5..10).inject(1) {|product, n| product * n } #=> 151200

    # find the longest word
    longest = %w{ cat sheep bear }.inject do |memo,word|
       memo.length > word.length ? memo : word
    end
    longest #=> "sheep"

    # find the length of the longest word
    longest = %w{ cat sheep bear }.inject(0) do |memo,word|
       memo >= word.length ? memo : word.length
    end
    longest #=> 5

```

Regards,

Serabe
