-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomptage.py
119 lines (98 loc) · 3.34 KB
/
comptage.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
import math
def one(s, val):
yield s, val
if val != 0:
yield "-"+s, -val
if val > -1:
if val < 8 and val != 1 and val != 2:
fac = math.factorial(val)
yield s+"!", fac
if val > 1:
sqrt = np.int(math.sqrt(val))
# For python 3.8 : sqrt = math.isqrt(val)
if sqrt**2 == val:
yield "√"+s, sqrt
yield "-√"+s, -sqrt
def multi(s1, val1, s2, val2):
if s2[0] != "-":
yield f"({s1} + {s2})", val1+val2
yield f"({s1} - {s2})", val1-val2
if val1 > 0 or val2 > 0:
yield f"({s1} * {s2})", val1*val2
if val2 != 0:
v = val1/val2
vi = np.int(v)
if vi == v:
yield f"({s1} / {s2})", vi
# if val1 == val2:
# yield f"{s1} = {s2}", val1
def full(s, val):
for so, valo in one(s, val): yield so, valo
for i in range(1, len(s)):
s1 = s[:i]
s2 = s[i:]
val1 = int(s1)
val2 = int(s2)
ss1 = np.empty((5*32**len(s1)-1,), dtype="U25")
vals1 = np.empty((5*32**len(s1)-1,), dtype="int_")
n1 = 0
for n1, (so, valo) in enumerate(full(s1, val1)):
ss1[n1] = so
vals1[n1] = valo
ss2 = np.empty((5*32**len(s2)-1,), dtype="U25")
vals2 = np.empty((5*32**len(s2)-1,), dtype="int_")
n2 = 0
for n2, (so, valo) in enumerate(full(s2, val2)):
ss2[n2] = so
vals2[n2] = valo
for i1 in range(n1+1):
for i2 in range(n2+1):
for si, vali in multi(ss1[i1], vals1[i1], ss2[i2], vals2[i2]):
for so, valo in one(si, vali): yield so, valo
def generate(x):
s = str(x)
for i in range(1, len(s)):
s1 = s[:i]
s2 = s[i:]
val1 = int(s1)
val2 = int(s2)
ss1 = np.empty((5*32**len(s1)-1,), dtype="U25")
vals1 = np.empty((5*32**len(s1)-1,), dtype="int_")
n1 = 0
for n1, (so, valo) in enumerate(full(s1, val1)):
ss1[n1] = so
vals1[n1] = valo
ss1.resize((n1,))
vals1.resize((n1, ))
sorted1 = vals1.argsort()
ss1 = ss1[sorted1]
vals1 = vals1[sorted1]
ss2 = np.empty((5*32**len(s2)-1,), dtype="U25")
vals2 = np.empty((5*32**len(s2)-1,), dtype="int_")
n2 = 0
for n2, (so, valo) in enumerate(full(s2, val2)):
ss2[n2] = so
vals2[n2] = valo
ss2.resize((n2,))
vals2.resize((n2, ))
sorted2 = vals2.argsort()
ss2 = ss2[sorted2]
vals2 = vals2[sorted2]
i1 = i2 = 0
b1 = b2 = 0
while i1 < n1 and i2 < n2:
if vals1[i1] == vals2[i2] and vals1[i1] >= 0:
b1 = i1
b2 = i2
while i1 < n1 and vals1[i1] == vals2[i2]:
i1 += 1
while i2 < n2 and vals2[i2] == vals1[i1-1]:
i2 += 1
for a1 in range(b1, i1):
for a2 in range(b2, i2):
yield vals1[a1], f"{ss1[a1]} = {ss2[a2]}"
elif vals1[i1] < vals2[i2]:
i1 += 1
else:
i2 += 1