Created
October 30, 2010 23:12
-
-
Save beccasaurus/655851 to your computer and use it in GitHub Desktop.
Factorial in C# and Ruby (loop vs recursion)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Runtime.CompilerServices; | |
public class Factorial { | |
public static void Main(string[] args) { | |
string method = (args.Length > 0) ? args[0] : null; | |
long number = (args.Length > 1) ? Int64.Parse(args[1]) : 1; | |
int times = (args.Length > 2) ? Int32.Parse(args[2]) : 1; | |
Stopwatch timer = null; | |
switch (method) { | |
case "loop": | |
timer = Stopwatch.StartNew(); | |
for (var i = 0; i < times; i++) | |
LoopFactorial(number); | |
timer.Stop(); | |
Console.WriteLine("Ran LoopFactorial({0}) {1} times taking {2} milliseconds and returning {3}", | |
number, times, timer.ElapsedMilliseconds, LoopFactorial(number)); | |
break; | |
case "recursive": | |
timer = Stopwatch.StartNew(); | |
for (var i = 0; i < times; i++) | |
RecursiveFactorial(number); | |
timer.Stop(); | |
Console.WriteLine("Ran RecursiveFactorial({0}) {1} times taking {2} milliseconds and returning {3}", | |
number, times, timer.ElapsedMilliseconds, RecursiveFactorial(number)); | |
break; | |
default: | |
Console.WriteLine("Usage: Factorial.exe [loop|recursive] [number] [times to run]"); | |
break; | |
} | |
} | |
public static long LoopFactorial(long value) { | |
long factorial = 1; | |
long i = value; | |
while (i > 0) { | |
factorial = factorial * i; | |
i--; | |
} | |
return factorial; | |
} | |
public static long RecursiveFactorial(long value) { | |
if (value == 1) | |
return 1; | |
else | |
return value * RecursiveFactorial(value - 1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./Factorial.exe | |
Usage: Factorial.exe [loop|recursive] [number] [times to run] | |
$ ./Factorial.exe recursive 20 100000000 | |
Ran RecursiveFactorial(20) 100000000 times taking 10796 milliseconds and returning 2432902008176640000 | |
$ ./Factorial.exe loop 20 100000000 | |
Ran LoopFactorial(20) 100000000 times taking 2888 milliseconds and returning 2432902008176640000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'benchmark' | |
class Integer | |
def factorial_recursive | |
self <= 1 ? 1 : self * (self - 1).factorial_recursive | |
end | |
def factorial_iterative | |
f = 1; for i in 1..self; f *= i; end; f | |
end | |
end | |
method, number, times = ARGV | |
number = number ? number.to_i : 1 | |
times = times ? times.to_i : 1 | |
case method | |
when 'loop' | |
seconds = Benchmark.realtime do | |
(1..times).each { number.factorial_iterative } | |
end | |
puts "Ran factorial_iterative #{times} times taking #{seconds} seconds and returning #{ number.factorial_iterative }" | |
when 'recursive' | |
seconds = Benchmark.realtime do | |
(1..times).each { number.factorial_recursive } | |
end | |
puts "Ran factorial_recursive #{times} times taking #{seconds} seconds and returning #{ number.factorial_recursive }" | |
else | |
puts 'Usage: factorial.rb [loop|recursive] [number] [times to run]' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby --version | |
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [amd64-java] | |
$ ./factorial.rb | |
Usage: factorial.rb [loop|recursive] [number] [times to run] | |
$ ./factorial.rb recursive 20 10_000_000 | |
Ran factorial_recursive 10000000 times taking 33.9210000038147 milliseconds and returning 2432902008176640000 | |
$ ./factorial.rb loop 20 10_000_000 | |
Ran factorial_iterative 10000000 times taking 29.8079998493195 milliseconds and returning 2432902008176640000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./factorial.rb | |
Usage: factorial.rb [loop|recursive] [number] [times to run] | |
$ ./factorial.rb recursive 20 1_000_000 | |
Ran factorial_recursive 1000000 times taking 10.8792989253998 seconds and returning 2432902008176640000 | |
$ ./factorial.rb loop 20 1_000_000 | |
Ran factorial_iterative 1000000 times taking 5.87225413322449 seconds and returning 2432902008176640000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment