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
commit7ea2ceddb832b9973694fecac9fe3c30400735ba (patch)
treea9b60dec20fa5f7f52ca7c8113195f2d65728a22 /sample/fact.rb
parent62e41d3f2e48422bbdf1bb2db83ae60b255b1a1a (diff)
This commit was generated by cvs2svn to compensate for changes in r11,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12 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"