Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save axiak/524018 to your computer and use it in GitHub Desktop.
Save axiak/524018 to your computer and use it in GitHub Desktop.
// Newbie programmer
int factorial_newbie(int n) {
if (n == 0) {
return 1
} else {
return n * factorial_newbie(n - 1)
}
}
println factorial_newbie(6)
// First year programmer, studied Pascal
int factorial_pascal(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
return result;
}
println factorial_pascal(6)
// First year programmer, studied C
int fact_c(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
println fact_c(6)
// First year programmer, SICP
int fact_sicp(int n, int acc=1) {
if (n > 1) {
return (fact_sicp((n - 1), (acc * n)))
} else {
return (acc)
}
}
println fact_sicp(6)
// First year programmer, Python
int Factorial_python(int n) {
int res = 1
(2..n).each { i ->
res *= i
}
return res
}
println Factorial_python(6)
// Lazy Groovy programmer
int fact_lazy(int n) {
return (n <= 1) ? 1 : n * fact_lazy(n - 1)
}
println fact_lazy(6)
// Lazier Groovy programmer
def factLazier = { (it <= 1) ? 1 : it * call(it - 1) }
println factLazier(6)
// Groovy expert programmer
def factExpert = { n -> n ? (1..n).inject(1, { a, b -> a * b }): 1 }
println factExpert(6)
// Unix programmer
def factorialUnix(int n) {
def command = "factorial " + n
def proc = command.execute()
proc.waitFor()
print proc.in.text
}
factorialUnix(6)
// Web designer
String factorialWeb(int n) {
/*-------------------------------------------------
--- Code snippet from The Math Vault ---
--- Calculate factorial (C) Arthur Smith 1999 ---
-------------------------------------------------*/
String result = Integer.toString(1);
int i = 1 //Thanks Adam
while (i <= n) {
//result = result * i; //It's faster to use *=
//result = Integer.toString(result * result + i)
//result = Integer.parseInt(result *= i) #??????
result = Integer.toString(Integer.parseInt(result) * i)
//result = new String(Integer.parseInt(result) * i)
i = i + 1;
}
return result
}
println factorialWeb(6)
// Windows programmer
def CalculateAndPrintFactorialEx(dwNumber,
hOutputDevice,
lpLparam,
lpWparam,
lpsscSecurity,
dwReserved) {
if (lpsscSecurity != null) {
return null; // Not implemented
}
int dwResult = 1;
for (int dwCounter = 1; dwCounter <= dwNumber; dwCounter++) {
dwResult *= dwCounter;
}
hOutputDevice.print(dwResult);
hOutputDevice.print('\n');
return 1;
}
CalculateAndPrintFactorialEx(6, System.out, null, null, null, null)
// Enterprise programmer
class InternalBase {
private int base
public InternalBase(Integer base) {
this.base = base.intValue()
}
int getBase() {
return new Integer(base)
}
}
class StandardMathematicsSystem {
private static StandardMathematicsSystem INSTANCE = null
private Integer base
private StandardMathematicsSystem(InternalBase base) throws RuntimeException {
if (base.getBase().compareTo(new Integer(2)) != 0) {
throw RuntimeException("Non base 2 bases are not supported.")
}
this.base = base.getBase()
}
int calculateFactorial(Integer target) {
Integer result = new Integer(1)
for (Integer i = new Integer(2); i.compareTo(target) <= 0; i = new Integer(i.intValue() + 1)) {
result = result * i
}
return result
}
static private StandardMathematicsSystem createInstance(InternalBase base) {
return new StandardMathematicsSystem(base)
}
static StandardMathematicsSystem getInstance(InternalBase base) {
if (INSTANCE == null) {
INSTANCE = createInstance(base)
}
return INSTANCE
}
}
println StandardMathematicsSystem.getInstance(new InternalBase(new Integer(2))).calculateFactorial(new Integer(6))
// CPU parsimonious programmer
def factorial_cpu(int n) {
def fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000]
return fact[n]
}
println factorial_cpu(6)
@behrangsa
Copy link

I thought expert Groovy programmers do pay attention and follow Groovy/Java coding style guidelines. In other words, an expert Groovy programmer would name the function factExpert rather than fact_expret, ey?

@axiak
Copy link
Author

axiak commented Aug 15, 2010

That's a good point. :-) I had thought about removing all of those _* names but that would unfortunately cause it to of course error. I'll make that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment