summaryrefslogtreecommitdiff
path: root/sample/fact.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-01-16 12:19:22 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-01-16 12:19:22 +0000
commitfd1d8cdc09ed86e4a0812120a17ff0d7b04adcaf (patch)
tree341289a84a427f1e92425c7ebf82d2f1733e9a40 /sample/fact.rb
parentf12baed5df6d3c213dd75d2f0d9f36bb179fb843 (diff)
*** empty log message ***
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@11 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample/fact.rb')
-rw-r--r--sample/fact.rb10
1 files changed, 6 insertions, 4 deletions
diff --git a/sample/fact.rb b/sample/fact.rb
index 49678bc9d0..1462a6923a 100644
--- a/sample/fact.rb
+++ b/sample/fact.rb
@@ -1,8 +1,10 @@
def fact(n)
- if n == 0
- 1
- else
- n * fact(n-1)
+ return 1 if n == 0
+ f = 1
+ while n>0
+ f *= n
+ n -= 1
end
+ return f
end
print fact(ARGV[0].to_i), "\n"