-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfind_lowest_base.py
64 lines (51 loc) · 1.86 KB
/
find_lowest_base.py
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
"""
Program to find the minimum base, where a entered decimal number can be represented using all same digits
"""
see_steps = True
def main(m):
def get_n():
"""
function to determine max number of times a decimal number has to be divided to get the Quotient as 0
:return:
"""
quo = m
Xn = 1
while quo > 0: # can also be written as while quo, this is beginner friendly
quo //= b ** Xn # performing integer division
Xn += 1
return Xn
def try_n(n):
"""
try to find if all digits for the base b representation of decimal number m are same.
:param n: the n, determined by the get_n function
:return: boolean, the common digit (only useful when boolean part is True)
"""
last_quo = m // (b ** n) # this should be 0
K = m % b
if last_quo == 0:
pass
else:
return False, K
for i in range(1, n):
if ((m // b ** i) % b) == K:
pass
else:
resid = (m // b ** i) % b
if see_steps:
print(f"\t{resid=} != {K}, at {i=}")
return False, K
return True, K
for b in range(2, 10): # iterate from 2 to 10 as given base is 10
max_n = get_n()
if see_steps:
print(f"\nBase: {b}\nMax steps: {max_n}")
res, common = try_n(max_n)
if res: # check if the returned boolean part is True
if not see_steps:
print(f"\nBase: {b}\nMax steps: {max_n}")
print("Minimum base is:", b, f"Base ({b}) representation: {f'{common}' * max_n}")
return
print(f"{m} cannot be represented using all same digits.")
if __name__ == '__main__':
main(int(input("Enter the decimal number:")))
# @github.com/tuhin-thinks