Generate all possible combinations of 10 digits

David -  
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

jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 

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

0
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   9 947
 

Hello,

And as the result may not be a number, but a string of 8 digits, we could start at 00000000, 00000001, ...

0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830 > jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention  
 

Yes.. it's true that I didn't think about the lead zeros..

Anyway.. it's going to take a long time to write .:-)

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   Ambassadeur 1 587
 

Hello, I suggest using "product" instead of "combinations_with_replacement".

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587
 

syntax:

for i in itertools.product(list, repeat=8)
0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587 > yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention  
 

Your code does 10 million "opens". Better to do it just once:

import itertools liste=[str(i) for i in range(10)] with open("liste.txt","w") as f: c=0 for n in itertools.product(liste,repeat=8): c += 1 f.write("\n"+"".join(n)) print(c)
0
trauqnej Posted messages 162 Status Membre 23
 

Hello,

I can see something in this style:

fic = open("/tmp/fichier.txt",'w') for i in range(100000000): fic.write("{:0>8d}".format(i)+"\n") fic.close()

Best regards,

0