Home > Net >  Best way to find all the combination (72^5) and insert into Mysql DB
Best way to find all the combination (72^5) and insert into Mysql DB

Time:11-06

I'm having a hard time finding a solution for this and I don't even know if it's possible...

I'm developing a small app where I have 5 types of items with different stats, 7 levels (some have only 3 levels), and I would like to find all the items stats combinaisons.

Ex:

  • Item 1 lvl 1, Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • Item 1 lvl 2, Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • Item 1 lvl 3, Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • Item 1 lvl 4, Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • ....
  • Item 7 lvl 7, Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • Item 7 lvl 7, Item 1 lvl 2 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • Item 7 lvl 7, Item 1 lvl 3 Item 1 lvl 1 Item 1 lvl 1 Item 1 lvl 1
  • ...

So it will generate around 1 934 917 632 possibilities.

What should be the best approach to do this?

Is it ok to store those values into a Mysql Database?

Is there any way to optimize this? Do the calculation live with a lot of filters to reduce the possible combinaisons?

I already try How to get all combinations from multiple arrays? it works fine, but I only test with 40 possibilities, I'am more concerned about the Database part...

Thank you guys!

CodePudding user response:

All combinations can be mapped from the numbers from 0 to 1 934 917 631. It just takes some DIVs and MODs to split an integer into 5 numbers, each between 0 and 71. That might be a better starting point.

And if you want to store those 2 billion rows on disk, consider storing just 2 billion numbers, then pick them apart to find the 5 thingies.

In MySQL, a row with 5 TINYINT UNSIGNED columns is another approach. It's not quite as compact as a single INT UNSIGNED, but it might (or might not) be easier to work with.

CodePudding user response:

You can use the following code to generate all the combinations:

public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5};
    int n = arr.length;
    int r = 3;
    int[] data = new int[r];
    combinationUtil(arr, data, 0, n - 1, 0, r);
}

static void combinationUtil(int[] arr, int[] data, int start, int end, int index, int r) {
    if (index == r) {
        for (int j = 0; j < r; j  ) {
            System.out.print(data[j]   " ");
        }
        System.out.println("");
        return;
    }
    for (int i = start; i <= end && end - i   1 >= r - index; i  ) {
        data[index] = arr[i];
        combinationUtil(arr, data, i   1, end, index   1, r);
    }
}
  • Related