Finding Square Root without using sqrt()

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 5092 Views - Last Post: 05 August 2012 - 09:39 AM

#16 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Finding Square Root without using sqrt()

Posted 08 October 2009 - 05:36 PM

Well pow(x,.5) works pretty good for an alternative
Was This Post Helpful? 0
  • +
  • -

#17 wowelf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-October 09

Re: Finding Square Root without using sqrt()

Posted 20 December 2009 - 03:33 AM

I have to change "int no "to "float no" .Then it's working (sqrt())
Was This Post Helpful? 0
  • +
  • -

#18 voodoomedo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-December 08

Re: Finding Square Root without using sqrt()

Posted 02 June 2010 - 04:48 PM

just thank you ..
Was This Post Helpful? 0
  • +
  • -

#19 krishhna4u   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 10

Re: Finding Square Root without using sqrt()

Posted 20 August 2010 - 04:37 AM

i cant understand the logic of square root please tell me any one x1=i; for(int j=0;j<10;j++) { x2=m; x2/=x1; x2+=x1; x2/=2; x1=x2; } return x2; i am chakri and my profile in this is krishhna4u.
Was This Post Helpful? 0
  • +
  • -

#20 kevywilly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 23-September 10

Re: Finding Square Root without using sqrt()

Posted 23 September 2010 - 11:27 AM

I am just a hack, but here is another method that computes square root of 750,000,000 in just 880 iterations by starting with a guess of 1/2 and doubling the increment each time until the number is reached. The sign of the increment changes depending on whether the result is greater than or less than the desired result. Also, as we approach the solution, the increment is reset to its initial value to avoid overshooting the result. #include #include using namespace std; int dbl(int val); int fact(int val); double squareRoot(double val); int main() { squareRoot(9); squareRoot(144); squareRoot(17); squareRoot(750000000); return 0; } double squareRoot(double val) { // set initial increment value double initial_increment = 0.0000001; double curr_increment = initial_increment; // initialize current increment to initial -(initial increment) because // we will be starting with a guess = 1/2 of the value we are trying to compute // and as a result the initial guess squared will be > value // guess root = 1/2 val and set curr_increment = -(initial_increment) double guess = val / 2.0; //double curr_increment=-initial_increment; // calc the initial the difference double dif = (val - (guess * guess)); // direction parameter are we moving up or down to reach the number? double direction; // counter int i = 0; // loop until we reach our threshold while (abs(dif) > 0.000001) { if (abs(dif) < 0.01) { // if we are getting close, use smaller increments initial_increment = initial_increment / 2.0; } // set the direction - up / down if (dif < 0.0){ direction = -1.0; } else { direction = 1.0; } // if direction has not changed if ((curr_increment/abs(curr_increment)) == (direction/abs(direction))) { curr_increment *= 2.0; } else { // if current increment < 0 use initial_increment, we are changing direction curr_increment = initial_increment * direction; } // to avoid infinite loop use initial_increment if increment > current guess if (abs(curr_increment) > guess) { curr_increment = initial_increment * direction; } guess += curr_increment; // increment the guess dif = (val - (guess * guess)); //calc the diff i++; } cout << "iterations: " << i << "\t initial value:" << val << "\troot: " << guess << "\tcalculated value: " << guess * guess << "\tdiff: " << dif << endl; return guess; }
Was This Post Helpful? 0
  • +
  • -

#21 Kaywon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 11

Re: Finding Square Root without using sqrt()

Posted 26 February 2011 - 02:50 AM

Gud,
Was This Post Helpful? 0
  • +
  • -

#22 Vodkacannon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-November 11

Re: Finding Square Root without using sqrt()

Posted 05 August 2012 - 09:38 AM

square root of x = e^(1/2*log*x) (e is eulers number) The denominator in the fraction corresponds to the root. So you can solve any root. I guess this isn't any better though because you still have to use math calls.
Was This Post Helpful? 0
  • +
  • -

#23 Vodkacannon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-November 11

Re: Finding Square Root without using sqrt()

Posted 05 August 2012 - 09:39 AM

My bad its not log x it's ln(x) (natural logarithm)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2