You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
792B

  1. #! /usr/bin/env python3
  2. import random
  3. def generer(n):
  4. chiffres = [random.randint(0, 9) for _ in range(n)]
  5. if len(set(chiffres)) == 1:
  6. chiffres[0] = (chiffres[0]+1) % 10
  7. return chiffres
  8. def liste_vers_int(chiffres, decroissant):
  9. s_liste = ''.join(str(c) for c in sorted(chiffres, reverse=decroissant))
  10. return(int(s_liste))
  11. def kaprekar():
  12. chiffres = generer(4)
  13. print(chiffres)
  14. a = liste_vers_int(chiffres, decroissant=True)
  15. b = liste_vers_int(chiffres, decroissant=False)
  16. step = 1
  17. while a-b != 6174:
  18. step += 1
  19. chiffres = [int(c) for c in str(a-b)]
  20. a = liste_vers_int(chiffres, decroissant=True)
  21. b = liste_vers_int(chiffres, decroissant=False)
  22. print(f'{step}:{a-b}')
  23. return step
  24. kaprekar()