Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- public class Combination {
- private Map<Integer,Long> factorialMap = new HashMap<Integer,Long>();
- public Long getFactorial(int number) {
- Long val = factorialMap.get(number);
- if(val != null) {
- return val;
- } else {
- val = getFactorialRecursive(number);
- factorialMap.put(number, val);
- return val;
- }
- }
- public Long getFactorialRecursive(int number) {
- if(number == 1 || number == 0) {
- return 1L;
- } else {
- return number * getFactorialRecursive(number-1);
- }
- }
- public Long combination(int fromVal, int chooseVal) {
- return getFactorial(fromVal)/(getFactorial(chooseVal)*getFactorial(fromVal-chooseVal));
- }
- public static void main(String[] args) {
- Combination comb = new Combination();
- for(int i = 0; i < 20; i++) {
- System.out.print(20 +" chose " + i + " = ");
- System.out.println(comb.combination(20,i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement