Skip to content

Commit 8021491

Browse files
authored
Update AdvDiv.py
Dividing first number with long decimal part much more efficiently, try, for example, `AdvDiv(123.15555, 7.54, 24)`.
1 parent 5200250 commit 8021491

File tree

1 file changed

+81
-77
lines changed

1 file changed

+81
-77
lines changed

AdvDiv.py

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,84 @@
11
import re
22
def div(n1, n2, r = 0, rstr1 = "[", rstr2 = "]"):
3-
if float(n2)==float(0):
4-
return False
5-
sign = "-" if ((float(n2) >= 0) if (float(n1) < 0) else (float(n2) < 0)) else ""
6-
n1 = abs(float(n1))
7-
n2 = abs(float(n2))
8-
r = abs(int(r))
9-
def times10(nstring):
10-
if nstring.endswith(".0"):
11-
nstring = nstring[:-2]
12-
if(nstring=="0"):
13-
return "0"
14-
if "." in nstring and nstring.find(".")==len(nstring) - 2:
15-
return nstring.replace(".", "")
16-
if "." in nstring:
17-
return nstring.split(".")[0] + nstring.split(".")[1][0] + "." + nstring.split(".")[1][1:]
18-
return nstring + "0"
19-
n1string = str(n1)[:-2] if str(n1).endswith(".0") else str(n1)
20-
n2string = str(n2)[:-2] if str(n2).endswith(".0") else str(n2)
21-
while "." in n1string or "." in n2string:
22-
if not "." in n1string:
23-
n1string+= str(r)[0]
24-
if len(str(r)) > 1:
25-
r = int(str(r)[1:] + str(r)[0])
26-
elif "." in times10(n1string):
27-
n1string = times10(n1string)+str(r)[0]
28-
if len(str(r)) > 1:
29-
r = int(str(r)[1:] + str(r)[0])
30-
else:
31-
n1string = times10(n1string)
32-
n2string = times10(n2string)
33-
n1 = int(n1string)
34-
n2 = int(n2string)
35-
res = ""
36-
n1s = list(n1string)
37-
n1s1 = list(n1string.split(".")[0])
38-
carry = 0
39-
newcarry = 0
40-
over = False
41-
i = 0
42-
for v in n1s1:
43-
res+= str((int(times10(str(carry))) + int(v)) // n2)
44-
carry = (int(times10(str(carry))) + int(v)) % n2
45-
i+= 1
46-
if res=="":
47-
res = "0"
48-
i+= 1
49-
doti = i
50-
res+= "."
51-
carries = [carry]
52-
i+= 1
53-
n1s.append("")
54-
while True:
55-
if(i >= len(n1s)):
56-
if "rcount" in vars():
57-
rcount+= 1
58-
else:
59-
rcount = 0
60-
over = True
61-
n1s.append(int(list(str(r))[rcount % len(str(r))]))
62-
newcarry = (int(times10(str(carry))) + int(n1s[i])) % n2
63-
if newcarry==0 and r==0:
64-
res+= str((int(times10(str(carry))) + int(n1s[i])) // n2)
65-
return sign + re.sub("^$", "0", re.sub("^\.", "0.", res.strip("0").rstrip(".")))
66-
x = 0
67-
while x < len(carries):
68-
if carries[x]==newcarry and (x % len(str(r)))==((i - doti) % len(str(r))):
69-
res+= str((int(times10(str(carry))) + int(n1s[i])) // n2)
70-
return sign + re.sub("^$", "0", re.sub("^\.", "0.", re.sub("^0+", "", "".join([res[:doti + carries.index(newcarry) + 1], rstr1, res[doti + carries.index(newcarry) + 1:], rstr2]))))
71-
x+= 1
72-
res+= str((int(times10(str(carry))) + int(n1s[i])) // n2)
73-
carry = newcarry
74-
if over:
75-
carries.append(carry)
76-
i+= 1
3+
if float(n2)==float(0):
4+
return False
5+
sign = "-" if ((float(n2) >= 0) if (float(n1) < 0) else (float(n2) < 0)) else ""
6+
n1 = abs(float(n1))
7+
n2 = abs(float(n2))
8+
r = abs(int(r))
9+
def times10(nstring):
10+
if nstring.endswith(".0"):
11+
nstring = nstring[:-2]
12+
if(nstring=="0"):
13+
return "0"
14+
if "." in nstring and nstring.find(".")==len(nstring) - 2:
15+
return nstring.replace(".", "")
16+
if "." in nstring:
17+
return nstring.split(".")[0] + nstring.split(".")[1][0] + "." + nstring.split(".")[1][1:]
18+
return nstring + "0"
19+
n1string = str(n1)[:-2] if str(n1).endswith(".0") else str(n1)
20+
n2string = str(n2)[:-2] if str(n2).endswith(".0") else str(n2)
21+
while "." in n2string:
22+
if not "." in n1string:
23+
n1string+= str(r)[0]
24+
if len(str(r)) > 1:
25+
r = int(str(r)[1:] + str(r)[0])
26+
elif "." in times10(n1string):
27+
n1string = times10(n1string)+str(r)[0]
28+
if len(str(r)) > 1:
29+
r = int(str(r)[1:] + str(r)[0])
30+
else:
31+
n1string = times10(n1string)
32+
n2string = times10(n2string)
33+
n1 = float(n1string)
34+
n2 = int(n2string)
35+
res = ""
36+
n1s = list(n1string)
37+
n1s1 = list(n1string.split(".")[0])
38+
carry = 0
39+
newcarry = 0
40+
over = False
41+
i = 0
42+
for v in n1s1:
43+
res+= str((int(times10(str(carry))) + int(v)) // n2)
44+
carry = (int(times10(str(carry))) + int(v)) % n2
45+
i+= 1
46+
if res=="":
47+
res = "0"
48+
i+= 1
49+
doti = i
50+
res+= "."
51+
carries = []
52+
i+= 1
53+
rcount = -1
54+
if not "." in n1s:
55+
n1s.append(".")
56+
while True:
57+
if(i >= len(n1s)):
58+
rcount+= 1
59+
over = True
60+
n1s.append(int(list(str(r))[rcount % len(str(r))]))
61+
newcarry = (int(times10(str(carry))) + int(n1s[i])) % n2
62+
if over and newcarry==0 and r==0:
63+
res+= str((int(times10(str(carry))) + int(n1s[i])) // n2)
64+
return sign + re.sub("^$", "0", re.sub("^\.", "0.", res.strip("0").rstrip(".")))
65+
x = 0
66+
while x < len(carries):
67+
if over and carries[x]==newcarry and (x % len(str(r)))==((rcount + 1) % len(str(r))):
68+
res+= str((int(times10(str(carry))) + int(n1s[i])) // n2)
69+
result = sign + re.sub("^$", "0", re.sub("^\.", "0.", re.sub("^0+", "", res[:i - rcount + x] + "[" + res[i - rcount + x:] + "]")))
70+
if result[result.index("[") - 1]==result[result.index("]") - 1]:
71+
result = result[:result.index("[") - 1] + "[" + result[result.index("[") - 1] + result[result.index("[") + 1:result.index("]") - 1] + "]"
72+
if result.index("]")==result.index("[") + 3 and result[result.index("[") + 1]==result[result.index("[") + 2]:
73+
result = result[:result.index("[") + 1] + result[result.index("[") + 2:]
74+
return result.replace("[", rstr1).replace("]", rstr2)
75+
x+= 1
76+
res+= str((int(times10(str(carry))) + int(n1s[i])) // n2)
77+
if over:
78+
carries.append(carry)
79+
carry = newcarry
80+
i+= 1
7781
if __name__ == "__main__":
78-
import sys
79-
if len(sys.argv) > 2:
80-
print(div(sys.argv[1], sys.argv[2], sys.argv[3] if len(sys.argv) > 3 else 0, sys.argv[4] if len(sys.argv) > 4 else "[", sys.argv[5] if len(sys.argv) > 5 else "]"))
82+
import sys
83+
if len(sys.argv) > 2:
84+
print(div(sys.argv[1], sys.argv[2], sys.argv[3] if len(sys.argv) > 3 else 0, sys.argv[4] if len(sys.argv) > 4 else "[", sys.argv[5] if len(sys.argv) > 5 else "]"))

0 commit comments

Comments
 (0)