-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaprekan_func.py
More file actions
81 lines (70 loc) · 2.22 KB
/
kaprekan_func.py
File metadata and controls
81 lines (70 loc) · 2.22 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def kaprekar_routine(num):
""" Returns the fix points or constants for the Kaprekar
routine from Dattrateya Ramachanda Kraprekar (1949)
only for base 10.
Substract the biggest possible number created from digits
of input number to the smallest possible.
N_digits | max steps | cycle/Kaprekar constant
----------------------------------------------
4 | 8 | 6174
3 | ? | 495
2 | ? | 09-81-63-27-45
Source: https://en.wikipedia.org/wiki/Kaprekar%27s_routine
"""
sequence = list([num])
n_digits = len(str(num))
n_steps = 1
while True: # two exit conditions
snum = f'{num:0{n_digits}}'
if len(set(snum)) == 1:
# exit if all digits are equal
msg = "All digits are the same. Last number is 0!"
print("WARN:", msg)
sequence.append(0)
return sequence, n_steps
biggest = int("".join( sorted(snum, reverse=True, key=int) ))
smallest = int("".join(sorted(snum, key=int)))
num = biggest - smallest
if num in sequence:
# if got a existing one, it will loop
msg = f"Next number {num} already in sequence!"
print("WARN:", msg)
return sequence, n_steps
sequence.append(num)
n_steps += 1
def kaprekar_carpet():
# TODO: Kaprekar carpet (image)
# https://youtu.be/pDXek06Bde4?t=161
pass
# TODO: Other kaprekar routines:
# https://en.wikipedia.org/wiki/D._R._Kaprekar
#
# example useless
#
print("#### EXAMPLE USELESS ####")
for n in [22, 111]:
k_ser = kaprekar_routine(n)
print(
f"kaprekar_routine({n}) - Steps: {k_ser[1]}\n",
k_ser[0], '\n'
)
#
# example cycles
#
print("#### EXAMPLE CYCLES ####")
for n in [24, 121, 13451, 123456]:
k_ser = kaprekar_routine(n)
print(
f"kaprekar_routine({n}) - Steps: {k_ser[1]}\n",
k_ser[0], '\n'
)
#
# example constants
#
print("#### EXAMPLE CONSTANTS ####")
for n in [0, 1234, 549945, 631764, 63317664, 97508421, 554999445]:
k_ser = kaprekar_routine(n)
print(
f"kaprekar_routine({n}) - Steps: {k_ser[1]}\n",
k_ser[0], '\n'
)