# Factorial By Recursion

Page 1 of 1

## 2 Replies - 1735 Views - Last Post: 29 December 2011 - 04:50 PM

### #1 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

# Factorial By Recursion

Posted 21 July 2007 - 01:07 PM

Description: Just copy the code and run it in a Ruby InterpreterFinds the factorial of a number by using a recursive function
```# PennyBoki @
puts 'Enter a number'
STDOUT.flush
string1 = gets.chomp
puts 'The factorial of '+string1+' is'
y = string1.to_i
def factorial(y)
if y==0
return 1
else
return (y*factorial(y-1))
end
end
x = factorial(y)
puts x.to_s
```

Is This A Good Question/Topic? 0

## Replies To: Factorial By Recursion

### #2 shabaaz

Reputation: 0
• Posts: 7
• Joined: 15-April 09

## Re: Factorial By Recursion

Posted 15 April 2009 - 09:42 AM

thx

### #3 Karel-Lodewijk

Reputation: 455
• Posts: 864
• Joined: 17-March 11

## Re: Factorial By Recursion

Posted 29 December 2011 - 04:50 PM

Know that factorial in a non-recursive manner is much faster: (1..10000).inject(1){|fact, x| fact *= x}