-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture.py
More file actions
82 lines (65 loc) · 2.09 KB
/
lecture.py
File metadata and controls
82 lines (65 loc) · 2.09 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
82
def char2utf8(input_character):
input_unicode = ord(input_character)
input_binary = bin(input_unicode)
input_binary_without_0b = input_binary[2:]
if len(input_binary_without_0b) <= 7:
return hex(input_unicode)
else:
z = input_binary_without_0b[-6:]
if len(input_binary_without_0b) <= 11:
pass
else:
y = input_binary_without_0b[-12:-6]
return hex(int(
'0b1110{:0>4}10{}10{}'.format(
input_binary_without_0b[-16:-12],
y,
z) if len(input_binary_without_0b) <= 16 else None,
2))
print(char2utf8('台'))
print(char2utf8('台') == '0xe58fb0')
print(char2utf8(' '))
print(char2utf8(' ') == '0x20')
def exp_mod(b, d, n):
d_binary = bin(d)
d_binary_without_0b = d_binary[2:]
b_j = b
b_d = 1
for d_j in reversed(d_binary_without_0b):
if d_j == '1':
b_d = (b_d * b_j) % n
b_j = (b_j ** 2) % n
return b_d
print(exp_mod(10, 5, 91))
print(exp_mod(82, 29, 91))
def exp_mod_while(b, d, n):
b_j = b
b_d = 1
while d:
if d % 2:
b_d = (b_d * b_j) % n
b_j = (b_j ** 2) % n
d //= 2
return b_d
print(exp_mod_while(10, 5, 91))
print(exp_mod_while(82, 29, 91))
def get_quotient_remainder_list(a, b):
u"""Assume a >= b"""
quotient_remainder_list = [divmod(a, b)]
while quotient_remainder_list[-1][1] != 0:
quotient_remainder_list.append(divmod(
quotient_remainder_list[-2][1] if len(quotient_remainder_list) > 1 else b,
quotient_remainder_list[-1][1]))
return quotient_remainder_list
def gcd_while(a, b):
if a < b:
return gcd_while(b, a)
remainders = [a % b]
while remainders[-1] != 0:
remainders.append(
(remainders[-2] if len(remainders) > 1 else b) % remainders[-1])
return \
remainders[-2] if len(remainders) > 1 else \
b
print(gcd_while(72, 5))
print(gcd_while(5, 72))