Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Permutation and Combination in Java

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
HI,
Is there any utility class in java which helps in computing Permutation and Combination?

Marshal
Posts: 28271
95
• Number of slices to send:
Optional 'thank-you' note:
The calculations themselves are very simple but you might require BigInteger if your numbers are going to become large.

Marshal
Posts: 79562
380
• Number of slices to send:
Optional 'thank-you' note:
Go to the API index (that was for P) and try ctrl-F permutation. And you will probably find the same as I did . . . nothing. You will have to try with C for combination, too.

Campbell Ritchie
Marshal
Posts: 79562
380
• Number of slices to send:
Optional 'thank-you' note:
Combination =
n!
r! × (n - r)!

Permutation =
n!
r!

In all cases nr and if n = r both nCr = 1 and nPr = 1

You can probably count down from n and up from r; if you multiply downwards, you can reduce the maximum size of intermediate values. Don’t try to work out 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 ÷ 7 ÷ 6 ÷ 5 ÷ 4 ÷ 3 ÷ 2 ÷ 1.
Try 10 ÷ 1 × 9 ÷ 2 × 8 ÷ 3 × 7 ÷ 4 × 6 ÷ 5 × 5 ÷ 6 × 4 ÷ 7 × 3 × 2 × 1
You can speed the process, at the cost of more complicated code, if you can work out how to remove common factors and divisors.

 Is that a spider in your hair? Here, threaten it with this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser