AR14020/Math_Solver
0
1import streamlit as st2from sympy import symbols, Eq, solve3 4# Arithmetic Operations with Step-by-Step Solutions5 6def solve_addition(numbers):7 result = sum(numbers)8 steps = f"Step 1: Add all the numbers: {' + '.join(map(str, numbers))} = {result}"9 return result, steps10 11def solve_subtraction(numbers):12 result = numbers[0]13 steps = f"Step 1: Start with the first number: {numbers[0]}"14 for i, num in enumerate(numbers[1:], start=2):15 result -= num16 steps += f"\nStep {i}: Subtract {num} from the result: {result}"17 return result, steps18 19def solve_multiplication(numbers):20 result = 121 steps = f"Step 1: Start with 1 and multiply it by: {' * '.join(map(str, numbers))}"22 for num in numbers:23 result *= num24 steps += f" = {result}"25 return result, steps26 27def solve_division(numbers):28 result = numbers[0]29 steps = f"Step 1: Start with the first number: {numbers[0]}"30 for i, num in enumerate(numbers[1:], start=2):31 if num == 0:32 return "Cannot divide by zero", "Division by zero error"33 result /= num34 steps += f"\nStep {i}: Divide by {num}: {result}"35 return result, steps36 37def solve_percentage(x, y):38 result = (x * y) / 10039 steps = f"Step 1: Multiply the number by the percentage: {x} * {y} = {x * y}\nStep 2: Divide the result by 100: {x * y} / 100 = {result}"40 return result, steps41 42def solve_fraction(fraction_str):43 try:44 numerator, denominator = map(int, fraction_str.split('/'))45 result = numerator / denominator46 steps = f"Step 1: Divide the numerator by the denominator: {numerator} / {denominator} = {result}"47 return result, steps48 except ValueError:49 return "Invalid fraction format. Please use 'numerator/denominator'.", "Invalid input"50 51def solve_estimation(x, error):52 result = x + (x * error / 100)53 steps = f"Step 1: Add the error percentage to the number: {x} + ({x} * {error} / 100) = {result}"54 return result, steps55 56def solve_percent_equivalence(x, y):57 if y != 0:58 result = (x / y) * 10059 steps = f"Step 1: Divide the first number by the second number: {x} / {y} = {x / y}\nStep 2: Multiply the result by 100: {x / y} * 100 = {result}"60 return result, steps61 else:62 return "Cannot divide by zero", "Division by zero error"63 64 65# Algebra Problem Solvers66 67def solve_linear_equation(coefficients):68 # Create symbols dynamically based on the keys (variable names)69 variables = symbols(*list(coefficients.keys())) # Unpack keys properly as separate arguments70 71 # Create the equation (assuming the sum of coefficients * variables equals 0)72 equation = Eq(sum(coef * var for coef, var in zip(coefficients.values(), variables)), 0)73 solution = solve(equation, variables)74 75 # Generate step-by-step explanation76 steps = "Step 1: Write the equation in standard form: "77 equation_str = " + ".join([f"{coef}*{var}" for coef, var in zip(coefficients.values(), variables)])78 steps += f"{equation_str} = 0\n"79 steps += "Step 2: Isolate the variable(s) by rearranging the equation."80 steps += f"\nStep 3: Solve the equation: {equation_str} = 0"81 82 return solution, steps83 84def solve_quadratic_equation(a, b, c):85 x = symbols('x')86 equation = Eq(a*x**2 + b*x + c, 0)87 solution = solve(equation, x)88 89 steps = f"Step 1: Write the quadratic equation: {a}x² + {b}x + {c} = 0"90 steps += "\nStep 2: Apply the quadratic formula: x = [-b ± sqrt(b² - 4ac)] / 2a"91 steps += f"\nStep 3: Substitute the values: x = [-{b} ± sqrt({b}² - 4*{a}*{c})] / 2*{a}"92 return solution, steps93 94def solve_cubic_equation(a, b, c, d):95 x = symbols('x')96 equation = Eq(a*x**3 + b*x**2 + c*x + d, 0)97 solution = solve(equation, x)98 99 steps = f"Step 1: Write the cubic equation: {a}x³ + {b}x² + {c}x + {d} = 0"100 steps += "\nStep 2: Factor or use numerical methods to find the roots."101 return solution, steps102 103def solve_polynomial(coefficients):104 x = symbols('x')105 equation = sum(coef * x**i for i, coef in enumerate(coefficients))106 solutions = solve(equation, x)107 108 steps = f"Step 1: Write the polynomial equation in standard form:\n"109 poly_str = " + ".join([f"{coef}x^{i}" for i, coef in enumerate(coefficients)])110 steps += f"Equation: {poly_str} = 0\n"111 steps += "Step 2: Use numerical or symbolic methods to find the roots of the equation."112 113 return solutions, steps114 115def solve_system_of_equations(eqns):116 solution = solve(eqns)117 steps = "Step 1: Write the system of equations in standard form."118 steps += "\nStep 2: Use substitution or elimination to solve the system."119 return solution, steps120 121 122# Streamlit UI123st.title("Interactive Problem Solver")124 125problem_type = st.selectbox("Select the type of problem", ["Arithmetic Operations", "Algebra Problems"])126 127if problem_type == "Arithmetic Operations":128 operation = st.selectbox("Select the arithmetic operation", [129 "Addition",130 "Subtraction",131 "Multiplication",132 "Division",133 "Percentage",134 "Fraction",135 "Estimation",136 "Percent Equivalence"137 ])138 139 if operation in ["Addition", "Subtraction", "Multiplication", "Division"]:140 num_variables = st.number_input("Enter the number of variables", min_value=2, value=2)141 numbers = [st.number_input(f"Enter variable {i+1}:", value=0) for i in range(num_variables)]142 143 elif operation == "Percentage":144 x = st.number_input("Enter the number:", value=0)145 y = st.number_input("Enter the percentage:", value=0)146 147 elif operation == "Fraction":148 fraction_str = st.text_input("Enter the fraction (numerator/denominator):", "1/2")149 150 elif operation == "Estimation":151 x = st.number_input("Enter the number:", value=0)152 error = st.number_input("Enter the estimation error percentage:", value=0)153 154 elif operation == "Percent Equivalence":155 x = st.number_input("Enter the first number:", value=0)156 y = st.number_input("Enter the second number:", value=0)157 158 if st.button("Solve"):159 if operation == "Addition":160 result, steps = solve_addition(numbers)161 elif operation == "Subtraction":162 result, steps = solve_subtraction(numbers)163 elif operation == "Multiplication":164 result, steps = solve_multiplication(numbers)165 elif operation == "Division":166 result, steps = solve_division(numbers)167 elif operation == "Percentage":168 result, steps = solve_percentage(x, y)169 elif operation == "Fraction":170 result, steps = solve_fraction(fraction_str)171 elif operation == "Estimation":172 result, steps = solve_estimation(x, error)173 elif operation == "Percent Equivalence":174 result, steps = solve_percent_equivalence(x, y)175 176 st.write(f"Result: {result}")177 st.write(f"Solution Steps: \n{steps}")178 179elif problem_type == "Algebra Problems":180 algebra_problem = st.selectbox("Select the algebra problem", [181 "Linear Equation",182 "Quadratic Equation",183 "Cubic Equation",184 "Higher-Order Polynomial",185 "System of Equations"186 ])187 188 if algebra_problem == "Higher-Order Polynomial":189 degree = st.number_input("Enter the degree of the polynomial (e.g. 4 for quartic):", min_value=1, value=4)190 coefficients = []191 for i in range(degree, -1, -1):192 coef = st.number_input(f"Enter coefficient for x^{i}:", value=1)193 coefficients.append(coef)194 195 if st.button("Solve"):196 solution, steps = solve_polynomial(coefficients)197 st.write(f"Solutions: {solution}")198 st.write(f"Solution Steps: \n{steps}")199 200 if algebra_problem == "Linear Equation":201 num_vars = st.number_input("Enter the number of variables:", min_value=1, value=2)202 coefficients = {}203 for i in range(num_vars):204 var = st.text_input(f"Enter variable {i+1} (e.g. 'x'):", value=f"x{i+1}")205 coef = st.number_input(f"Enter coefficient for {var}:", value=1)206 coefficients[var] = coef207 208 if st.button("Solve"):209 solution, steps = solve_linear_equation(coefficients)210 st.write(f"Solution: {solution}")211 st.write(f"Solution Steps: \n{steps}")212 213 elif algebra_problem == "Quadratic Equation":214 a = st.number_input("Enter coefficient a:", value=1)215 b = st.number_input("Enter coefficient b:", value=1)216 c = st.number_input("Enter coefficient c:", value=1)217 218 if st.button("Solve"):219 solution, steps = solve_quadratic_equation(a, b, c)220 st.write(f"Solution: {solution}")221 st.write(f"Solution Steps: \n{steps}")222 223 elif algebra_problem == "Cubic Equation":224 a = st.number_input("Enter coefficient a:", value=1)225 b = st.number_input("Enter coefficient b:", value=1)226 c = st.number_input("Enter coefficient c:", value=1)227 d = st.number_input("Enter coefficient d:", value=1)228 229 if st.button("Solve"):230 solution, steps = solve_cubic_equation(a, b, c, d)231 st.write(f"Solution: {solution}")232 st.write(f"Solution Steps: \n{steps}")233 234 elif algebra_problem == "System of Equations":235 eqns = []236 num_eqns = st.number_input("Enter the number of equations:", min_value=1, value=2)237 for i in range(num_eqns):238 eqn = st.text_input(f"Enter equation {i+1} (e.g., 2*x + 3*y = 5):", value="x + y = 10")239 eqns.append(Eq(eval(eqn.split("=")[0].strip()), eval(eqn.split("=")[1].strip())))240 241 if st.button("Solve System"):242 solution, steps = solve_system_of_equations(eqns)243 st.write(f"Solution: {solution}")244 st.write(f"Solution Steps: \n{steps}")245 246 