Thread: Finding root of a number

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    24

    Finding root of a number

    So i'm trying to write a program that finds the root of a number without formulas i.e. sqrt(x).

    so far i have come up with
    Code:
    void Roots(double a, int b)
    {
    	double x;
    
    	switch(b)
    	{
    		case 2:
    			for(x = 0; x < a; x = x + .001)
    			{
    				if ( a == x*x)
    				{
    					cout << "\n\tThe square root of " << a << " equals " << x;
    				}
    			}
    			break;
    		default:
    			cout << "\n\tI haven't got to that switch yet";
    
    	}
    }
    and in my calling program i have:
    Code:
    Root(5,2);
    When i change that 2 to a 3, i get my default, but when i have it as a 2 to activate case 2, it prints nothing out. Why?

    Also can anyone explain to me how you find rational roots of numbers i.e. 2^(5/2). I want to write a general function to do not only whole roots, but also rational roots. I have been reading this but i'm tired and its not clicking : Calculate square root without a calculator

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could get rid of the switch if you wrote your own Pow function.

    Code:
    if ( a == Pow(x, b) )
    The next thing is that it is not always going to be exactly equal, since your function has poor precision (and floating point numbers are only approximations anyway). You should rather try to minimize the difference between a and the Pow.

    Also, instead of increments of 0.001, you might also try something like a binary search. Start with a gross overestimation (e.g a) and an underestimation (e.g 0), and test the middle point between those. Repeat until result is good enough.
    Last edited by anon; 03-06-2011 at 04:28 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    i can't use pow or sqrt or the like, defeats the purpose.

    i'm trying to do it using only for loops and if commands.

    Also why doesn't it return a thing for 2?
    Last edited by dvsumosize; 03-06-2011 at 05:24 PM.

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    look up Newton's Method.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    I looked up the newtons method and it works for when you know the root and you can make switch for it, which was what i was doing at first. But i can't do it for an unknown fractional root i.e. (3/7) because that would entail putting a pow in it right? this is what i mean

    Code:
    		case 4:
    			for (c = 0; c  < 15; c++)
    			{
    				d = d - (d*d*d*d - a)/(4*d*d*d);
    			}
    			cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
    			break;
    i can only do those d like that hwen i know the root beforehand, otherwise i would need to do pow(d,b) which would entail using the pow and that defeats what i do. Got a suggestion?

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Make your own function that will provide you the functionality of pow(number) and try using that.
    Also, if you want to calculate root with rational numbers, yes you can, but make a class of rational numbers and overload some of necessary operators to make it work...

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    so i basically got my case ones down
    using logic and newton method

    Code:
    void Roots(double a, int b)
    {
    	double x,d = 10;
    	int c;
    
    	switch(b)
    	{
    		case 2:
    			
    			for(x = 0; x < a; x = x + .001)
    			{
    				if ( a / x >= x - 0.001 && a / x <= x + 0.001 )
    				{
    					cout << "\n\tThe square root of " << a << " = " << fixed << setprecision(3) << x;
    				}
    			}
    			break;
    		case 3:
    			if ( a > 0 )
    			{
    				for (c = 0; c  < 15; c++)
    				{
    					d = d - (d*d*d - a)/(4*d*d);
    				}
    				cout << "\n\tThe third root of " << a << " = " << fixed << setprecision(3) << d ;
    			}
    			else
    			{
    				a = a * -1;
    				for (c = 0; c  < 15; c++)
    				{
    					d = d - (d*d*d - a)/(4*d*d);
    				}
    				cout << "\n\tThe third root of " << a * -1 << " = " << fixed << setprecision(3) << d * -1;
    			}
    			break;
    		case 4:
    			for (c = 0; c  < 15; c++)
    			{
    				d = d - (d*d*d*d - a)/(4*d*d*d);
    			}
    			cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
    			break;
    		default:
    			cout << "\n\tYou didn't assign that root";
    	}
    }
    but i haven't learned overloading yet, is it possible to use a logical iterator to calculate the nth root without using newton method?

    oh a side note, i was practicing ways of doing it differently
    Code:
    		case 4:
    			/*
    			for (c = 0; c  < 15; c++)
    			{
    				d = d - (d*d*d*d - a)/(4*d*d*d);
    			}
    			cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
    			break;
    			*/
    			for (x = 0; x < a; x++)
    			{
    				if ( x * x * x * x <= a && (x + 1 ) * (x + 1)*(x + 1)*(x + 1) > a)
    				{
    					for ( y = 0; y < 1; y = y + .1)
    					{
    						x = x + y;
    						if ( x * x * x * x <= a && (x + .1 ) * (x + .1) * (x + .1) * (x + .1) > a)
    						{
    							for (z = 0; z < .1; z = z +.01)
    							{
    								x = x + z;
    								if ( x * x * x * x <= a && (x + .01 ) * (x + .01) * (x + .01) * (x + .01) > a)
    								{
    									for (Third = 0; Third < .01; Third = Third + .001)
    									{
    										x = x + Third;
    										if (x * x * x * x <= a && (x + .001 ) * (x + .001) * (x + .001) * (x + .001) > a)
    										{
    											cout << "\n\tThe 4th root of " << a << " = " << x;
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    			break;
    This worked for perfect 4th roots like 81 and 16. But when its a number like 4 or 20 or etc.., it returns nothing. Why?
    Last edited by dvsumosize; 03-07-2011 at 12:29 AM.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Listen what you are trying to do is hard code...
    I suggest you something...
    Take a copy and pen
    Start thinking that if you get the same problem, how your mind will start solving this with all the constraints you are specified with...
    Make a step by step english or your natural language statements, for your ease of understanding, and as soon as you are finished with solving with your mind, now simulate it with the programming language....
    I guarantee you that you will solve this problem in no time...

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The n'th root of a number can be found by dividing the logarithm of the number by n, as follows.

    // cube root of 256
    x = 256;
    n = 3;
    a = log(x) / n;
    r = exp(a);

    Plus, this works for fractional and negative powers.

    Plus, you can use logs to any base, but natural logs (base e) are the most convenient.

    If you can't use log and exp, then write your own, based on these polynomial series.
    Taylor series - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the root of a polynomial
    By budala in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 06:38 AM
  2. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. finding the square root of a number
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2003, 07:46 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM