summaryrefslogtreecommitdiff
path: root/sample/fact.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/fact.rb')
-rw-r--r--sample/fact.rb11
1 files changed, 6 insertions, 5 deletions
diff --git a/sample/fact.rb b/sample/fact.rb
index 49678bc9d0..9f6ca72ca7 100644
--- a/sample/fact.rb
+++ b/sample/fact.rb
@@ -1,8 +1,9 @@
def fact(n)
- if n == 0
- 1
- else
- n * fact(n-1)
+ return 1 if n == 0
+ f = 1
+ n.downto(1) do |i|
+ f *= i
end
+ f
end
-print fact(ARGV[0].to_i), "\n"
+puts fact(ARGV[0].to_i)