Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / Visual Basic
Article

Simplifying Square Roots

Rate me:
Please Sign up or sign in to vote.
2.89/5 (10 votes)
23 Apr 2005CPOL2 min read 73.4K   154   13   7
Breaks down square roots to their simplest form.

Introduction

This is a basic program that takes a square root and simplifies it for you. I always find myself needing to do this kind of thing and the calculators don't seem to come with this option so I decided to write this simple program. For example, it takes the square root of 60 and breaks it down to 4 * 15. It then returns "2 √15" (simplified). It comes in handy when big numbers are used.

IsDecimal function

The way it works is it has to check at several points if numbers are perfect squares or not, and the way to do this is by getting the square root and checking whether it's a whole number or a decimal. Here is the IsDecimal function:

VB
Public Function IsDecimal(ByVal num As String) As Boolean
      Dim t As String
      For i As Integer = 0 To num.Length - 1
           t = num.Substring(i, 1)
           If t = "." Then Return True
      Next
      Return False
End Function

What this function does is it takes the input number as a String and checks through every character. If a "." is found, then it returns True (it is a decimal), if no "." is found then it returns False (it is a whole number).

Simplifying square root code

First take a look at the code:

VB
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.

Conclusion

This is a small code that is pretty useful when doing math homework :). Also the IsDecimal function might come in handy for other apps. Any comments or bugs are always welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Visit Visual C# Kicks for more .Net Resources and Articles at vckicks.110mb.com

Comments and Discussions

 
GeneralNew C# Version Pin
VCSKicks21-Aug-08 19:12
VCSKicks21-Aug-08 19:12 
GeneralOther Possible Features Pin
JasonSG7-May-05 6:19
JasonSG7-May-05 6:19 
You might want to add the ability to simplify negative numbers, decimals, and use roots other than square roots. I am currently working on my own program to do this, but I ran into trouble with odd roots (like a cube root) using negative numbers (VB gives me -1.#IND for -27^(1/3)).
GeneralSlight changes Pin
iGadget27-Apr-05 5:18
iGadget27-Apr-05 5:18 
GeneralSimplifications Pin
Richard Deeming27-Apr-05 0:31
mveRichard Deeming27-Apr-05 0:31 
GeneralEasy way to check if a number is a decimal Pin
Rei Miyasaka23-Apr-05 16:43
Rei Miyasaka23-Apr-05 16:43 
GeneralRe: Easy way to check if a number is a decimal Pin
gpmaker23-Apr-05 18:59
gpmaker23-Apr-05 18:59 
GeneralRe: Easy way to check if a number is a decimal Pin
Niels Penneman27-Apr-05 3:56
Niels Penneman27-Apr-05 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.