How to create a random math problem in LaTeX?
I know I'm late to the party here, but I would use Sage and SageTeX:
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\section{Problem}
\begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
\end{sagesilent}
Let \(x_1\) and \(x_2\) be the roots of \(\sage{poly}=0\).
Find \(x_1x_2\).
\section{Solution}
\[
x_1x_2=\sage{c/a}.
\]
\sageplot{plot(poly) }
\end{document}
The output looks like this:
This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
\documentclass{article}
\usepackage{pgf}
\begin{document}
\section{Problem}
\pgfmathsetseed{1}
\pgfmathtruncatemacro\coeffa{random(2,10)}
\pgfmathtruncatemacro\coeffb{random(2,10)}
\pgfmathtruncatemacro\coeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
\immediate\write18{solve.cmd "tex(solve(\coeffa*x^2 + (\coeffb*x) + (\coeffc)));" > solution1.tex}
Let \(x_1\) and \(x_2\) be the roots of \(\coeffa x^2 +\coeffb x +\coeffc=0\). Find \(x_1x_2\).
\section{Solution}
% read the solution
\input solution1
\end{document}
I tried to use Maxima's plot2d
function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png
plot with gnuplot
and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape
or --enable-write18
flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
\documentclass{article}
\immediate\write18{solve.cmd}
\newread\myread
\openin\myread=problem.txt
\read\myread to \theequation
\read\myread to \thesolution
\begin{document}
\section{Problem}
This is our equation:
\theequation
\section{Solution}
And this is our solution:
\thesolution
\end{document}
Let's check the code: first of all, we run solve.cmd
, which outputs both equation and solution to a file named problem.txt
. Then we create a \newread\myread
and open this file \openin\myread=problem.txt
. Now we map the first line to \theequation
(which is the equation which we defined in our Maxima code) and the second line to \thesolution
(same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed
in it and replace the pattern. =)
The lcg package contains macros to generate (pseudo) random numbers. A little example (the plot is also generated but using TikZ
):
\documentclass{article}
\usepackage[counter=coefa,first=-8, last=12]{lcg}
\usepackage{pgfplots}
\begin{document}
\rand\arabic{coefa}
\let\Coefa\thecoefa
\chgrand[counter=coefb]
\rand\arabic{coefb}
\let\Coefb\thecoefb
\chgrand[counter=coefc]
\rand\arabic{coefc}
\let\Coefc\thecoefc
\section{Problem}
Let \(x_1\) and \(x_2\) be the roots of \( \Coefa x^2 +\Coefb x + \Coefc =0\). Find \(x_1x_2\).
\section{Solution}
\[
x_1x_2=\frac{\Coefc}{\Coefa}
\]
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel={$f(x) = \Coefa x^2 + \Coefb x + \Coefc $}
]
\addplot[color=orange,thick] {\Coefa*x^2 + \Coefb* x + \Coefc};
\end{axis}
\end{tikzpicture}
\end{document}
Of course, when compiling my example you'll get different values for the corfficients, so a different equation and plot will be obtained.