Thread: Square root

  1. #1
    Unregistered
    Guest

    Square root

    I need help getting started writing a program to determine the square root of number using the iteration formula. Printing out the first consecutive pair which are within 0.001 of each other.
    Any help would be greatly appreciated.
    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for ( d = 0.0000; d < totest; d += 0.0001 )
    {
        if( d*d == totest ) printf("%f is the square root of %f", d, totest );
    }
    There. Consider your homework done. I'm feeling incredibly generous today.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    Talking you need to ..

    read the first 3 chapters in your C programming text book first..

    that will explain something about the basic format

    when i was a kid, they taught us to compute square roots using division... i think it was consecutive dividing by the largest square... something like that....


    check out the following explanation, and try to code it.



    the { is the square root symbol

    4. 3 5 8
    Example: {19.00000000000
    4^2 = 16
    -----
    3 00
    83 x 3 = 2 49
    ------
    5100
    865 x 5 = 4325
    ------
    77500
    8708 x 8 = 69664

    Step 1:

    If the number of digits to the left of the decimal point is odd, then
    the first digit of the answer will be the largest number whose square
    is less than the first digit of the original number. If the number of
    digits to the left of the decimal point is even, then the first digit of
    the answer will be the largest number whose square is less than the
    first _two_ digits of the dividend. In this case, 19 is a 2-digit
    number, so the first digit of the quotient is the largest number
    whose square is less than 19, i.e. 4. Write that number above
    either the first digit of the dividend (if odd) or the second digit of
    the dividend (if even)

    Step 2:

    Subtract the square of the number in Step 1, then bring down two
    more digits.

    Step 3:

    Double the current quotient (on top, ignoring any decimal point), and
    put a blank space in front of it. Here 4 x 2 = 8. Put down 8_ x _ to
    the left of the current remainder, in this case 300.

    Step 4:

    The next digit of the quotient will be the largest number that can
    be put in both blanks so that the resulting multiplication problem
    is less than or equal to the current remainder. In this case, the
    number is 3, because 83 x 3 = 249, whereas 84 x 4 = 336 is too
    high. Write this number above the second digit of the next two
    numbers; in this case the 3 would go above the second 0. We now
    have a quotient of 4.3.

    Step 5:

    As long as you want more digits, subtract the product from the
    remainder (i.e. 300 - 249 = 51) and bring down the next two digits,
    in this case 51 turns into 5100, which becomes the current
    remainder. Now repeat steps 3 and 4.

    If you ever get a remainder of zero, you've got a perfect square
    on your hands. I hope this makes sense to you.

  4. #4
    Unregistered
    Guest

    square root

    Thanks for the input, I understood the test for finding 0.001 but was hung up on the process of how to loop the test.
    Again, thanks.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    Hey Quzah???

    Ever take a course in algorithms or advanced data structures where they discuss the concept of "Big O"

    The solution you provided would surely get a big "D" everywhere I went to school... a "D" is being generous.

    That solution was like creating a busy loop for a timer.

    Try it again.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Hey Quzah???

    Originally posted by David21078
    Ever take a course in algorithms or advanced data structures where they discuss the concept of "Big O"

    The solution you provided would surely get a big "D" everywhere I went to school... a "D" is being generous.

    That solution was like creating a busy loop for a timer.

    Try it again.
    I provided exactly what he asked (no, I didn't create a break point for him, but then again, I tend not to do everything for them. Hell, I practicly gave him his entire homework program.), nothing more.

    He asked how to use a loop, by .001 increments, to test for the square root. Yeah, I had one more digit of precision in there.

    Again, it's not my job to do everything for them. He wanted a loop. He got a loop.

    need help getting started writing a program to determine the square root of number using the iteration formula. Printing out the first consecutive pair which are within 0.001 of each other.
    Vola. Loop that tests for square root within .001 increments of eachother.

    He didn't ask: "What is the most efficient possible way to test for square root?"

    He asked how to find the square root to the .001 precision "the iteration forumlae". Iteration = incrementation. Since the precision required was .001, the simplest method is what I've shown above.

    One thing you'll learn when asking me questions: be precise. I'll freely interpret your words as I see fit, so you'd better be damn sure you say exactly the problem, exactly the need, exactly what's wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    I was really having fun...

    busting your chops...

    Really though,

    I think his instructor would really appreciate a more elegant solution; i.e. an algorithm that makes less iterations.

    You know like the old manual method of using division by hand when you were a kid in elementary school.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: I was really having fun...

    Originally posted by David21078
    busting your chops...

    Really though,

    I think his instructor would really appreciate a more elegant solution; i.e. an algorithm that makes less iterations.
    Assuming his instructor actually even realizes that 'void main' is wrong...

    As for classes and instructions, my C is self taught. I've never taken classes on it. I honestly doubt they'd be usefu now. I'm sure there are some classes on advanced theory or algorithms that would be handy, but I don't need them for what I do (unless someone actually decides to pay me to program).

    Again, he didn't know how to do a for loop. Reread that. Then ask me why I gave him the simplest implementation.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    7
    You seem to be interested in this stuff...why not pursue a formal program of instruction...start by taking C for credit..they will teach you some basic algorithm stuff...then C++ where you will get more...and even go into some Object Oriented programming basics... ...

    I am finishing my Master's...in CS, but I am by no means an "expert".. I am also a Software Engineer...and program now in C mostly... but will be doing some C++ stuff really soon... before I did some stuff in Ada..

    Ada is a really good language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Square Root ?!?
    By Tm687 in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 04:38 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM