How To Simplify Square Roots With Code

This simple C/C++ program does it for you!

We've all been there.

Sitting in a highschool math class learning about radicals and the boring process it takes to simplify them, when all of a sudden, it hits you:

Why not automate this?

There has to be a way, right?

Yes! There certainly is a way to simplify radicals through code. There are many different ways to go through the process but I will be going over a very intutive one.

Let's start off with the basics. When simplifying a square root, you're trying to find the largest perfect square that you can just remove from the rest of the radicand.

In this formula, we have S , the Radicand , or the original number that we are getting the root of.

You'll find that in the right side of the equation, all we've done is seperated K from S.

K can be literally any number, but it makes more sense if it's a perfect square, because then you can remove the root symbol. Here's an example:

the square root of 4 is 2, so we can remove the √ sign. However, since math teachers are afraid of decimals, we have to leave the square root of 6 the way it is.

So now that we have the perfect formula for simplifying a square root, we just need an easy way to get K.

No need to fear, as I have built a program that works in C that will find K for us!

Since it works in C, it also works in C++. Additionally, that makes it compatable with the ce toolchain so you can build a root-simplifier right on your calculator!


/*
---------------------FUNCTION DATA---------------------
Author: Aengus Patterson
Date: 10-20-2022
license: MIT license
Required Libs: <stdlib.h>
Input: square, where square is S in the formula below
Output: K, where K is in the formula below
Formula:  sqrt(s) = sqrt(K) * sqrt(square/K)
Example: sqrt(24) =       2 * sqrt(6)
*/

//to find K, we go through every number up to square to see if it is
// 1) divisible by square
// 2) a perfect square

int simpSqrt(int square)
{
    //the value of K may not change so we set it to 1
    int K = 1;
    for (int i = 2; i <= square; i++)
    {
        //if i is divisible by square, then we can move on
        if (square % i == 0)
        {
            //the algorithm below is used so you don't have to include <math.h>
            //its a square root algorithm that only gives the output (endNum) correctly when
            //the input (i) is a perfect square, which is good because that's what we want.
            int counter = 1, squareRoot=1, endNum;
            while (squareRoot <= i)
            {
                counter++;
                squareRoot = counter*counter;
            }
            endNum= counter - 1;
            //if the square of endNum == i, then it's a perfect square.
            if (endNum * endNum == i)
            {
                //set K equal to i. This may be set multiple (or no) times throughout this loop.
                K = i;
            }
        }
    }
    return K;
}
    
            

There you have it! Just call that function from somewhere within your code and you'll have K. From there, you can build a program that can simplify all those square roots for you.