views:

79

answers:

5

Ok so i am really bored and have decided to make a lottery calculator type thing (yes i know, i am sad!)

Anyway, i was wondering if there was a java library method/class for working out permutations/combinations. I want to generate all possible number sets, that have 6 numbers from 1 - 49 with no repeats in them

If this isnt available as a pre written method, whats the best approach for me to write my own?

Thank you

A: 

Google on "permutations java" found this.

Have fun!

Carl Smotricz
hmmmm the have fun seems like it could havea double meaning ;)
Ricki
No offense was intended; you indicated you were doing this because you were bored, i.e. "for fun." Personally, I feel that having the complete list of permutations is exactly as valuable as not having it, but to each his own!
Carl Smotricz
A: 

And this

extraneon
+1  A: 

This is my second answer. Here's a stupid but easy approach to coding this problem (in Java):

for (int i1=1; i1<45; i1++) {
   for (int i2=i1+1; i2<46; i2++) {
      for (int i3=i2+1; i3<47; i3++) {
         for (int i4=i3+1; i4<48; i4++) {
            for (int i5=i4+1; i5<49; i5++) {
               for (int i6=i5+1; i6<50; i6++) {
                  System.out.format("%d %d %d %d %d %d\n", i1, i2, i3, i4, i5, i6);
}}}}}}
Carl Smotricz
A: 

If you are doing this for fun, should you not be doing it from scratch?

With extra points for imaginative code? Looks like an ideal chance to try out recursion.

Think

getPermitations(length) { result = new Vector tmp = getPermitations(lenght-1)

for (i = 0=>9) { for (String s: tmp) { result.add(i + tmp); } }

}

Yes, I know that is not working code, where is the fun in copying someone else working code?

Then play with optimising it for extra points.

Jon
Or you could write something to actually build the full probability tree, creating a new object for every possibility. I wonder if you run out of memory first!
Jon
+1  A: 

A rough estimation:

49 * 48 * 47 * 46 * 45 * 44 = 10.068.347.520

This is the length of a list containing all possible combinations. Note that you can't use an ArrayList because this is backed by an array and an arrays maximum size is limited to Integer.MAX_VALUE. Even if you use byte arrays to store the ten billion combinations, you should start the jvm like this:

java -Xmx250G my.little.LotteryGenerator

(assuming you have sufficient memory on board)

Andreas_D