-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaprekar.py
More file actions
28 lines (20 loc) · 795 Bytes
/
kaprekar.py
File metadata and controls
28 lines (20 loc) · 795 Bytes
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
"""
Kaprekar constant, or 6174, is a constant that arises when we take a 4-digit integer,
form the largest and smallest numbers from its digits, and then subtract these two numbers.
Continuing with this process of forming and subtracting, we will always arrive at the number 6174.
"""
import random
def kaprekar(num : int) -> None:
if num == 6174: return
num = int(''.join(sorted(str(num), reverse=True)))
rev = int(''.join(sorted(str(num))))
intermediate_num = num - rev
print(f" {num} - {rev} = {intermediate_num}")
kaprekar(intermediate_num)
if __name__ == '__main__':
"""
Generates a random four digit number and applys the
algorithm to make it reach 6174 and shows the steps.
"""
number = random.randint(1000,9999)
kaprekar(number)