Results 1 to 15 of 15

Thread: Square Root Formula

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Square Root Formula

    I am using a square formula in a programming assignment the formula is as follows in pseudo-code:
    Code:
    INPUT number
    INPUT guess
    
    LOOP
      oldguess = newguess
    
      newguess = 1/2 * (number / oldguess + oldguess)
    WHILE newguess - oldguess > 0.00005
    The problem I'm having is writing this method as a mathematical forumla. I can vaugely remember using square brackets to represent repitition but I honestly can't remember. So I'm wondering if any of you guys could help???
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  2. #2
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hmm...

    you could always do this:

    U1 = 1
    Un = (K/Un-1 + Un-1) /2
    sqrt(K) = lim(f->inf) Uf
    sql_lall

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    VB is like pseudo code for me...

    VB Code:
    1. Public Function SquareRoot(thenumber As Double) as Double
    2. Dim theVals() As Double
    3. ReDim theVals(1 To thenumber)
    4. theVals(1) = (1 / 2) + (thenumber / 2)
    5. For i = 2 To thenumber
    6.     theVals(i) = (theVals(i - 1) / 2) + (thenumber / (2 * theVals(i - 1)))
    7.     If theVals(i) = theVals(i - 1) Then
    8.         SquareRoot = theVals(i)
    9.         Exit Function
    10.     End If
    11. Next i
    12. End Function

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    BTW...keep in mind that this is not a perfect solution........found it on this site a while back....and never have had time to make it perfect....

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Ahha...in VB it will work better like this...

    VB Code:
    1. Public Function SquareRoot(thenumber As Double) As Double
    2. Dim theVals() As Double
    3. ReDim theVals(1 To thenumber) As Double
    4. theVals(1) = (1# / 2#) + (thenumber / 2#)
    5.  
    6. For i = 2 To thenumber
    7.     theVals(i) = (theVals(i - 1) / 2#) + (thenumber / (2# * theVals(i - 1)))
    8.    
    9.         SquareRoot = theVals(i)
    10.    
    11. Next i
    12. End Function


    But for pseudo code the other code is better....
    Last edited by NoteMe; Dec 14th, 2003 at 07:35 PM.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Or actually this is how I would have done it....


    VB Code:
    1. Public Function SquareRoot(thenumber As Double) As Double
    2.  
    3.     Dim Last As Double
    4.     Dim This As Double
    5.    
    6.     Last = (1# / 2#) + (thenumber / 2#)
    7.     This = (Last / 2#) + (thenumber / (2# * Last))
    8.    
    9.     Do While (Last <> This)
    10.         Last = This
    11.         This = (Last / 2#) + (thenumber / (2# * Last))
    12.     Loop
    13.    
    14.     SquareRoot = This
    15.  
    16. End Function

  7. #7

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: hmm...

    Originally posted by sql_lall
    you could always do this:

    U1 = 1
    Un = (K/Un-1 + Un-1) /2
    sqrt(K) = lim(f->inf) Uf
    NoteMe - unfortunatly pseudo code is not mathematical notation.

    sql_lall: What does that formula mean.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking :D

    Ok, a brief explanation:

    1) Have you heard of recursion? Stuff like this:
    A0=0, A1=1,
    An = An-1 + An+1

    This is the Fibonacci series, as you can figure out that:
    A2 = 1, A3 = 2, A4=3...

    2) U1 = 1: Un = (K/Un-1 + Un-1) /2 <--- this is also recursion

    Basically, this is the part which works out the next term from the last one. The square root of the number K is the 'last' term of this series. However, as there is no 'last' term, it is simply the asymptote of the values, which is written as:
    lim(f->inf) Uf
    sql_lall

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Re: hmm...

    Originally posted by visualAd
    NoteMe - unfortunatly pseudo code is not mathematical notation.

    What did you not understand with it....? ALl of it?

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    whats wrong with sqrt(x)?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by kedaman
    whats wrong with sqrt(x)?

    It is a programming assignment....

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well he is asking for the mathematical notation.. not the code

    I think this is newton raphson approximation btw so you can write
    x= (x0 - n/x0)/2 or without x0 as divisor x= 3/2 x0 (1- x0^2/n)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I am not going to argue with you on this too...but that is not pseudo code either....

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I don't think that was what he was looking for either
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I want to impress the tutor by putting it in mathematical notation.

    I think I knew all this once but - after a year with no math(s) lessons I have gone all rusty
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width