Generate all possible combinations of 10 digits
trauqnej Posted messages 162 Status Membre -
Hello,
I would like to create a Python program that records all possible combinations of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 in a file.
The result should only be 8 characters long, and a digit can be used multiple times.
Here is my code:
#necessary imports from itertools import product #creating the list of 10 digits list = ["0","1","2","3","4","5","6","7","8","9"] #increment variable count = 0 #loop for "calculating" the combinations for i in product(list, repeat=8): count += 1 #conversion of the list to string Str_from_list = "".join(i) #adding the occurrence to a text file with open("/home/pierre/Bureau/crackpdf/dict.txt", "a") as f: f.write("\n") f.write(Str_from_list) print(count) The result is not what I expected: only 24310 occurrences.
Does anyone have an idea?
Thank you in advance
David
3 réponses
Hello
When you talk about the repetition of numbers, does it mean that you can have the same digit multiple times in one number?
For example, multiple zeros: 10,000,000?
If so, there’s no need for combinatorial mathematics; you just need to create a loop from the number 10,000,000 to the number 99,999,999.
On the other hand, if you want the combinations of each number having a distinct digit among the list you proposed, for example 12345679,
then yes, there are only 24,310 possible combinations.
.
Regards,
Jordane
Hello, I suggest using "product" instead of "combinations_with_replacement".
Hello,
And as the result may not be a number, but a string of 8 digits, we could start at 00000000, 00000001, ...
Yes.. it's true that I didn't think about the lead zeros..
Anyway.. it's going to take a long time to write .:-)