Simplifing Square Roots

This is a discussion on Simplifing Square Roots within the C++ Programming forums, part of the General Programming Boards category; Hello, I wanted to make a program that could simplify square roots. So before i started anyting i wanted to ...

  1. #1
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31

    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
    IDE: Microsoft Visual C++ .net Standard 2003

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You need to factorize the number, then remove any factors occurring twice and put them outside.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    you can do sqr(9) = 3, or pow(9, 0.5) = 3. I assume that's what your asking for.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    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

  5. #5
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31
    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?
    IDE: Microsoft Visual C++ .net Standard 2003

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.

  7. #7
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    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.

  8. #8
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31
    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.
    IDE: Microsoft Visual C++ .net Standard 2003

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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?
    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)

    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!

    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.
    Last edited by Dae; 10-01-2005 at 06:30 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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.

    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.)
    Last edited by Cactus_Hugger; 10-01-2005 at 07:05 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  11. #11
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31
    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!!!!!!!!!
    Last edited by LiNeAr; 10-01-2005 at 08:12 PM.
    IDE: Microsoft Visual C++ .net Standard 2003

  12. #12
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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++.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Roots?
    By MiroMage in forum C Programming
    Replies: 6
    Last Post: 09-27-2008, 10:28 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  4. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 03:29 AM
  5. Square roots / Powers etc.
    By Robert602 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2001, 03:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21