Guest

charly

By: a guest on May 6th, 2008  |  syntax: Java  |  size: 1.00 KB  |  hits: 124  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. public class Test {
  2.         static double factR(double n,int f) {
  3.                 if (n<0)
  4.                         return -1;
  5.                 else if (n==0)
  6.                         return 1;
  7.                 return ((f & 1)==0)?factBis(n,n):factBis2(n);
  8.         }
  9.        
  10.         static double factBis(double n, double r) {
  11.                 return (n==1)?r:factBis(--n,n*r);
  12.         }
  13.        
  14.         static double factBis2(double n) {
  15.                 return (n==1)?n:n*factBis2(n-1);
  16.         }
  17.        
  18.         static double factI(double n) {
  19.                 if (n==0 || n==1)
  20.                         return 1;
  21.                 else if (n<0)
  22.                         return -1;
  23.                        
  24.                 double r=n;
  25.  
  26.                 while(n>1)
  27.                         r*=--n;
  28.  
  29.                 return r;
  30.         }
  31.        
  32.         public static void main(String[] args) {
  33.                 long t0=System.currentTimeMillis();
  34.                
  35.                 System.out.println(factR(50,1));
  36.                
  37.                 long t1=System.currentTimeMillis();
  38.                
  39.                 System.out.println(t1-t0);
  40.                
  41.                 t0=System.currentTimeMillis();
  42.                
  43.                 System.out.println(factR(50,2));
  44.                
  45.                 t1=System.currentTimeMillis();
  46.                
  47.                 System.out.println(t1-t0);
  48.                
  49.                 t0=System.currentTimeMillis();
  50.                
  51.                 System.out.println(factI(50));
  52.                
  53.                 t1=System.currentTimeMillis();
  54.                
  55.                 System.out.println(t1-t0);
  56.         }
  57. }