Private Sub Solve_Click()

'The basic method for finding a sqaure root is simple enough. The system uses a formula

'that approximates a square root. Each approximation comes closer than the previous one.

'The system loops through the formula as many times as needed until the latest

'approximation matches the previous one. At that point the system can do no better at

'the specified level of accuracy and displays its result.

'For example, suppose the user asks to find the square root of 100.

'The system always begins with the answer 5. The formula then will lead to the following

'approximations: 20; 12.5; 10.25 (close enough for an accuracy of 0 decimal points);

'10.003 accuracy < 3); 10.0000004 (accuracy < 6); 10.


'Part I: Declare variables

Dim x, z, m As Double 'Variables used in calculation

Dim y As Integer 'Accuracy of answer

Dim Tester As Boolean 'Control the recursive function

Dim mCompare, zCompare As Double 'Test answer for accuracy



'Part II: Get the input

x = Problem 'Get the number queried

y = Accuracy 'Get the accuracy sought


'Part III: Setup the invariable openers

Tester = False 'Open the loop

z = 5 'Begin all searches by guessing 5

zCompare = 0 'Nothing to compare in first round

'Part IV: Check input for its usability

'Accuracy cannot be more than 10 decimal points

If y > 10 Then

MsgBox "Accuracy cannot exceed 10 decimal points!", vbOKOnly, "Square Root Abacus"

Accuracy = 10

y = 10

End If


'Accuracy cannot be measured with negative numbers

If y < 0 Then

MsgBox "Accuracy cannot have negative decimal points.", vbOKOnly, "Square Root Abacus"

Accuracy = 0

y = 0

End If


'Note: this procedure does not check to make sure the accuracy input

' is an integer. The text box automatically trims input to

' the integer.


'The square roots of negative numbers are imaginary and cannot be

'computed with this algorithm.

If x < 0 Then

MsgBox "Sorry, the abacus cannot solve for negative numbers.", vbOKOnly, "Square Root Abacus"

Exit Sub

End If


'Finding the square root of 0 may lead to division by 0.

If x = 0 Then

Answer = 0

Exit Sub

End If


'Part V: Loop through the algorithm

'The classic algorithm for finding square roots uses this approximation formula:

' Square root of N = 1/2 (N/n + n)

'If that answer is not accurate enough using n = previous approximation.

'This algorithm uses the equation: m = ((x/z)+z)/2

'in which m = approximation, x = N, z = n

'Once the approximation is found it is compared to the previous approximation

'If the two answers match, the result is displayed on the form.

'Otherwise, a further approximation is made.

Do Until Tester

m = ((x / z) + z) / 2


'The comparison only searches for the required level of accuracy.

'The select procedure finds the needed level of accuracy and trims the decimal

'number as required.

'The trimming method:

'(1) round off the last decimal by adding 5 just beyond the required level of accuracy.

' e.g., suppose the approximation = 10.28, accuracy = 1; 10.28 +.05 = 10.33

' so the system will round to 10.3

'(2) multiply the number by a factor that will bring all required decimal points

' to the left of the points. e.g., 10.33 x 10 = 103.3

'(3) convert the number to an integer. e.g., 103.3 = 103

'(4) divide the integer by a factor that restores the decimal. e.g., 103/10 = 10.3


Select Case Accuracy

Case Is = 0

mCompare = Int(m + 0.5)

Case Is = 1

mCompare = Int((m + 0.05) * 10) / 10

Case Is = 2

mCompare = Int((m + 0.005) * 100) / 100

Case Is = 3

mCompare = Int((m + 0.0005) * 1000) / 1000

Case Is = 4

mCompare = Int((m + 0.00005) * 10000) / 10000

Case Is = 5

mCompare = Int((m + 0.000005) * 100000) / 100000

Case Is = 6

mCompare = Int((m + 0.0000005) * 1000000) / 1000000

Case Is = 7

mCompare = Int((m + 0.00000005) * 10000000) / 10000000

Case Is = 8

mCompare = Int((m + 0.000000005) * 100000000) / 100000000

Case Is = 9

mCompare = Int((m + 0.0000000005) * 1000000000) / 1000000000

Case Is = 10

mCompare = Int((m + 0.00000000005) * 10000000000#) / 10000000000#

End Select


'The comparison uses the level of accuracy sought.

If mCompare = zCompare Then

Tester = True 'Ends the looping

Answer = mCompare 'Answer appears on form

Else

z = m

zCompare = mCompare 'This step spares us having to recalculate zCompare


'The algorithm divides by z, so z is checked to be sure it does not = 0

If z = 0 Then

Answer = 0

Exit Sub

End If

End If

Loop

End Sub