Created
January 29, 2010 21:58
-
-
Save krbullock/290189 to your computer and use it in GitHub Desktop.
This file contains 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
#Newbie programmer | |
def factorial(x) | |
if x == 0 | |
return 1 | |
else | |
return x * factorial(x - 1) | |
end | |
end | |
puts factorial(6) | |
#First year programmer, studied Pascal | |
def factorial(x) | |
result = 1 | |
i = 2 | |
while i <= x | |
result = result * i | |
i = i + 1 | |
end | |
return result | |
end | |
puts factorial(6) | |
#First year programmer, studied C | |
def fact( x ) #{ | |
result = i = 1; | |
while ( i <= x ) #{ | |
result *= i; | |
i += 1; | |
end #} | |
return result; | |
end #} | |
puts( fact( 6 ) ) | |
#First year programmer, SICP | |
def fact(x, acc=1) | |
if (x > 1) then (fact((x - 1), (acc * x))) | |
else acc | |
end | |
end | |
puts(fact(6)) | |
#First year programmer, studied Python | |
def Factorial(x)#: | |
res = 1 | |
for i in Range.new(2, x+1)#: | |
res *= i | |
end # silly unnecessary keyword | |
return res | |
end # why can't I just use indentation? | |
puts Factorial(6) | |
#Lazy Ruby programmer | |
def fact(x) | |
x > 1 ? x * fact(x - 1) : 1 | |
end | |
puts fact(6) | |
#Lazier Ruby programmer | |
def f x; x > 1 ? x * fact(x - 1) : 1 end | |
puts f(6) | |
#Ruby expert programmer | |
puts (1..6).inject {|fact, x| fact * x} | |
#Ruby hacker | |
class Integer | |
def fact | |
if self > 1 then self * (self - 1).fact | |
else 1 | |
end | |
end | |
end | |
puts 6.fact | |
#Web designer | |
# app/controllers/factorials_controller.rb | |
class FactorialsController | |
before_filter :calculate_factorial!, :only => :index | |
def index | |
end | |
private | |
# Code snippet from writhing :the => Rails <http://writhing.therai.ls/> | |
# (c) Arthur Smith 2008 | |
def calculate_factorial!(x) | |
# cache the value for repeated use - thanks DHH | |
@factorial ||= (1..x).to_a.inject(1) {|sum, n| sum *= n} #?????? | |
end | |
end | |
# app/views/factorials/index.html.haml | |
%= render :partial => 'factorial', :locals => {:factorial => @factorial} | |
# app/views/factorials/_factorial.html.haml | |
%strong.factorial= factorial | |
#Unix programmer | |
def fact(x) | |
system(sprintf("factorial %s", x.to_s)) | |
end | |
puts fact(6) | |
#Perl hacker | |
def fact(x) | |
`factorial #{x}` | |
end | |
puts fact(6) | |
#Windows programmer | |
require 'Win32API' | |
NULL = nil | |
def CalculateAndPrintFactorialEx(dwNumber, | |
hOutputDevice, #not currently used | |
lpLparam, | |
lpWparam, | |
lpsscSecurity, | |
*dwReserved) | |
if lpsscSecurity != NULL | |
return NULL #Not implemented | |
end | |
dwResult = dwCounter = 1 | |
while dwCounter <= dwNumber | |
dwResult *= dwCounter | |
dwCounter += 1 | |
end | |
ods = Win32API.new("kernel32", "OutputDebugString", ['P'], 'V') | |
ods.Call(dwResult.to_s + "\n") | |
return 1 | |
end | |
import sys | |
CalculateAndPrintFactorialEx(6, STDOUT, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) | |
#Enterprise Ruby programmer | |
class FactorialFactory { | |
public Integer factoriand; | |
public Integer base; // for later use | |
public FactorialFactory(Integer newFactoriand, Integer newBase) { | |
factoriand = newFactoriand; | |
base = newBase; | |
} | |
public calculate() { | |
Integer result; | |
if (factoriand > 1) { | |
result = 0; | |
} else { | |
nextLowerFactorial = new FactorialFactory(factoriand - 1, base); | |
result = factoriand * nextLowerFactorial.calculate(); | |
} | |
return result; | |
} | |
} | |
class FactorialPrinter { | |
public static void main(String[] args) { | |
fact = FactorialFactory.new(6, 10) | |
System.out.println(fact.calculate()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment