j-js commited on
Commit
cf11a47
·
verified ·
1 Parent(s): d72f959

Update solver_factorial.py

Browse files
Files changed (1) hide show
  1. solver_factorial.py +24 -24
solver_factorial.py CHANGED
@@ -8,48 +8,48 @@ from models import SolverResult
8
 
9
 
10
  def solve_factorial(text: str) -> Optional[SolverResult]:
11
- lower = (text or "").lower()
 
12
 
13
- if "!" not in text and "factorial" not in lower:
14
  return None
15
 
16
- # n!
17
- m = re.search(r"(\d+)\s*!", text)
18
  if m:
19
  n = int(m.group(1))
20
- if n < 0:
21
- return None
22
- result = math.factorial(n)
 
 
 
23
  return SolverResult(
24
  domain="quant",
25
  solved=True,
26
- topic="factorial",
27
- answer_value=str(result),
28
- internal_answer=str(result),
29
  steps=[
30
- "Use n! = n × (n−1) × ... × 1.",
 
 
31
  ],
32
  )
33
 
34
- # "how many trailing zeros in 100!"
35
- m = re.search(r"trailing zeros.*?(\d+)\s*!", lower)
36
  if m:
37
  n = int(m.group(1))
38
- count = 0
39
- power = 5
40
- while power <= n:
41
- count += n // power
42
- power *= 5
43
  return SolverResult(
44
  domain="quant",
45
  solved=True,
46
- topic="factorial_trailing_zeros",
47
- answer_value=str(count),
48
- internal_answer=str(count),
49
  steps=[
50
- "Trailing zeros come from factors of 10 = 2×5.",
51
- "There are always more 2s than 5s in a factorial.",
52
- "Count factors of 5 using n//5 + n//25 + n//125 + ...",
53
  ],
54
  )
55
 
 
8
 
9
 
10
  def solve_factorial(text: str) -> Optional[SolverResult]:
11
+ raw = text or ""
12
+ lower = raw.lower()
13
 
14
+ if "!" not in raw and "factorial" not in lower and "trailing zero" not in lower:
15
  return None
16
 
17
+ # trailing zeros in n!
18
+ m = re.search(r"trailing zeros?.*?(\d+)\s*!", lower)
19
  if m:
20
  n = int(m.group(1))
21
+ count = 0
22
+ power = 5
23
+ while power <= n:
24
+ count += n // power
25
+ power *= 5
26
+
27
  return SolverResult(
28
  domain="quant",
29
  solved=True,
30
+ topic="factorial_trailing_zeros",
31
+ answer_value=str(count),
32
+ internal_answer=str(count),
33
  steps=[
34
+ "Trailing zeros come from factors of 10.",
35
+ "Each 10 contributes one 2 and one 5.",
36
+ "In factorials, 5s are the limiting factor, so count factors of 5.",
37
  ],
38
  )
39
 
40
+ # direct factorial value
41
+ m = re.search(r"(\d+)\s*!", raw)
42
  if m:
43
  n = int(m.group(1))
44
+ result = math.factorial(n)
 
 
 
 
45
  return SolverResult(
46
  domain="quant",
47
  solved=True,
48
+ topic="factorial",
49
+ answer_value=str(result),
50
+ internal_answer=str(result),
51
  steps=[
52
+ "Expand the factorial as descending multiplication.",
 
 
53
  ],
54
  )
55