# Simplifing Square Roots

• 10-01-2005
LiNeAr
Simplifing Square Roots
Hello, I wanted to make a program that could simplify square roots. So before i started anyting i wanted to post on here. I was thinking about using the headerfile <cmath>, but i am not to familiar with it. What i am trying to say is, how would i go about indicating a square root sign. Or is there some kind of command in <cmath> that would represent a square root sign.

Ex. sq.24= 2*sq.6
• 10-01-2005
Sang-drax
You need to factorize the number, then remove any factors occurring twice and put them outside.
• 10-01-2005
rockytriton
you can do sqr(9) = 3, or pow(9, 0.5) = 3. I assume that's what your asking for.
• 10-01-2005
mhenrique
I think he didnt mean that, I think he wanted to do like in math classes...

Ex: sqrt(20) = 2*sqrt(5)

Substitute sqrt with the root symbol
• 10-01-2005
LiNeAr
Quote:

Originally Posted by mhenrique
I think he didnt mean that, I think he wanted to do like in math classes...

Ex: sqrt(20) = 2*sqrt(5)

Substitute sqrt with the root symbol

Yeah that's exactly what I wanted to do. But i dont have a clue on how to do that. Mainly in part, because i dont know what to use for the square root sign. So if anyone knows, that would be a big help. Unless what you showed me in the example was what i should use. Is it? :confused:
• 10-01-2005
Rashakil Fol
You can't output the square root sign. Also, the C function for computing the square root is called 'sqrt', but I don't think you'll be using it.
• 10-01-2005
rockytriton
oh I see, no, there's nothing to do that for you. It sounds like it would be a good idea for a math library for you to write.
• 10-01-2005
LiNeAr
Quote:

Originally Posted by rockytriton
oh I see, no, there's nothing to do that for you. It sounds like it would be a good idea for a math library for you to write.

That sucks. I was hoping that there was something for what I wanted to do. I wouldn't have a clue on how to make a library. So I guess it was a waste of time. :( :p
• 10-01-2005
Dae
Everyone is saying the right thing in this topic. You need to factorize the original sqaure root.

Quote:

Originally Posted by LiNeAr
Yeah that's exactly what I wanted to do. But i dont have a clue on how to do that. Mainly in part, because i dont know what to use for the square root sign. So if anyone knows, that would be a big help. Unless what you showed me in the example was what i should use. Is it? :confused:

As rockytriton said,

The sqaure root of 25 is the same as 25 to the power of 1/2. The third root of 81 is the same as 81 to the power of 1/3.

pow(25, 0.5) == sqrt(25)

Quote:

I think he didnt mean that, I think he wanted to do like in math classes...

Ex: sqrt(20) = 2*sqrt(5)

Substitute sqrt with the root symbol
Thats factoring, like Sang-drax said.

You need to convert the math for factoring sqaure roots into a program. You would have to take the original number, divide it by two, then start there and loop down to check if the number is a prime factor, aka. remainder is equal to 0.

Heres some psuedo for your modo:

make an array of 10 ints
assign the original value (taken from cin) to the first element of the array
make a for loop that has an i int which you decrement every loop
check to see if the first element of the array can be further factored
- basicly take that number, divide it by two then minus the decrement (originally 0), and loop to check if original number modulus (%) the new number is equal to an even number
- if it isnt, decrement and try again, all the way down to 1
- if you do find one, you check for the next array element not being used, and assign it to that one, and continue checking for more
- you should then check if the number youve gotten if sqaure rooted
- once at 1, you go to the next element, and check for more.
eventually you'll have like original element 24, turns into 12, 2; turns into 6, 2, 2; which turns into 3, 2, 2, 2.

Edit: I stopped and decided to make the program myself, because I wouldnt mind having it also.. you know, the ultimate math functions program would be fun to make (even though graphic calculators exist).

Mind turned out a bit different from pseudo code, it tests if theres any decimals, not if its even (because thats a flaw if 25^(1/2)). It could probably be more optimal, or self explanitory.. but it works, so I'll optimize it (if necessary) some other time.

See if you get any ideas from it:
Code:

```#include <iostream> #include <cmath> int main() {   int origValue;   std::cin >> origValue;     int primeFactor[10] = {0};   int primeIter = 0;   int multValue[5] = {0};   int multIter = 0;   bool quit; //incase we want to exit the loop   //if the entered number has a root, automatically find it and ignore factoring   int tempi = static_cast<int>(sqrt(origValue));   //   if(sqrt(origValue) - tempi == 0) {     multValue[multIter] = tempi;   } else   for(int i = origValue - 1; i > 1 && quit != 1; --i) {     int tempValue = origValue / i;     if((static_cast<double>(origValue) / static_cast<double>(i)) - static_cast<double>(tempValue) == 0) {       int tempFactor = static_cast<int>(sqrt(i)); //the whole number value       //       while(1) {         if(primeFactor[primeIter] == 0) {           //dont add factor to list, continue iterating on it std::cout << "fir: " << i << " ";           //check if the decimal value minus the whole number value is 0           if(sqrt(i) - static_cast<double>(tempFactor) == 0) {             multValue[multIter] = tempFactor; //assign the value as a multiple             quit = 1; //lets back it out of here             ++multIter;             break;           }           ++primeIter;           break;         } else           ++primeIter;       }//while       //////////////////       //repeat from above for the second factor       int tempFactor2 = static_cast<int>(sqrt(origValue  / i));       //       while(1) {         if(primeFactor[primeIter] == 0) {           primeFactor[primeIter] = origValue / i; std::cout << "sec: " << origValue / i;           if(sqrt(origValue / i) - static_cast<double>(tempFactor) == 0) {             multValue[multIter] = tempFactor2;             primeFactor[primeIter] = 0;             ++multIter;             break;           }           ++primeIter;           break;         } else           ++primeIter;       }//while       //////////////////       origValue = i; std::cout << std::endl;     }//if     //////////////////     //assign the factor we are working on if we cant factor anymore     if(i == 2) {       while(1) {         if(primeFactor[primeIter] == 0) {           primeFactor[primeIter] = origValue;           break;         } else           ++primeIter;       }//while     }   }//for   for(int i = 0; i < 10; ++i)     std::cout << "Prime Factor #" << i + 1 << " - " << primeFactor[i] << std::endl;       for(int i = 0; i < 5; ++i)     std::cout << "Multiple #" << i + 1 << " - " << multValue[i] << std::endl;   std::cin.ignore();   std::cin.get(); }```
The fir and sec couts show the steps! :D

It still has a few flaws,
1) it wasnt able to factor 80,000, because of too little sized array (should use a vector).
2) it will end up with factors 2x2, or 3x3, and it wont multiply them, and then sqrt them - like a human being would do.. You could put it all into a function, then call the function again to make sure it factors all the way.
3) it seems to be skipping every 2 in the primeFactor[] array when it wants to enter a value there.
• 10-01-2005
Cactus_Hugger
I've actually written this exact program, although it was for the TI-83+ (and in TI-Basic.) However, I took a different approach.

Factors involve complicated thinking to me, so:
Take X, the value under the square root. If X % (Y*Y) == 0, then an answer is Y * sqrt(X) (since if X is divisible by Y squared, we can say sqrt(XYY) and pull the Y out of the radical.) I converted it to C (while sitting here):
Code:

```#include <stdio.h> #include <math.h> int main() {         int x, y;         printf("Enter a number: ");         scanf("%d", &x);         for(y = ceil(sqrt(x)); y > 1; y--)         {                 if(x % (y * y) == 0) break;         }         printf("Simple form: %d * %c(%d)\n", y, 251, x / (y * y));         return 0; }```
That code has served me faithfully for many (ok, 2) years. The TI code was a bit more complicated (But also worked for cube roots, ect.). Now, if you want to literally output a 'sqrt' sign, that's a bit more complicated. You can output a 0xFB, which looks like a sqrt sign on the windows console. (But isn't very portable.) If you're in some GUI, the square root symbol is the unicode character U+221A.

Quote:

1) it wasnt able to factor 80,000...
sqrt(80000) = 200 * sqrt(2)
Although mine will fail on very large numbers. (>1.9B, shouldn't hurt too much.)
• 10-01-2005
LiNeAr
Cool. Thanks. To bad you wrote the code already :( I kind of wanted to figure it out. But oh well, I am going to work on some other math stuff. I am making a program for figuring out 2 step equations with the form mx+b=mx+b, where the program figures out the value of x. So i'll post that once its done, it's not too hard. PEACE OUT!!!!!!!!! :p
• 10-01-2005
Dae
Quote:

Originally Posted by Cactus_Hugger
I've actually written this exact program, although it was for the TI-83+ (and in TI-Basic.) However, I took a different approach.

Factors involve complicated thinking to me, so:
Take X, the value under the square root. If X % (Y*Y) == 0, then an answer is Y * sqrt(X) (since if X is divisible by Y squared, we can say sqrt(XYY) and pull the Y out of the radical.) I converted it to C (while sitting here):
Code:

```#include <stdio.h> #include <math.h> int main() {         int x, y;         printf("Enter a number: ");         scanf("%d", &x);         for(y = ceil(sqrt(x)); y > 1; y--)         {                 if(x % (y * y) == 0) break;         }         printf("Simple form: %d * %c(%d)\n", y, 251, x / (y * y));         return 0; }```
That code has served me faithfully for many (ok, 2) years. The TI code was a bit more complicated (But also worked for cube roots, ect.). Now, if you want to literally output a 'sqrt' sign, that's a bit more complicated. You can output a 0xFB, which looks like a sqrt sign on the windows console. (But isn't very portable.) If you're in some GUI, the square root symbol is the unicode character U+221A.

sqrt(80000) = 200 * sqrt(2)
Although mine will fail on very large numbers. (>1.9B, shouldn't hurt too much.)

Thats a great algorithm. You definetly thought outside the sqaure on that one.

Search google for better algorithms when making these types of programs too, there could be formulas, or calculus that you can easily convert to C++.