waltermilner

Untitled

Oct 28th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.58 KB | None | 0 0
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         for (int n=1;n<10; n++)
  4.             System.out.println(Factorial.func(n));
  5.         for (int n=1;n<10; n++)
  6.             System.out.println(Factorial.func(n));
  7.     }
  8. }
  9.  
  10. class Factorial
  11. {
  12.     static Map<Integer, Long> table= new TreeMap<Integer, Long>();
  13.    
  14.     static long func(int n)
  15.     {
  16.         Long val=table.get(n); // have we already worked it out?
  17.         if (val!=null) return val;
  18.         else
  19.         { // work it out
  20.             long temp=1;
  21.             for (int i=2; i<=n; i++)
  22.                 temp*=i;
  23.             table.put(n, temp); // remember it
  24.             return temp; // and return answer
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment