From 82ff506c9823936c977ff6bea7fdb9ca7fc42281 Mon Sep 17 00:00:00 2001 From: Xinyu Zhou Date: Tue, 24 Jun 2025 08:59:35 +0800 Subject: [PATCH] random.randint(a, b) includes b, where b=4000 would cause exception (#425) * random.randint(a, b) includes b, where b=4000 would cause exception * math.factorial only works with integers starting python 3.9 * A constant equation does not have a stationary point --- mathgenerator/calculus.py | 7 ++++++- mathgenerator/misc.py | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mathgenerator/calculus.py b/mathgenerator/calculus.py index 42196c3..03d4745 100644 --- a/mathgenerator/calculus.py +++ b/mathgenerator/calculus.py @@ -82,7 +82,12 @@ def stationary_points(max_exp=3, max_coef=10): | $f(x)=6x^3 + 6x^2 + x + 8$ | ${- \frac{1}{3} - \frac{\sqrt{2}}{6}, - \frac{1}{3} + \frac{\sqrt{2}}{6}}$ | """ solution = '' - while len(solution) == 0: + + # A constant function has no stationary points, and the answer will be Reals. e.g.: + # x = sympy.symbols('x') + # s = sympy.stationary_points(1 + 0 * x , x) + # assert s == sympy.Reals + while solution == sympy.Reals or len(solution) == 0: x = sympy.symbols('x') problem = 0 for exp in range(max_exp + 1): diff --git a/mathgenerator/misc.py b/mathgenerator/misc.py index 80b6b13..15bf329 100644 --- a/mathgenerator/misc.py +++ b/mathgenerator/misc.py @@ -97,6 +97,9 @@ def base_conversion(max_num=60000, max_base=16): def _newton_symbol(n, k): """Utility of binomial_distribution()""" + # math.factorial only works with integers starting python 3.9 + # ref: https://docs.python.org/3/library/math.html#math.factorial + n, k = int(n), int(k) return math.factorial(n) / (math.factorial(k) * math.factorial(n - k)) @@ -190,13 +193,14 @@ def complex_to_polar(min_real_imaginary_num=-20, max_real_imaginary_num=20): return problem, f'${theta}$' -def decimal_to_roman_numerals(max_decimal=4000): +def decimal_to_roman_numerals(max_decimal=3999): """Decimal to Roman Numerals | Ex. Problem | Ex. Solution | | --- | --- | | The number $92$ in roman numerals is: | $XCII$ | """ + assert 0 <= max_decimal <= 3999, f"max_decimal({max_decimal}) must be <= 3999" x = random.randint(0, max_decimal) x_copy = x roman_dict = {