0
Hi guys, i need help to get all possible combinations (permutations) after generating a set of random numbers. I attached code
4 Random numbers are generated from the range 1-49. Eg, 1,2,3,4)(11,32,17,49) and so on https://code.sololearn.com/cLz62HXBzy3k/?ref=app
11 ответов
+ 2
After reading your post, and most of the other comments, I've decided to give this a try as well. The question posted is slightly unclear, but since I have time, I answered everything that I could think of (Java isn't the programming language I'm most familiar with).
Before everything that has to do with computer programming, here is something you might want to know for future reference:
Combinations are the numbers of combinations that a number of individual objects can make, where order does not matter.
Permutations, however, are combinations where the order does matter.
Now, into the programming.
First, I thought that you could have wanted is all of the possible combinations of 4 numbers within a 49 number range (1 ~ 49), and it looks like this:
https://code.sololearn.com/cVFp2YQ3xQZI
You might notice that it ends with time limit exceeded. This is because there are too many combinations that the computer has to print, and it can't handle that many.
Then, I, for some strange reason, thought that you might want to know how many combinations there are. So, I wrote this not very conventional way of calculating the amount of combinations there were:
https://code.sololearn.com/cm0P6Xy4tdPA
This prints the number of combinations that the previous code had to print, and it might make sense to you why the program exceeded the time limit.
Finally, after looking at your code, I also thought that maybe you wanted all of the combinations from the 50 randomly generated sets of 4 numbers, without repeats or care for order. But, if you think about it hard enough, you'll realize a few things:
- First, the number of sets printed changes.
- and second, the number of sets printed would be 200!/(50!150!), a number so large that even Google can't calculate it. (Evaluating to 453,858,377,923,246,061,067,441,390,280,868,162,761,998,660,528 from Wolfram Alpha)
That's all I've got, and I hope it helps!
+ 1
so you actually want to use unordered sampling without replacement, meaning the objects are unique. In this case the total number of possible combinations is given mathematically by the equation n!/k!(n-k)!. So in your case is 49!/4!(49-4)! = a big number...
One way to do this programatically is using 4 for loops, one nested into the other. You have also to add some control of what number is already in your 4 number array each time. you dont need the random method
+ 1
Julien thanks for help, and i am sorry for not replying sooner, i was actually searching on Heaps algorithm because i haven't used it before, so i didn't want to come back to you empty handed if I struggle, i will let you know how it went. Thanks again
+ 1
Thank you Aaron Hung for clarifying, i appreciate it, i am learning a lot from you guys 🤗🤗
0
could you be more specific? Does the order of generated bumbers count? Your goal is to catch all possible combinations from these numbers generated randomly or you just want to find the combinations?
0
Hi kostas Batz, thank you, and No the oder is not important, i just want all possible combinations
0
And the numbers are not supposed to repeat, like lets (1,1,2,3) every combination Must have unique numbers
0
Kostas, thank you again for clarifying, now i understand what my needs to be done but I am not sure how to implement such in code but I am going to give it a try also
0
Yeah, no problem!