Browse Source

Refactor small_factors

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.12.0
MegaIng 3 years ago
parent
commit
3436b33885
1 changed files with 3 additions and 9 deletions
  1. +3
    -9
      lark/utils.py

+ 3
- 9
lark/utils.py View File

@@ -379,18 +379,12 @@ def small_factors(n):
Currently, we also keep a + b <= SMALL_FACTOR_THRESHOLD, but that might change
"""
assert n >= 0
if n < SMALL_FACTOR_THRESHOLD:
if n <= SMALL_FACTOR_THRESHOLD:
return [(n, 0)]
# While this does not provide an optimal solution, it produces a pretty good one.
# See above comment and PR #949
for a in range(SMALL_FACTOR_THRESHOLD, 1, -1):
b = n % a
if a + b > SMALL_FACTOR_THRESHOLD:
continue
r = n // a
assert r * a + b == n # Sanity check
if r <= SMALL_FACTOR_THRESHOLD:
return [(r, 0), (a, b)]
else:
r, b = divmod(n, a)
if a + b <= SMALL_FACTOR_THRESHOLD:
return small_factors(r) + [(a, b)]
assert False, "Failed to factorize %s" % n

Loading…
Cancel
Save