def polynomial_function_find_negative(): max_coeff_abs = 144 min_degree = 2 max_degree = 5 degree = randint(min_degree, max_degree) coeffs = around(random.rand(6) * (max_coeff_abs * 2)) - max_coeff_abs x = symbols('x') # f(x) expr = 0 # f(-x) neg_expr = 0 # arange is half open; it doesn't include the top value, so we add 1 for i in arange(degree - 1) + 1: coeff = Rational(coeffs[int(i)]) power = Rational(degree - i) expr += coeff * (x)**power neg_expr += coeff * (-x)**power question = fmath("f(x) = " + latex(expr)) answer = fmath("f(-x) = " + latex(neg_expr)) return create_full_text_problem([question], [answer])
def addition_integers(length=2, low=1, high=10) -> Problem: length = int(length) low = int(low) high = int(high) if length < 2: raise ValueError("addition problem must have length of 2") if low > high: raise ValueError("max must be greater than or equal to min") equation = "" sum = 0 for i in range(length): value = random.randrange(low, high) equation += str(value) sum += value if i < length - 1: equation += "+" problem_value = fmath(equation) solution_value = "\\({}\\)".format(sum) problem_widget = Widget(build_text_widget_options(problem_value)) solution_widget = Widget(build_text_widget_options(solution_value)) return Problem([problem_widget], [solution_widget])
def quadratic_function_find_vertex_intercept_form(): # We may want to include fractions for more difficult problems in the future min_c = 1 max_c = 6 min_n = 1 max_n = 30 min_a = 1 max_a = 5 c = randint(min_c, max_c) n = randint(min_n, max_n) a = randint(min_a, max_a) c = -c if rand() < 0.5 else c x = symbols('x') expr = a * (x + c)**2 # Expand into standard form expr = expand(expr) # Remove c^2 expr = expr - a * c**2 # Add n expr = expr + n eval_expr = a * (x + c)**2 + n - (a * (c**2)) problem_instruction = "Find vertex " problem_expr = fmath(latex(expr) + ' = 0') problem_solution = fmath(latex(eval_expr) + ' = 0') return create_full_text_problem([problem_expr], [problem_solution])
def compound_interest_continuous_find_effective_roi(): r = randint(1, 15) / 100 r_e = f_effective_rate_of_interest_continuous(r) problem_instruction = "Find the effective return on investment for this investment (round to 3 decimal points):" problem_text = f"For {fmath(fper(r))} compounded continuously" solution_text = fmath(fper(r_e)) return create_full_text_problem( [problem_instruction, problem_text], [solution_text] )
def compound_interest_discrete_find_effective_roi(): n = choice(compounding_n_choices) r = randint(1, 15) / 100 r_e = f_effective_rate_of_interest_discrete(r, n) problem_instruction = "Find the effective return on investment for this investment (round to 3 decimal points):" problem_text = f"For {fmath(fper(r))} compounded {compounding_choices[n]}" solution_text = fmath(fper(r_e)) return create_full_text_problem( [problem_instruction, problem_text], [solution_text] )
def compound_interest_continuous_find_p(): a = 50 * randint(1, 21) t = randint(1, 3) + choice([0, 0.5]) r = randint(1, 15) / 100 p = round(f_present_value_continuous(a, r, t), 2) plural_end = "s" if t != 1 else "" problem_instruction = "Find the present value of this investment:" problem_text = f"To get {fmath(fcur(a))} after {fmath(ffrac(t))} year{plural_end} at {fmath(fper(r))} compounded continuously" solution_text = fmath(fcur(p)) return create_full_text_problem( [problem_instruction, problem_text], [solution_text] )
def compound_interest_discrete_find_p(): a = 50 * randint(1, 21) n = choice(compounding_n_choices) t = randint(1, 3) + choice([0, 0.5]) r = randint(1, 15) / 100 p = round(f_present_value_discrete(a, r, n, t), 2) year_word = fplural("year", "years", t) problem_instruction = "Find the present value of this investment:" problem_text = f"To get {fmath(fcur(a))} after {fmath(ffrac(t))} {year_word} at {fmath(fper(r))} compounded {compounding_choices[n]}" solution_text = fmath(fcur(p)) return create_full_text_problem( [problem_instruction, problem_text], [solution_text] )
def compound_interest_discrete_find_a(): p = 50 * randint(1, 21) n = choice(compounding_n_choices) t = randint(1, 3) + choice([0, 0.5]) r = randint(1, 15) / 100 a = f_compound_interest_discrete(p, r, n, t) year_word = fplural("year", "years", t) problem_instruction = "Find the amount that results from this following investment:" problem_text = f"{fmath(fcur(p))} invested at {fmath(fcur(r))} compounded {compounding_choices[n]} after a period of {fmath(ffrac(t))} {year_word}" solution_text = fmath(fcur(a)) return create_full_text_problem( [problem_instruction, problem_text], [solution_text] )
def sum_consecutive_integers(integer_count: bool = None, max_initial_integer: int = 50) -> Problem: if integer_count is not None and integer_count < 2: raise ValueError('parameter integer_count on sum_consecutive_integers must be greater than or equal to 2') integer_count = integer_count if integer_count is not None else Random().randint(3, 5) base_integer = Random().randrange(0, max_initial_integer) final_integer = 0 for i in range(integer_count): final_integer += base_integer + i nth_integer_to_find = Random().randint(1, integer_count) ordinal_number = ford(nth_integer_to_find) question_label = f"The number {fmath(final_integer)} is the sum of {fmath(integer_count)} consecutive integers. What is the {ordinal_number}?" solution_integer = base_integer + (nth_integer_to_find - 1) solution = fmath(solution_integer) return create_full_text_problem([question_label], [solution])
def formatted_interval_from_continuous(values): interval = interval_from_continuous(values) interval_string = '[{}, {}]'.format(interval[0], interval[1]) interval_formatted = fmath(interval_string) return interval_formatted
def find_trig_func_arc_inverse_with_calculator(): problem_instruction = "Use a calculator to find the value of this expression rounded to two decimal places:" functions = { "arccsc": ("csc", lambda x: arcsin(1 / x), True), "arcsec": ("sec", lambda x: arccos(1 / x), True), "arccot": ("cot", lambda x: arctan(1 / x), False), } function_keys = list(functions.keys()) picked_function_key = function_keys[randint(0, len(function_keys))] picked_function = functions[picked_function_key] picked_function_has_gap = picked_function[2] number_types = ["fraction", "sqrt", "whole"] picked_number_type = choice(number_types) picked_number = None fraction_range = (-4, 4) sqrt_range = (1, 10) whole_range = (-10, 10) def pick_number(): if picked_number_type == "fraction": # denominator shouldn't be 1 denominator = choice([ randint(fraction_range[0], -1), randint(2, fraction_range[1] + 1) ]) denominator_sign = 1 if denominator >= 0 else -1 if picked_function_has_gap: numerator = denominator_sign * (abs(denominator) + 1) else: numerator = denominator_sign * (abs(denominator) + choice([-1, 1])) fraction = Fraction(numerator, denominator).limit_denominator() # simplify numerator = fraction.numerator denominator = fraction.denominator return (numerator / denominator, ffrac(numerator, denominator)) elif picked_number_type == "sqrt": square = randint(sqrt_range[0], sqrt_range[1]) return (square**0.5, f"\sqrt{{{square}}}") elif picked_number_type == "whole": whole_number = choice( [randint(whole_range[0], 0), randint(1, whole_range[1] + 1)]) return (whole_number, str(whole_number)) picked_number = pick_number() answer = around(picked_function[1](picked_number[0]), 3) problem_text = fmath(f"\{picked_function[0]}^{{-1}}({picked_number[1]})") solution_text = fmath(str(answer)) problem = build_gen_problem_content([problem_instruction, problem_text], [solution_text]) # if env.is_debug(): # debug_info = [ # {"picked_number_type": picked_number_type}, # {"picked_function": picked_function_key}, # {"picked number": picked_number}, # ] # return DebugProblemWrapper(debug_info, problem) return problem