Permalink
Browse files

Overrode eric's factorial which slipped into dev.

[git-p4: depot-paths = "//src/ruby_to_c/dev/": change = 1882]
  • Loading branch information...
1 parent ebd5e78 commit f947a96319c1f43fda17950aba29444bc250854b @zenspider zenspider committed Feb 1, 2005
Showing with 7 additions and 60 deletions.
  1. +7 −60 demo/factorial.rb
View
67 demo/factorial.rb
@@ -1,64 +1,11 @@
-##
-# Factorial demo class for Interpreter.
-
-class Factorial
-
- ##
- # This is a really slow factorial implementation, but that's ok!
-
- def fact(n)
- acc = 1
-
- n.downto 1 do |i|
- acc = mult acc, i
- end
-
- return acc
+class F
+ def factorial(n)
+ f = 1
+ n.downto(2) { |x| f *= x }
+ return f
end
- ##
- # Expensive multiplication! Cool!
-
- def mult(a, b)
- acc = 0
-
- a.downto 1 do |i|
- acc += b
- end
-
- return acc
+ def main # horrid but funny hack
+ return factorial(5)
end
-
- ##
- # This is a faster factorial implementation.
-
- def fast_fact(n)
- acc = 1
-
- n.downto 1 do |i|
- acc *= i
- end
-
- return acc
- end
-
- def main # :nodoc:
- n = 5
-
- fast = fast_fact(n)
- #puts "fast: #{fast}" # only with interp.rb
-
- slow = fact(n)
- #puts "slow: #{slow}" # only with interp.rb
-
- if fast == slow and fast == 120 then
- puts "pass"
- else
- puts "fail"
- end
-
- return slow
- end
-
end
-

0 comments on commit f947a96

Please sign in to comment.