well, Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
'txtnum is starting square root
'txtout1 is number outside of square root (part of simplified answer)
'txtout2 is number inside of square root (part of simplified answer)
'Not a number then abort code
If Not IsNumeric(txtnum.Text) Then Exit Sub
Dim num As Integer = txtnum.Text
'First check if perfect number
If Not IsDecimal(Math.Sqrt(num)) Then
'Number outside of square root
txtout1.Text = Math.Sqrt(num)
'Number inside square root (nothing in this case)
txtout2.Text = "x"
Exit Sub
End If
Dim t As Double
For i As Integer = 15 To 2 Step -1
t = num / (i * i)
If Not IsDecimal(t) Then
'Number outside of square root
txtout1.Text = Math.Sqrt(i * i)
'Number inside square root (not a perfect square)
txtout2.Text = num / (i * i)
Exit Sub
End If
Next
'Square root can't be simplified any further
txtout1.Text = "x"
txtout2.Text = "x"
End Sub
Now, first of course it must check that a number was inserted. Then it checks right off the bat if the number inserted is already a perfect square. This is done by getting the square root of the number, if the result is a whole number, then it is a perfect square and no simplification needs to be done.
If it is not a perfect square, then it's a matter of seeing if it's divisible by perfect squares. The code will just check for small numbers, starting with the square of 15 going down to the square of 2, this can be modified to work with bigger numbers. It checks it backwards to make sure it finds the biggest possible square. If it finds a square that fits, then it just gets the square root (will be whole number) and writes it to the outside of the square root, and writes to the inside of the square root how many times the square goes into the original number.
Answered by
Romi
, an ibibo Master,
at
5:12 PM on July 01, 2008