Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.1 kB
4
Indexable
Never
import random

def generate_addition_problem():
    a = random.randint(1, 100)
    b = random.randint(1, 100)
    problem = f"\\item (400) $\\Huge{{{a} + {b}}}$"
    answer = f"% {a + b}"
    return problem, answer

def generate_subtraction_problem():
    a = random.randint(1, 100)
    b = random.randint(1, 100)
    if b > a:
        a, b = b, a
    problem = f"\\item (400) $\\Huge{{{a} - {b}}}$"
    answer = f"% {a - b}"
    return problem, answer

def generate_multiplication_problem():
    a = random.randint(1, 12)
    b = random.randint(1, 12)
    problem = f"\\item (400) $\\Huge{{{a} \\times {b}}}$"
    answer = f"% {a * b}"
    return problem, answer

def generate_division_problem():
    a = random.randint(1, 12)
    b = random.randint(1, 12)
    product = a * b
    problem = f"\\item (400) $\\Huge{{{product} \\div {a}}}$"
    answer = f"% {b}"
    return problem, answer

def generate_arithmetic_problems(n):
    problems = []
    for _ in range(n):
        problem_type = random.choice(['addition', 'subtraction', 'multiplication', 'division'])
        
        if problem_type == 'addition':
            problem, answer = generate_addition_problem()
        elif problem_type == 'subtraction':
            problem, answer = generate_subtraction_problem()
        elif problem_type == 'multiplication':
            problem, answer = generate_multiplication_problem()
        elif problem_type == 'division':
            problem, answer = generate_division_problem()
        
        problems.append((problem, answer))
    
    return problems

def save_problems_to_file(problems, filename):
    with open(filename, 'w') as file:
        file.write("\\begin{enumerate}\n")
        for problem, answer in problems:
            file.write(f"{problem}\n{answer}\n")
        file.write("\\end{enumerate}\n")

# Generate 20 problems
problems = generate_arithmetic_problems(20)

# Save to a .tex file
save_problems_to_file(problems, "arithmetic_problems_with_answers.tex")

print("LaTeX file generated: arithmetic_problems_with_answers.tex")
Leave a Comment