19. 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)
20. 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)
21. First year programmer,
Python
int Factorial_python(int n) {
int res = 1
(2..n).each { i ->
res *= i
}
return res
}
println Factorial_python(6)
24. Groovy expert programmer
def factExpert = { n ->
n ? (1..n).inject(1, { a, b -> a * b }): 1 }
println factExpert(6)
25. 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))