|
-
Dec 10th, 2003, 05:28 PM
#1
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???
-
Dec 11th, 2003, 04:01 AM
#2
-
Dec 14th, 2003, 06:59 PM
#3
VB is like pseudo code for me...
VB Code:
Public Function SquareRoot(thenumber As Double) as Double
Dim theVals() As Double
ReDim theVals(1 To thenumber)
theVals(1) = (1 / 2) + (thenumber / 2)
For i = 2 To thenumber
theVals(i) = (theVals(i - 1) / 2) + (thenumber / (2 * theVals(i - 1)))
If theVals(i) = theVals(i - 1) Then
SquareRoot = theVals(i)
Exit Function
End If
Next i
End Function
-
Dec 14th, 2003, 07:06 PM
#4
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....
-
Dec 14th, 2003, 07:25 PM
#5
Ahha...in VB it will work better like this...
VB Code:
Public Function SquareRoot(thenumber As Double) As Double
Dim theVals() As Double
ReDim theVals(1 To thenumber) As Double
theVals(1) = (1# / 2#) + (thenumber / 2#)
For i = 2 To thenumber
theVals(i) = (theVals(i - 1) / 2#) + (thenumber / (2# * theVals(i - 1)))
SquareRoot = theVals(i)
Next i
End Function
But for pseudo code the other code is better....
Last edited by NoteMe; Dec 14th, 2003 at 07:35 PM.
-
Dec 14th, 2003, 07:40 PM
#6
Or actually this is how I would have done it....
VB Code:
Public Function SquareRoot(thenumber As Double) As Double
Dim Last As Double
Dim This As Double
Last = (1# / 2#) + (thenumber / 2#)
This = (Last / 2#) + (thenumber / (2# * Last))
Do While (Last <> This)
Last = This
This = (Last / 2#) + (thenumber / (2# * Last))
Loop
SquareRoot = This
End Function
-
Dec 15th, 2003, 04:02 AM
#7
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.
-
Dec 15th, 2003, 04:19 AM
#8
Fanatic Member
: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 
-
Dec 15th, 2003, 08:39 AM
#9
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?
-
Dec 15th, 2003, 03:52 PM
#10
transcendental analytic
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.
-
Dec 15th, 2003, 03:56 PM
#11
Originally posted by kedaman
whats wrong with sqrt(x)?
It is a programming assignment....
-
Dec 15th, 2003, 04:03 PM
#12
transcendental analytic
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.
-
Dec 15th, 2003, 04:06 PM
#13
I am not going to argue with you on this too...but that is not pseudo code either....
-
Dec 15th, 2003, 04:08 PM
#14
transcendental analytic
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.
-
Dec 15th, 2003, 05:08 PM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|