-2

A lottery winner needs to bet on 6 numbers from an array specified as listed below.

to extract the possible combinations - the code consists of two parts :

phase 1 - get all the permutations:

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37]

combinations = tuple(itertools.permutations(a, 6))

phase 2 - deduct all combinations:

I took some help from a different post here to remove any such duplicates because in lottery the combination [1,3,5] is similar to betting on [5,1,3].

The machine has launched, and crashed out of memory before ending phase 1.

I know most of you would recommend an upgrade to 64bit etc, but I'm looking to find a more efficient code to process the necessary results on the current machine I have. mainly for learning purposes.

many thanks for your reply !

Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
JuvinH
  • 3
  • 2
  • 3
    It's pretty hard to debug code we can't see. – Jonathan Hall Dec 22 '19 at 16:21
  • Unsure of what you are trying output here? There are 1.9 billion permutations of length 6 lists of 38 unique items so your code is trying to generate 1.9 billion lists which is why you are running out of memory. – Ben Dec 22 '19 at 16:28
  • As an aside, your code is running out of memory trying to allocate enough for 1,402,410,240 tuples of numbers, then filtering it. On my Python 3.7, each such tuple consumes 104 bytes, so you'll need at least 145 gigabytes of memory. – AKX Dec 22 '19 at 16:29
  • Please consider the guidelines for asking a good question: https://stackoverflow.com/help/how-to-ask – bad_coder Dec 22 '19 at 16:40

1 Answers1

0

Since you're looking for combinations, just use itertools.combinations()...

>>> import itertools
>>> numbers = list(range(1, 38))
>>> combinations = set(itertools.combinations(numbers, 6))
>>> print(len(combinations))
    2324784
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Correct, but inefficient. There is no need to generate all results, just to count them. You can easily calculate how many there are: `factorial(37) // factorial(37-6) // factorial(6)`. – zvone Dec 22 '19 at 16:46
  • Sure! Assuming the OP might need the combinations too at some point, this also shows them how to get at them. – AKX Dec 22 '19 at 16:48
  • thank you ! this works perfectly. didn't know about itertools.combinations prior to your comment :) – JuvinH Dec 23 '19 at 07:54