Holden,
While searching for some examples of how one might go about writing a program in C++ to show the process of completing the square to put a quadratic equation in the form of a trinomial square, (x + p)^ 2 = q, not to mention the complications should I desire to print the solutions, if they even exist, in exact form (using radicals), I stumbled upon this video and wondered if you understood the language the man is speaking. i am aware that there are around 40 different dialects in the large area called India.
The cool thing is that I find part of me understands what he is saying. Well, not literally or verbatim, but I can follow his reasoning by looking at his code.
I still have this obsession with finding a way to force the computer to give results in exact form as opposed to decimal. I see how SymPy does this at least in the IPython terminal, and I envy and appreciate the group of individuals who put that collection of modules together.
The reason why I enjoy building some things from scratch is because I like to come face to face with the complications of how one would go about programming a computer to do such things.
It really makes one appreciate what we can do with pencil and paper, drawing those symbols like the radical sign.
I would like to be able to write a program that gives output like SymPy:
In [1]: solve(x**2 + 5*x - 5,x)
Out[1]:
⎡ 5 3⋅√5 3⋅√5 5 ⎤
⎢- ─ + ────, - ──── - ─ ⎥
⎣ 2 2 2 2 ⎦
but also I would like to display each step.
I am thinking along the lines of combining with Rational class put together for dealing with fractions, and then incorporating some of the following techniques for keeping the radicals in exact form.
The following is some code I "put together" which I run at the command line, such as sqrt 180.
I can always incorporate the the parts minus the "show and tel"l parts into another program. The code below is not the same as the attached file.
I leave it here as an example of the kind of goofy things I become engrossed in when tinkering around with mathematics and programming. Incorporating some programming can breathe life into elementary algebra. It's a process of rediscovery. The two disciplines really do complement each other.
_______________________________________________________________________________________
#include<iostream>
#include <cstdlib>
#include<cmath>
#define EPSILON 0.0000001 // least minimum value for comparison
double SquareRoot(double _val);
void SquareRoot(int _integer);
int main(int argc, char* argv[])
{
if (argc !=2) {
std::cout << "USE: " << argv[0] << " <positive integer>\n";
std::cout << "to simplify square root of integer.\n";
return 1;
}
int N = atoi(argv[1]);
if ( (N <= 0) || (N > 9999) ) {
std::cout << "Input positive integer, goddamnit!\n";
std::cout << "And make it less than 10000, please.\n";
return 2;
}
int factor = 316; // 31^2 = 961; 316^2 = 99856
while ( N%(factor*factor) != 0 ) {
factor -= 1;
}
int r = N/(factor*factor);
std::cout << "\nSimplified square root of " << N << " is ";
if (factor > 1) std::cout << factor << "*";
std::cout << "sqrt(" << r << ")\n";
std::cout << std::endl;
std::cout << "Trial Square Root of " << N << ":\n";
SquareRoot(N);
std::cout << std::endl << std::endl;
double n = atof(argv[1]);
double sqroot = SquareRoot(n);
std::cout << "Using SquareRoot(" << n << ") = " << sqroot;
double s = sqrt(n);
std::cout << "\nUsing sqrt(" << n << ") = " << s << std::endl;
return 0;
}
double SquareRoot(double _val) {
double low = 0;
double high = _val;
double mid = 0;
while (high - low > EPSILON) {
mid = low + (high - low) / 2; // finding mid value
if (mid*mid > _val) {
high = mid;
} else {
low = mid;
}
}
return mid;
}
void SquareRoot(int _integer) {
int sqr = 1;
while (sqr*sqr < _integer) {
sqr += 1;
}
int b = sqr;
int a = sqr - 1;
std::cout << a*a << " < " << _integer << " < " << b*b;
std::cout << "\nSo we know that sqrt("<< a*a << ") < sqrt(" << _integer;
std::cout << ") << sqrt(" << b*b << ")\n";
std::cout << "That is, " << a << " < sqrt(" << _integer;
std::cout << ") < " << b << std::endl;
int i;
double c, d;
double digit = 1;
double nths = double(a) + 0.1*digit;
for (int i=0; i < 4; i++) {
std::cout << "\nSimilarly, \n";
while (nths*nths < _integer) {
nths += 0.1*digit;
}
d = nths; // store value greater than square root
c = nths - 0.1*digit; // store value less than square root
std::cout << "\nSince (" << c << ")^2 = " << c*c;
std::cout << " and since (" << d << ")^2 = " << d*d << ",\n";
std::cout << "we know that " << c << " < sqrt(" << _integer;
std::cout << ") < " << d << std::endl;
digit = 0.1*digit;
nths = c + 0.1*digit;
}
}
_________________________________________________
To whom it may concern: The attached file was uploaded as txt, since those are the rules for freeboards.
Just rename as cts.cpp and compile (with g++ or whatever):
g++ -g cts.cpp -std=c++11 -o cts
--- needs
-std=c++11 for std::to_String()
./cts
It is an attempt to show results in exact "human form" along with decimal results.
[The file has been attached to
a post below (in this thread)]