Opened 15 years ago

Closed 15 years ago

## #2851 closed enhancement (wontfix)

# dojo.math.combinations is correct but inefficient

Reported by: | guest | Owned by: | anonymous |
---|---|---|---|

Priority: | high | Milestone: | |

Component: | General | Version: | 0.4.2 |

Keywords: | math combinations | Cc: | |

Blocked By: | Blocking: |

### Description

Great work guys!

I just wanted to suggest an improvement to dojo.math.combinations that does less math:

public int combinations(final int n, final int r) {

if(n==0

r==0) return 1; var bigger; var smaller;

if (r<(n-r)) {

bigger=n-r; smaller=r;

} else {

bigger=r; smaller=n-r;

} var num=1; for(var i=n;i>bigger;i--){ num *= i; } return num/factorial(smaller);

}

This produces the same results but is faster and uses fewer factorial() calls. Write if you want me to explain the math behind it: [email protected]…

-gmb

**Note:**See TracTickets for help on using tickets.

code was not ported to 0.9