
charly
By: a guest on May 6th, 2008 | syntax:
Java | size: 1.00 KB | hits: 124 | expires: Never
public class Test {
static double factR(double n,int f) {
if (n<0)
return -1;
else if (n==0)
return 1;
return ((f & 1)==0)?factBis(n,n):factBis2(n);
}
static double factBis(double n, double r) {
return (n==1)?r:factBis(--n,n*r);
}
static double factBis2(double n) {
return (n==1)?n:n*factBis2(n-1);
}
static double factI(double n) {
if (n==0 || n==1)
return 1;
else if (n<0)
return -1;
double r=n;
while(n>1)
r*=--n;
return r;
}
public static void main
(String[] args
) {
long t0
=System.
currentTimeMillis();
System.
out.
println(factR
(50,1
));
long t1
=System.
currentTimeMillis();
t0
=System.
currentTimeMillis();
System.
out.
println(factR
(50,2
));
t1
=System.
currentTimeMillis();
t0
=System.
currentTimeMillis();
System.
out.
println(factI
(50
));
t1
=System.
currentTimeMillis();
}
}