Useful Stata Programs for Answering Kids' Tough Math Questions

My son convinced me that I have forgotten the quadratic formula.  I guess such knowledge isn't needed to be a Professor?   For parents who are facing tough questions from their kids, here is a Stata program for approximating the solution to any non-linear function.  Below,  I also provide a prime number solver.


/* Non-Linear Equation Solver that approximates the solution to
 X^4  +  3*X^3 - 2*X^2  =   543007 by searching in increments of .001  */

input z
1
end
expand = 1000000
* the program searches for the answer in increments of .001
gen x=_n/1000
gen alex = x^4 + 3*x^3 -2*x^2 - 543007
gen m=_n
sort m
* note that that for small x values that alex < 0, we search for that value of x such that alex was < 0 but then alex > 0
keep if alex[_n-1]<0 alex="" amp="" n="">0
list x alex


Here is a program for figuring out whether any positive integer is prime or not;

input z
1
end
expand = 4935679
gen x=_n
gen prime= 4935679/x
gen m=int(prime)
keep if prime-m==0
count
list
* If more than 2 rows appear then the number (in this case 4935679)  isn't prime