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

#1 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • 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   User is offline

  • New D.I.C Head

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

Re: Factorial By Recursion

Posted 15 April 2009 - 09:42 AM

thx
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • 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}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1