### Install mathgenerator with pip Source: https://lukew3.github.io/mathgenerator Installs the mathgenerator package via pip. ```bash pip install mathgenerator ``` -------------------------------- ### Install mathgenerator via pip Source: https://lukew3.github.io/mathgenerator/mathgenerator.html Install the mathgenerator package using pip. ```bash pip install mathgenerator ``` -------------------------------- ### Binary to Hexadecimal (binary_to_hexidecimal) Source: https://lukew3.github.io/mathgenerator/mathgenerator/computer_science.html Generates a problem asking for the hexadecimal representation of a binary string and returns the solution as a hex string prefixed with '0x'. The example shows '010101' converting to '0x15'. ```python def binary_to_hexidecimal(): r"""Binary to Hexidecimal | Ex. Problem | Ex. Solution | | --- | --- | | 010101 | 0x15 | """ bin = random.randint(0, 1000) bin = bin(bin)[2:] hex = hex(int(bin, 2)) problem = bin solution = hex return problem, solution ``` -------------------------------- ### Fibonacci Series (fibonacci_series) Source: https://lukew3.github.io/mathgenerator/mathgenerator/computer_science.html Generates a problem asking for the first n Fibonacci numbers and returns the solution as a comma-separated string. The example shows the first 10 numbers as 0,1,1,2,3,5,8,13,21. ```python def fibonacci_series(min_no=1): r"""Fibonacci Series | Ex. Problem | Ex. Solution | | --- | --- | | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | """ n = random.randint(min_no, 20) def createFibList(n): list = [] for i in range(n): if i < 2: list.append(i) else: val = list[i - 1] + list[i - 2] list.append(val) return list fibList = createFibList(n) problem = "The Fibonacci Series of the first ${n}$ numbers is ?" solution = ', '.join(map(str, fibList)) return problem, f'${solution}$' ``` -------------------------------- ### Get list of all generators with getGenList() Source: https://lukew3.github.io/mathgenerator/mathgenerator.html Returns a list of all generators in the format [funcname, subjectname]. ```python [funcname, subjectname] ``` -------------------------------- ### mathgenerator package source (__init__.py) Source: https://lukew3.github.io/mathgenerator Full source of the mathgenerator package's __init__.py, showing imports, the get_gen_list() function, gen_by_id(), and legacy functions getGenList() and genById(). ```python 1""" 2.. include:: ../README.md 3""" 4 5from .algebra import * 6from .basic_math import * 7from .calculus import * 8from .computer_science import * 9from .geometry import * 10from .misc import * 11from .statistics import * 12 13from ._gen_list import gen_list 14 15 16# [funcname, subjectname] 17def get_gen_list(): 18 return gen_list 19 20def gen_by_id(id, *args, **kwargs): 21 return globals()[gen_list[id][0]](*args, **kwargs) 22 23 24# Legacy Functions 25def getGenList(): 26 return gen_list 27 28 29def genById(id, *args, **kwargs): 30 return globals()[gen_list[id][0]](*args, **kwargs) ``` -------------------------------- ### profit_loss_percent: Generate profit/loss percent problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates random cost price (1 to max_cp, default 1000) and selling price (1 to max_sp, default 1000) and returns a problem asking for the profit or loss percent, with the solution rounded to two decimals. Determines whether it is profit or loss based on the selling price relative to cost. ```python def profit_loss_percent(max_cp=1000, max_sp=1000): """Profit or Loss Percent | Ex. Problem | Ex. Solution | | --- | --- | | Loss percent when $CP = 751$ and $SP = 290$ is: | $61.38$ | """ cP = random.randint(1, max_cp) sP = random.randint(1, max_sp) diff = abs(sP - cP) if (sP - cP >= 0): profitOrLoss = "Profit" else: profitOrLoss = "Loss" percent = round(diff / cP * 100, 2) problem = f"{profitOrLoss} percent when $CP = {cP}$ and $SP = {sP}$ is: " return problem, f'${percent}$' ``` -------------------------------- ### basic_algebra: Generate ax + b = c problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a basic algebra problem of the form ax + b = c and its solution. Uses a GCD calculation to simplify the solution fraction, and handles the cases where the solution is zero or a whole number. ```python def basic_algebra(max_variable=10): r"""Basic Algebra | Ex. Problem | Ex. Solution | | --- | --- | | $1x + 5 = 5$ | $0$ | """ a = random.randint(1, max_variable) b = random.randint(1, max_variable) c = random.randint(b, max_variable) # calculate gcd def calculate_gcd(x, y): while (y): x, y = y, x % y return x i = calculate_gcd((c - b), a) x = f"{(c - b)//i}/{a//i}" if (c - b == 0): x = "0" elif a == 1 or a == i: x = f"{c - b}" problem = f"${a}x + {b} = {c}$" solution = f"${x}$" return problem, solution ``` -------------------------------- ### Generate logarithm problem and solution Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a logarithm problem with a random base and value, returning the problem and solution as LaTeX strings. ```python def log(max_base=3, max_val=8): r"""Logarithm | Ex. Problem | Ex. Solution | | --- | --- | | $log_{3}(243)=$ | $5$ | """ a = random.randint(1, max_val) b = random.randint(2, max_base) c = pow(b, a) problem = f'$log_{{{b}}}({c})=$' solution = f'${a}$' return problem, solution ``` -------------------------------- ### Fibonacci Series Generation (createFibList and fibonacci_series) Source: https://lukew3.github.io/mathgenerator/mathgenerator/computer_science.html Generates a Fibonacci series problem. The createFibList helper builds the list up to n terms, and the main function returns a problem asking for the series of the first n numbers and the solution as a comma-separated string. ```python def createFibList(n): list = [] for i in range(n): if i < 2: list.append(i) else: val = list[i - 1] + list[i - 2] list.append(val) return list fibList = createFibList(n) problem = "The Fibonacci Series of the first ${n}$ numbers is ?" solution = ', '.join(map(str, fibList)) return problem, f'${solution}$' ``` -------------------------------- ### fraction_to_decimal function Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Generates a fraction-to-decimal problem by dividing two random integers and rounding to two decimal places. Returns LaTeX strings for the problem and the decimal solution. ```python def fraction_to_decimal(max_res=99, max_divid=99): r"""Fraction to Decimal | Ex. Problem | Ex. Solution | | --- | --- | | $83\div80=$ | $1.04$ | """ a = random.randint(0, max_divid) b = random.randint(1, min(max_res, max_divid)) c = round(a / b, 2) return rf'${a}\div{b}=$', f'${c}$' ``` -------------------------------- ### fraction_to_decimal(max_res=99, max_divid=99) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate a fraction-to-decimal problem and solution. ```APIDOC ## fraction_to_decimal(max_res=99, max_divid=99) ### Description Generates a fraction-to-decimal problem and its solution. ### Parameters - **max_res** (int) - Optional - Maximum result value (default: 99) - **max_divid** (int) - Optional - Maximum dividend value (default: 99) ### Returns - **problem** (str) - The problem string in LaTeX format - **solution** (str) - The solution string in LaTeX format ``` -------------------------------- ### minutes_to_hours: Generate time conversion problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a random number of minutes (1 to max_minutes, default 999) and returns a problem asking to convert it to hours and minutes, with the solution using integer division and modulo. ```python def minutes_to_hours(max_minutes=999): """Convert minutes to hours and minutes | Ex. Problem | Ex. Solution | | --- | --- | | Convert $836$ minutes to hours & minutes | $13$ hours and $56$ minutes | """ minutes = random.randint(1, max_minutes) ansHours = minutes // 60 ansMinutes = minutes % 60 problem = f"Convert ${minutes}$ minutes to hours & minutes" solution = f"${ansHours}$ hours and ${ansMinutes}$ minutes" return problem, solution ``` -------------------------------- ### fraction_to_decimal(max_res=99, max_divid=99) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate a fraction to decimal conversion problem and its solution. ```APIDOC ## fraction_to_decimal(max_res=99, max_divid=99) ### Description Generates a fraction to decimal conversion problem. ### Parameters - **max_res** (int) - Optional - Maximum value for the denominator (default: 99). - **max_divid** (int) - Optional - Maximum value for the numerator (default: 99). ### Returns - **problem** (str) - The problem string in LaTeX format, e.g., `$83\div80=$`. - **solution** (str) - The solution string in LaTeX format, e.g., `$1.04$`. ``` -------------------------------- ### basic_trigonometry(angles=[0, 30, 45, 60, 90], functions=['sin', 'cos', 'tan']) Source: https://lukew3.github.io/mathgenerator/mathgenerator/geometry.html Generates a trigonometry problem asking for the value of a trigonometric function at a given angle, and returns the problem and solution as a fraction or radical. ```APIDOC ## basic_trigonometry(angles=[0, 30, 45, 60, 90], functions=['sin', 'cos', 'tan']) ### Description Generates a trigonometry problem asking for the value of a trigonometric function (sin, cos, tan) at a given angle (in degrees). Returns the problem and solution, where the solution is expressed as a fraction or radical (e.g., 1/2, sqrt(3)/2). ### Method Function call ### Endpoint `mathgenerator.geometry.basic_trigonometry` ### Parameters - **angles** (list of int) - Optional - List of angles (in degrees) to choose from. Default: [0, 30, 45, 60, 90] - **functions** (list of str) - Optional - List of trigonometric functions to choose from. Default: ['sin', 'cos', 'tan'] ### Returns - **problem** (str) - The problem statement, e.g., "$\sin(30) = $" - **solution** (str) - The solution in LaTeX format, e.g., "$\frac{1}{2}$" ### Example ```python problem, solution = basic_trigonometry() print(problem) # $\sin(30) = $ print(solution) # $\frac{1}{2}$ ``` ``` -------------------------------- ### data_summary(number_values=15, min_val=5, max_val=50) Source: https://lukew3.github.io/mathgenerator/mathgenerator/statistics.html Generates a problem asking for the mean, standard deviation, and variance of a random data set. Returns the problem and solution as strings. ```APIDOC ## data_summary(number_values=15, min_val=5, max_val=50) ### Description Generates a problem asking for the mean, standard deviation, and variance of a random data set. Returns the problem and solution as strings. ### Method Function call ### Endpoint data_summary(number_values=15, min_val=5, max_val=50) ### Parameters - **number_values** (int) - Optional - Number of values in the data set (default: 15). - **min_val** (int) - Optional - Minimum value for random data (default: 5). - **max_val** (int) - Optional - Maximum value for random data (default: 50). ### Returns - **problem** (str) - The problem statement. - **solution** (str) - The solution as a string. ### Example ```python problem, solution = data_summary() ``` ``` -------------------------------- ### Generate orthogonal projection problem with orthogonal_projection Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates an orthogonal projection problem for 2D vectors and returns the problem and solution strings. Uses fractions.Fraction to represent the projection coefficients as fractions, and formats them as LaTeX fractions when the denominator is not 1. ```python def orthogonal_projection(min_val=-10, max_val=10): r"""Orthogonal Projection | Ex. Problem | Ex. Solution | | --- | --- | | Find the orthogonal projection of $[2, 3]$ onto $[4, -7]$ | $[\frac{-4}{5}, \frac{7}{5}]$ | """ v = [random.randint(min_val, max_val) for _ in range(2)] u = [random.randint(min_val, max_val) for _ in range(2)] dot_t = v[0] * u[0] + v[1] * u[1] dot_b = u[0] * u[0] + u[1] * u[1] frac = fractions.Fraction(dot_t, dot_b).limit_denominator() y = [frac * u[0], frac * u[1]] if y[0].denominator != 1: y[0] = rf'\frac{{{y[0].numerator}}}{{{y[0].denominator}}}' if y[1].denominator != 1: y[1] = rf'\frac{{{y[1].numerator}}}{{{y[1].denominator}}}' problem = f'Find the orthogonal projection of ${v}$ onto ${u}$' solution = f'$[{y[0]}, {y[1]}]$' return problem, solution ``` -------------------------------- ### quotient_of_power_same_power function Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a problem and solution for the quotient of powers with the same power. Uses random bases and a random exponent, computes the ratio and raises it to the power, rounding the solution to two decimals. ```python def quotient_of_power_same_power(max_base=50, max_power=10): """Quotient of Powers with Same Power | Ex. Problem | Ex. Solution | | --- | --- | | The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$ | $169.84$ | """ base1 = random.randint(1, max_base) base2 = random.randint(1, max_base) power = random.randint(1, max_power) step = base1 / base2 solution = round(step ** power, 2) problem = f"The quotient of ${base1}^{{{power}}}$ and ${base2}^{{{power}}} = " \ f"({base1}/{base2})^{power} = {step}^{{{power}}}$" return problem, f'${solution}$' ``` -------------------------------- ### binomial_distribution - Generate binomial probability problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a binomial distribution word problem about rejected pistons, with random rejection percentage, batch size, and rejection count. Computes the cumulative probability using _newton_symbol and returns the answer as a percentage rounded to two decimals. ```python def binomial_distribution(): """Binomial distribution | Ex. Problem | Ex. Solution | | --- | --- | | A manufacturer of metal pistons finds that, on average, $30.56$% of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of $20$ pistons will contain no more than $2$ rejected pistons? | $3.17$ | """ rejected_fraction = float(random.randint(30, 40)) + random.random() batch = random.randint(10, 20) rejections = random.randint(1, 9) answer = 0 rejected_fraction = round(rejected_fraction, 2) for i in range(0, rejections + 1): answer += _newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \ ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i))) answer = round(100 * answer, 2) problem = "A manufacturer of metal pistons finds that, on average, ${0:}$% "\ "of the pistons they manufacture are rejected because " \ "they are incorrectly sized. What is the probability that a "\ "batch of ${1:}$ pistons will contain no more than ${2:}$ " \ "rejected pistons?".format(rejected_fraction, batch, rejections) return problem, f'${answer}$' ``` -------------------------------- ### quotient_of_power_same_power(max_base=50, max_power=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a problem for the quotient of powers with the same power. It returns a problem statement and its solution. ```APIDOC ## quotient_of_power_same_power(max_base=50, max_power=10) ### Description Generates a problem for the quotient of powers with the same power. It returns a problem statement and its solution. ### Parameters - **max_base** (int) - Optional - Maximum base value (default 50). - **max_power** (int) - Optional - Maximum power value (default 10). ### Returns - **problem** (str) - The problem statement. - **solution** (str) - The solution. ### Example ```python problem, solution = quotient_of_power_same_power() print(problem) # e.g., "The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$" print(solution) # e.g., "$169.84$" ``` ``` -------------------------------- ### quotient_of_power_same_base(max_base=50, max_power=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a problem for the quotient of powers with the same base. It returns a problem statement and its solution. ```APIDOC ## quotient_of_power_same_base(max_base=50, max_power=10) ### Description Generates a problem for the quotient of powers with the same base. It returns a problem statement and its solution. ### Parameters - **max_base** (int) - Optional - Maximum base value (default 50). - **max_power** (int) - Optional - Maximum power value (default 10). ### Returns - **problem** (str) - The problem statement. - **solution** (str) - The solution. ### Example ```python problem, solution = quotient_of_power_same_base() print(problem) # e.g., "The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$" print(solution) # e.g., "$0.04$" ``` ``` -------------------------------- ### Generate an addition problem with mathgenerator Source: https://lukew3.github.io/mathgenerator/mathgenerator.html Shows two ways to generate an addition problem: directly via mathgenerator.addition() and via mathgenerator.genById(0). The problem and solution are returned as a pair. ```python import mathgenerator #generate an addition problem problem, solution = mathgenerator.addition() #another way to generate an addition problem using genById() problem, solution = mathgenerator.genById(0) ``` -------------------------------- ### basic_algebra(max_variable=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a basic algebra problem of the form ax + b = c and returns the problem and solution as strings. ```APIDOC ## basic_algebra(max_variable=10) ### Description Generates a basic algebra problem of the form $ax + b = c$ and returns the problem and solution as strings. ### Parameters - **max_variable** (int) - Optional - Maximum value for the random coefficients. Default is 10. ### Returns - **problem** (str) - The generated problem string, e.g., "$1x + 5 = 5$". - **solution** (str) - The solution string, e.g., "$0$". ### Example ```python problem, solution = basic_algebra() print(problem) # $1x + 5 = 5$ print(solution) # $0$ ``` ``` -------------------------------- ### product_of_scientific_notations: Generate scientific notation product problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates two random numbers in scientific notation (mantissa 1-10, exponent between min_exp_val and max_exp_val, default -100 to 100) and returns a problem asking for their product, with the solution. Adjusts the result to keep the mantissa below 10. ```python def product_of_scientific_notations(min_exp_val=-100, max_exp_val=100): r"""Product of scientific notations | Ex. Problem | Ex. Solution | | --- | --- | | Product of scientific notations $5.11 \times 10^{67}$ and $3.64 \times 10^{-59} = $ | $1.86 \times 10^{9}$ | """ a = [round(random.uniform(1, 10), 2), random.randint(min_exp_val, max_exp_val)] b = [round(random.uniform(1, 10), 2), random.randint(min_exp_val, max_exp_val)] c = [a[0] * b[0], a[1] + b[1]] if c[0] >= 10: c[0] /= 10 c[1] += 1 problem = rf"Product of scientific notations ${a[0]} \times 10^{{{a[1]}}}$ and ${b[0]} \times 10^{{{b[1]}}} = $" solution = rf'${round(c[0], 2)} \times 10^{{{c[1]}}}$' return problem, solution ``` -------------------------------- ### combine_like_terms: Generate and combine polynomial terms Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a problem with random polynomial terms and combines like terms by summing coefficients with the same exponent. The solution is sorted by exponent and formatted as a LaTeX string. ```python def combine_like_terms(max_coef=10, max_exp=20, max_terms=10): r"""Combine Like Terms | Ex. Problem | Ex. Solution | | --- | --- | | $6x^{9} + 3x^{2} + 5x^{19} + 3x^{17}$ | $3x^{2} + 6x^{9} + 3x^{17} + 5x^{19}$ | """ numTerms = random.randint(1, max_terms) coefs = [random.randint(1, max_coef) for _ in range(numTerms)] exponents = [random.randint(1, max(max_exp - 1, 2)) for _ in range(numTerms)] problem = " + ".join([f"{coefs[i]}x^{{{exponents[i]}}}" for i in range(numTerms)]) d = {} for i in range(numTerms): if exponents[i] in d: d[exponents[i]] += coefs[i] else: d[exponents[i]] = coefs[i] solution = " + ".join([f"{d[k]}x^{{{k}}}" for k in sorted(d)]) return f'${problem}$', f'${solution}$' ``` -------------------------------- ### binary_to_decimal function Source: https://lukew3.github.io/mathgenerator/mathgenerator/computer_science.html Generates a random binary string and returns the decimal equivalent as the solution. The problem is the binary string. ```python def binary_to_decimal(max_dig=10): r"""Binary to Decimal | Ex. Problem | Ex. Solution | | --- | --- | | $000110$ | $6$ | """ problem = ''.join([str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) solution = f'${int(problem, 2)}$' return f'${problem}$', solution ``` -------------------------------- ### percentage(max_value=99, max_percentage=99) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate a percentage problem and solution. ```APIDOC ## percentage(max_value=99, max_percentage=99) ### Description Generates a percentage-of-a-number problem and its solution. ### Parameters - **max_value** (int) - Optional - Maximum value for the base number (default: 99) - **max_percentage** (int) - Optional - Maximum percentage value (default: 99) ### Returns - **problem** (str) - The problem string - **solution** (str) - The solution string in LaTeX format ``` -------------------------------- ### log(max_base=3, max_val=8) Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a logarithm problem with a random base and argument, returning the problem and solution. ```APIDOC ## log(max_base=3, max_val=8) ### Description Generates a logarithm problem with a random base and argument, returning the problem and solution. ### Method Function call ### Endpoint `log(max_base=3, max_val=8)` ### Parameters - **max_base** (int) - Optional - Maximum base value (default: 3) - **max_val** (int) - Optional - Maximum exponent value (default: 8) ### Return - **problem** (str) - The problem statement, e.g., "$log_{3}(243)=$" - **solution** (str) - The solution, e.g., "$5$" ### Example ```python problem, solution = log() ``` ``` -------------------------------- ### quotient_of_power_same_base function Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a problem and solution for the quotient of powers with the same base. Uses random base and exponents, computes the difference of exponents, and returns a formatted problem string and the numeric solution. ```python def quotient_of_power_same_base(max_base=50, max_power=10): """Quotient of Powers with Same Base | Ex. Problem | Ex. Solution | | --- | --- | | The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$ | $0.04$ | """ base = random.randint(1, max_base) power1 = random.randint(1, max_power) power2 = random.randint(1, max_power) step = power1 - power2 solution = base ** step problem = f"The Quotient of ${base}^{{{power1}}}$ and ${base}^{{{power2}}} = " \ f"{base}^{{{power1}-{power2}}} = {base}^{{{step}}}$" return problem, f'${solution}$' ``` -------------------------------- ### binary_to_decimal(max_dig=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/computer_science.html Generates a problem to convert a binary number to decimal and returns the problem and solution. ```APIDOC ## binary_to_decimal(max_dig=10) ### Description Generates a problem to convert a binary number to decimal. ### Parameters - **max_dig** (int) - Optional - Maximum number of digits (default 10) ### Returns - **problem** (str) - The problem statement, e.g., "$000110$" - **solution** (str) - The solution, e.g., "$6$" ``` -------------------------------- ### product_of_scientific_notations Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a product of scientific notations problem. Returns a tuple containing the problem statement and solution. ```APIDOC ## product_of_scientific_notations ### Description Generates a product of scientific notations problem. Returns a tuple containing the problem statement and solution. ### Method Function call ### Endpoint mathgenerator.misc.product_of_scientific_notations ### Parameters - **min_exp_val** (int) - Optional - Minimum exponent value (default: -100) - **max_exp_val** (int) - Optional - Maximum exponent value (default: 100) ### Response Returns a tuple of two strings: (problem, solution). ### Example ```python problem, solution = product_of_scientific_notations(min_exp_val=-100, max_exp_val=100) print(problem) print(solution) ``` ``` -------------------------------- ### combinations(max_lengthgth=20) Source: https://lukew3.github.io/mathgenerator/mathgenerator/statistics.html Generates a problem asking for the number of combinations from a random number of objects picked a random number at a time, and returns the problem and solution as strings. ```APIDOC ## combinations(max_lengthgth=20) ### Description Generates a problem asking for the number of combinations from a random number of objects picked a random number at a time, and returns the problem and solution as strings. ### Method Function call ### Endpoint combinations(max_lengthgth=20) ### Parameters - **max_lengthgth** (int) - Optional - Maximum value for the total number of objects (default: 20). ### Returns - **problem** (str) - The problem statement. - **solution** (str) - The solution as a string. ### Example ```python problem, solution = combinations() ``` ``` -------------------------------- ### profit_loss_percent(max_cp=1000, max_sp=1000) Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a problem asking for the profit or loss percent given cost price and selling price. Returns a tuple containing the problem statement and the solution. ```APIDOC ## profit_loss_percent(max_cp=1000, max_sp=1000) ### Description Generates a problem asking for the profit or loss percent given cost price and selling price. Returns a tuple containing the problem statement and the solution. ### Parameters - **max_cp** (int) - Optional - Maximum cost price (default 1000) - **max_sp** (int) - Optional - Maximum selling price (default 1000) ### Returns - **tuple** - A tuple (problem, solution) where problem is a string and solution is a string. ### Example ```python problem, solution = profit_loss_percent() print(problem) # e.g., "Loss percent when $CP = 751$ and $SP = 290$ is: " print(solution) # e.g., "$61.38$" ``` ``` -------------------------------- ### basic_trigonometry(angles=[0, 30, 45, 60, 90], functions=["sin", "cos", "tan"]) Source: https://lukew3.github.io/mathgenerator/mathgenerator/geometry.html Generates a problem asking for the value of a trigonometric function at a given angle and returns the problem and solution. ```APIDOC ## basic_trigonometry(angles=[0, 30, 45, 60, 90], functions=["sin", "cos", "tan"]) ### Description Generates a problem asking for the value of a trigonometric function at a given angle and returns the problem and solution. ### Parameters - **angles** (list of int) - Optional - List of angles in degrees to choose from. Default is [0, 30, 45, 60, 90]. - **functions** (list of str) - Optional - List of trigonometric functions to choose from. Default is ["sin", "cos", "tan"]. ### Returns - **problem** (str) - The problem statement, e.g., "$\sin(30) = $" - **solution** (str) - The solution, e.g., "$\frac{1}{2}$" ``` -------------------------------- ### compare_fractions(max_val=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate a fraction comparison problem and its solution. ```APIDOC ## compare_fractions(max_val=10) ### Description Generates a problem asking to compare two fractions and returns the correct comparison symbol (`>`, `<`, or `=`). The fractions are generated with numerators and denominators in the range 1 to `max_val`. ### Parameters - **max_val** (int) - Optional - Maximum value for numerators and denominators. Defaults to 10. ### Returns - **problem** (str) - A string representing the comparison problem in LaTeX format, e.g., `Which symbol represents the comparison between $\frac{1}{2}$ and $\frac{3}{4}$?`. - **solution** (str) - A string containing the comparison symbol, e.g., `>`. ``` -------------------------------- ### Generate simple interest problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a simple interest problem with random principal, rate, and time, and returns the calculated interest rounded to two decimal places. ```python def simple_interest(max_principle=10000, max_rate=10, max_time=10): r"""Simple Interest | Ex. Problem | Ex. Solution | | --- | --- | | Simple interest for a principle amount of $7217$ dollars, $3$% rate of interest and for a time period of $10$ years is $=$ | $2165.1$ | """ p = random.randint(1000, max_principle) r = random.randint(1, max_rate) t = random.randint(1, max_time) s = round((p * r * t) / 100, 2) problem = f"Simple interest for a principle amount of ${p}$ dollars, ${r}$% rate of interest and for a time period of ${t}$ years is $=$ " solution = f'${s}$' return problem, solution ``` -------------------------------- ### area_of_circle_given_center_and_point(max_coordinate=10, max_radius=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/geometry.html Generates a problem asking for the area of a circle given its center and a point on the circle, and returns the problem and solution strings. ```APIDOC ## area_of_circle_given_center_and_point(max_coordinate=10, max_radius=10) ### Description Generates a problem asking for the area of a circle given its center and a point on the circle, and returns the problem and solution strings. ### Parameters - **max_coordinate** (int) - Optional - Maximum absolute coordinate value for the center. Default is 10. - **max_radius** (int) - Optional - Maximum radius value. Default is 10. ### Returns - **problem** (str) - The problem statement, e.g., "Area of circle with center $(7,-6)$ and passing through $(1.0, -6.0)$ is". - **solution** (str) - The solution, e.g., "$113.1$". ### Example ```python problem, solution = area_of_circle_given_center_and_point() print(problem) # Area of circle with center $(7,-6)$ and passing through $(1.0, -6.0)$ is print(solution) # $113.1$ ``` ``` -------------------------------- ### basic_algebra(max_variable=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a basic algebra problem of the form ax + b = c and its solution for x. ```APIDOC ## basic_algebra(max_variable=10) ### Description Generates a basic algebra problem of the form $ax + b = c$ and its solution for $x$. ### Parameters - **max_variable** (int) - Optional - Maximum value for the coefficients and constants. Default is 10. ### Returns - **problem** (str) - The problem string in LaTeX format. - **solution** (str) - The solution string in LaTeX format. ### Example ```python problem, solution = basic_algebra() print(problem) # $1x + 5 = 5$ print(solution) # $0$ ``` ``` -------------------------------- ### factors(max_val=1000) - Generate factors problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/misc.html Generates a random integer up to max_val and returns a problem asking for its factors, with the sorted list of factors as the solution. Uses a loop up to the square root to find divisors efficiently. ```python def factors(max_val=1000): """Factors of a number | Ex. Problem | Ex. Solution | | --- | --- | | Factors of $176 = $ | $[1, 2, 4, 8, 11, 16, 22, 44, 88, 176]$ | """ n = random.randint(1, max_val) factors = [] for i in range(1, int(n**0.5) + 1): if i**2 == n: factors.append(i) elif n % i == 0: factors.append(i) factors.append(n // i) else: pass factors.sort() problem = f"Factors of ${n} = $" solution = factors return problem, f'${solution}$' ``` -------------------------------- ### invert_matrix: Generate random matrix inversion problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a random invertible matrix problem and its inverse solution. When only_integer_elements_in_inverted_matrixe is True, it constructs a matrix with integer elements and ensures the inverse also has integer elements; otherwise it uses prime numbers and random integers. Returns a problem string and a solution string formatted with LaTeX matrices. ```python def invert_matrix(square_matrix_dimension=3, max_matrix_element=99, only_integer_elements_in_inverted_matrixe=True): r"""Invert Matrix | Ex. Problem | Ex. Solution | | --- | --- | | Inverse of Matrix $\begin{bmatrix} 4 & 1 & 4 \\ 5 & 1 & 5 \\ 12 & 3 & 13 \end{bmatrix}$ is: | $\begin{bmatrix} 2 & 1 & -1 \\ 5 & -4 & 0 \\ -3 & 0 & 1 \end{bmatrix}$ | """ if only_integer_elements_in_inverted_matrixe is True: isItOk = False Mat = list() while (isItOk is False): Mat = list() for i in range(0, square_matrix_dimension): z = list() for j in range(0, square_matrix_dimension): z.append(0) z[i] = 1 Mat.append(z) MaxAllowedMatrixElement = math.ceil( pow(max_matrix_element, 1 / (square_matrix_dimension))) randomlist = random.sample(range(0, MaxAllowedMatrixElement + 1), square_matrix_dimension) for i in range(0, square_matrix_dimension): if i == square_matrix_dimension - 1: Mat[0] = [ j + (k * randomlist[i]) for j, k in zip(Mat[0], Mat[i]) ] else: Mat[i + 1] = [ j + (k * randomlist[i]) for j, k in zip(Mat[i + 1], Mat[i]) ] for i in range(1, square_matrix_dimension - 1): Mat[i] = [ sum(i) for i in zip(Mat[square_matrix_dimension - 1], Mat[i]) ] isItOk = True for i in Mat: for j in i: if j > max_matrix_element: isItOk = False break if isItOk is False: break random.shuffle(Mat) Mat = sympy.Matrix(Mat) Mat = sympy.Matrix.transpose(Mat) Mat = Mat.tolist() random.shuffle(Mat) Mat = sympy.Matrix(Mat) Mat = sympy.Matrix.transpose(Mat) else: randomlist = list(sympy.primerange(0, max_matrix_element + 1)) plist = random.sample(randomlist, square_matrix_dimension) randomlist = random.sample( range(0, max_matrix_element + 1), square_matrix_dimension * square_matrix_dimension) randomlist = list(set(randomlist) - set(plist)) n_list = random.sample( randomlist, square_matrix_dimension * (square_matrix_dimension - 1)) Mat = list() for i in range(0, square_matrix_dimension): z = list() z.append(plist[i]) for j in range(0, square_matrix_dimension - 1): z.append(n_list[(i * square_matrix_dimension) + j - i]) random.shuffle(z) Mat.append(z) Mat = sympy.Matrix(Mat) problem = 'Inverse of Matrix $' + bmatrix(Mat.tolist()) + '$ is:' solution = f'${bmatrix(sympy.Matrix.inv(Mat).tolist())}$' return problem, solution ``` -------------------------------- ### exponentiation(max_base=20, max_expo=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate an exponentiation problem and its solution. ```APIDOC ## exponentiation(max_base=20, max_expo=10) ### Description Generates an exponentiation problem with a random base and exponent, and returns the result. ### Parameters - **max_base** (int) - Optional - Maximum value for the base. Defaults to 20. - **max_expo** (int) - Optional - Maximum value for the exponent. Defaults to 10. ### Returns - **problem** (str) - A string representing the exponentiation problem in LaTeX format, e.g., `$9^{5}=$`. - **solution** (str) - A string representing the result in LaTeX format, e.g., `$59049$`. ``` -------------------------------- ### exponentiation(max_base=20, max_expo=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate an exponentiation problem and its solution. ```APIDOC ## exponentiation(max_base=20, max_expo=10) ### Description Generates an exponentiation problem with a random base and exponent. ### Parameters - **max_base** (int) - Optional - Maximum value for the base (default: 20). - **max_expo** (int) - Optional - Maximum value for the exponent (default: 10). ### Returns - **problem** (str) - The problem string in LaTeX format, e.g., `$9^{5}=$`. - **solution** (str) - The solution string in LaTeX format, e.g., `$59049$`. ``` -------------------------------- ### Generate quadratic equation problem Source: https://lukew3.github.io/mathgenerator/mathgenerator/algebra.html Generates a quadratic equation problem with random coefficients and returns the zeros as the solution. The discriminant is guaranteed to be positive by choosing b appropriately. ```python def quadratic_equation(max_val=100): r"""Quadratic Equation | Ex. Problem | Ex. Solution | | --- | --- | | What are the zeros of the quadratic equation $22x^2+137x+25=0$ | ${-0.19, -6.04}$ | """ a = random.randint(1, max_val) c = random.randint(1, max_val) b = random.randint( round(math.sqrt(4 * a * c)) + 1, round(math.sqrt(4 * max_val * max_val))) D = math.sqrt(b * b - 4 * a * c) res = {round((-b + D) / (2 * a), 2), round((-b - D) / (2 * a), 2)} problem = f"What are the zeros of the quadratic equation ${a}x^2+{b}x+{c}=0$" solution = f'${res}$' return problem, solution ``` -------------------------------- ### compare_fractions(max_val=10) Source: https://lukew3.github.io/mathgenerator/mathgenerator/basic_math.html Function to generate a fraction comparison problem. ```APIDOC ## compare_fractions(max_val=10) ### Description Generates a problem and solution for comparing two fractions, e.g., comparing $\frac{1}{2}$ and $\frac{3}{4}$ yields solution $>$. ### Parameters - **max_val** (int) - Optional - Maximum value for numerators and denominators (default: 10) ### Returns - **problem** (str) - The problem statement in LaTeX format - **solution** (str) - The solution symbol (>, <, or =) ```