Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- for (int n=1;n<10; n++)
- System.out.println(Factorial.func(n));
- for (int n=1;n<10; n++)
- System.out.println(Factorial.func(n));
- }
- }
- class Factorial
- {
- static Map<Integer, Long> table= new TreeMap<Integer, Long>();
- static long func(int n)
- {
- Long val=table.get(n); // have we already worked it out?
- if (val!=null) return val;
- else
- { // work it out
- long temp=1;
- for (int i=2; i<=n; i++)
- temp*=i;
- table.put(n, temp); // remember it
- return temp; // and return answer
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment