Skip to content

Instantly share code, notes, and snippets.

@orjanv
Last active January 18, 2024 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orjanv/0caf81b0abb6267f9c1a5e4b6caf7b0a to your computer and use it in GitHub Desktop.
Save orjanv/0caf81b0abb6267f9c1a5e4b6caf7b0a to your computer and use it in GitHub Desktop.
Prøv ut Kaprekars rutine
from time import sleep
print("Vi prøver Kaprekar's rutine på vei mot konstanten 6174 og 495\n")
mitt_tall = input("Velg et tre eller firesifret tall, med minst to ulike siffer: ")
desc_a = "".join(sorted(mitt_tall, reverse=True))
asc_a = "".join(sorted(mitt_tall))
steg = 0
siffer = len(mitt_tall)
print(f"\nVi finner størst {desc_a} og minst tall {asc_a} fra sifrene og begynner rutinen...\n")
tall = "0"
while tall not in ("6174", "495"):
tall = str(int(desc_a) - int(asc_a))
tall = tall.zfill(siffer)
print(f"{desc_a} - {asc_a} = {tall}")
desc_a = "".join(sorted(tall, reverse=True))
asc_a = "".join(sorted(tall))
sleep(0.5)
steg += 1
print(f"\nVi har kommet frem til Kaprekar's: {tall} etter {steg} steg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment