• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Permutation and Combination in Java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
Is there any utility class in java which helps in computing Permutation and Combination?
 
Sheriff
Posts: 28417
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The calculations themselves are very simple but you might require BigInteger if your numbers are going to become large.
 
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic