-
Notifications
You must be signed in to change notification settings - Fork 0
/
_power.py
66 lines (50 loc) · 1.73 KB
/
_power.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
65
66
""" Finding the power of a number that, in the congruent equation, equals 1 or -1 with modulus M """
def main() -> list:
given_int, power, modulus = map(int, input().split())
cases = []
seq = modulus / 2
number = 1
for _ in range(1, power+1):
number *= given_int
while abs(number) > seq:
if __DEBUG == 1:
debug = number
if number < 0:
number += modulus
if __DEBUG == 1:
print(f'[DEBUG] operation: +\t power: {_:<5} old: {debug:<5} new: {number:<5}')
else:
number -= modulus
if __DEBUG == 1:
print(f'[DEBUG] operation: -\t power: {_:<5} old: {debug:<5} new: {number:<5}')
case = {f'{given_int}^{_} ≅ {number} (mod {modulus})'}
cases.append(case)
if __LIMIT == 1:
if number == -1 or number == 1:
break
if _ == power:
print(f'[DEBUG] Power {power} of given number is not enough to get 1 or -1!')
break
return cases
if __name__ == "__main__":
print('0 - Limit On | 1 - Limit off | 2 - Debug + Limit on | 3 - Debug only')
state = int(input('State (0 | 1 | 2 | 3)'))
# 0 - Limit On | 1 - Limit off | 2 - Debug + Limit | 3 - Debug only
match state:
case 0:
__LIMIT = 1
__DEBUG = 0
case 1:
__LIMIT = 0
__DEBUG = 0
case 2:
__LIMIT = 1
__DEBUG = 1
case 3:
__LIMIT = 0
__DEBUG = 1
case other:
__LIMIT = 1
__DEBUG = 0
for case in range(int(input())):
print(f'Case #{case+1}: {main()}')