Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Fun with continued fractions

, 26 Sep 2012
Illustrates the calculation and the usefulness of continued fractions
ContinuedFractions.zip
ContinuedFractions
ContinuedFractions.suo
ContinuedFractions
bin
Debug
ContinuedFractions.exe
ContinuedFractions.pdb
ContinuedFractions.vshost.exe
ContinuedFractions.vshost.exe.manifest
My Project
MyExtensions
Settings.settings
obj
x86
Debug
ContinuedFractions.exe
ContinuedFractions.g.resources
ContinuedFractions.pdb
ContinuedFractions.Resources.resources
ContinuedFractions_MarkupCompile.cache
ContinuedFractions_MarkupCompile.i.cache
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
MainWindow.baml
TempPE
My Project.Resources.Designer.vb.dll
Imports System.Globalization
Class MainWindow

    Dim result As New List(Of Fraction)

    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim style As NumberStyles
        Dim culture As CultureInfo
        Dim input As String

        input = EvaluateBrackets(TextBox1.Text.Replace(",", ".")).Replace("(", "").Replace(")", "")
        ' TextBox1.Text = input

        Dim number As Decimal
        Style = NumberStyles.AllowDecimalPoint
        culture = CultureInfo.InvariantCulture
        If Decimal.TryParse(input, style, culture, number) Then
            Dim Frac As New ContinuedFraction(number, ComboBox1.SelectedItem)
            TextBox2.Text = Frac.SeriesToString
            TextBox2.Text &= vbCrLf
            TextBox2.Text &= Frac.ConvergentFractionToString
        Else
            MessageBox.Show("Number cant be converted to a decimal, check input and try again")
        End If
        
    End Sub
    Dim er As New List(Of Integer)
    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        For i As Integer = 0 To 100
            er.Add(i)
        Next
        ComboBox1.ItemsSource = er
        ComboBox1.SelectedIndex = 0
    End Sub

    Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles TextBox1.KeyDown
        If e.Key = Key.Return Then
            TextBox1.Text = EvaluateBrackets(TextBox1.Text)
        End If
    End Sub
End Class

Public Class ContinuedFraction
    Private _Series As New List(Of System.Numerics.BigInteger)
    Public Property Series() As List(Of System.Numerics.BigInteger)
        Get
            Return _Series
        End Get
        Set(ByVal value As List(Of System.Numerics.BigInteger))
            _Series = value
        End Set
    End Property

    Private _ConvergentFractions As New List(Of Fraction)
    Public Property ConvergentFraction() As List(Of Fraction)
        Get
            Return _ConvergentFractions
        End Get
        Set(ByVal value As List(Of Fraction))
            _ConvergentFractions = value
        End Set
    End Property

    Public Function ConvergentFractionToString() As String
        Dim result As New Text.StringBuilder
        result.Append("[ ")
        For i As Integer = 0 To Series.Count - 1
            If Not i = Series.Count - 1 Then
                result.Append(GetFraction(i).ToString & "; ")
            Else
                result.Append(GetFraction(i).ToString & " ]")
            End If
        Next
        Return result.ToString
    End Function

    Public Function SeriesToString() As String
        Dim result As New Text.StringBuilder
        result.Append("a_n = [ ")
        For i As Integer = 0 To Series.Count - 1
            If Not i = Series.Count - 1 Then
                result.Append(Series(i).ToString & "; ")
            Else
                result.Append(Series(i).ToString & " ]")
            End If
        Next
        Return result.ToString
    End Function

    Sub New(ByVal input As Decimal, ByVal NrOfFractions As Integer)
        Dim intput As Decimal = input
        Dim temp_dec As System.Numerics.BigInteger
        For i As Integer = 0 To NrOfFractions
            temp_dec = Floor(intput)
            intput = intput - Math.Floor(intput)
            If Not intput = 0 Then
                intput = 1 / intput
            Else
                intput = 0
            End If
            Series.Add(temp_dec)
        Next
    End Sub

    Private Function GetSeriesExpantion(ByVal input As Decimal, ByVal NrFraction As Integer) As Integer
        Dim intput As Decimal = input
        Dim temp_dec As Integer
        For i As Integer = 0 To NrFraction
            temp_dec = CInt(Math.Floor(intput))
            intput = intput - Math.Floor(intput)
            If Not intput = 0 Then
                intput = 1 / intput
            Else
                intput = 0
            End If
        Next
        Return temp_dec
    End Function

    Private Function Floor(ByVal inp As Decimal) As System.Numerics.BigInteger
        Dim result As System.Numerics.BigInteger
        Dim str As String
        If inp.ToString.Contains(".") Then
            str = inp.ToString.Split(".")(0)
        Else
            str = inp.ToString.Split(",")(0)
        End If
        result = str
        Return result
    End Function
    Public Function GetFraction(ByVal Steps As Integer) As String
        Dim result As New Fraction
        result.Denominator = Series(Steps)
        result.Numerator = 1

        If Not Steps = 0 Then
            For i As Integer = Steps - 1 To 0 Step -1
                If Not result.Denominator = 0 Then
                    result = Series(i) + 1 / result
                End If
            Next
        End If

        Return result.ToString
    End Function
End Class

Public Structure Fraction
    Public pDenominator As System.Numerics.BigInteger
    Public pNumerator As System.Numerics.BigInteger

    Public Sub New(ByVal Numerator As System.Numerics.BigInteger, ByVal Denominator As System.Numerics.BigInteger)
        pNumerator = Numerator
        pDenominator = Denominator
    End Sub

    Public Sub New(ByVal Desimal As Double)

    End Sub

    Public Property Numerator() As System.Numerics.BigInteger
        Get
            Return pNumerator
        End Get
        Set(ByVal value As System.Numerics.BigInteger)
            pNumerator = value
        End Set
    End Property

    Public Property Denominator() As System.Numerics.BigInteger
        Get
            Return pDenominator
        End Get
        Set(ByVal value As System.Numerics.BigInteger)
            pDenominator = value
        End Set
    End Property

    ' Unary operators:
    Public Shared Operator +(ByVal c As Fraction) As Fraction
        Return c
    End Operator

    Public Shared Operator -(ByVal c As Fraction) As Fraction
        Return New Fraction(-c.Numerator, -c.Denominator)
    End Operator

    Public Overloads Shared Operator +(ByVal N1 As System.Numerics.BigInteger, ByVal N2 As Fraction) As Fraction
        Dim result As New Fraction
        result.Denominator = N2.Numerator * N1 + N2.Denominator
        result.Numerator = N2.Numerator
        result = MinFraction(result)
        Return result
    End Operator

    Public Overloads Shared Operator *(ByVal N1 As Fraction, ByVal N2 As Fraction) As Fraction
        Dim Result As New Fraction
        Result.Numerator = N1.Numerator * N2.Numerator
        Result.Denominator = N1.Denominator * N2.Denominator

        Result = MinFraction(Result)

        Return Result
    End Operator

    Public Overloads Shared Operator /(ByVal N1 As Fraction, ByVal N2 As Fraction) As Fraction
        Dim result As New Fraction
        result.Denominator = N1.Denominator * N2.Numerator
        result.Numerator = N1.Numerator * N2.Denominator

        result = MinFraction(result)

        Return result
    End Operator

    Public Overloads Shared Operator /(ByVal N1 As System.Numerics.BigInteger, ByVal N2 As Fraction) As Fraction
        Dim result, tmp As New Fraction
        tmp.Denominator = N1
        tmp.Numerator = 1
        result.Denominator = tmp.Denominator * N2.Numerator
        result.Numerator = tmp.Numerator * N2.Denominator

        result = MinFraction(result)

        Return result
    End Operator


    Private Shared Function MinFraction(ByVal FratNumber As Fraction) As Fraction
        Dim commonNum As System.Numerics.BigInteger = FratNumber.Numerator
        Dim commonDev As System.Numerics.BigInteger = FratNumber.Denominator

        Dim counter As System.Numerics.BigInteger = 0
        If commonNum < commonDev Then
            counter = commonNum
        Else
            counter = commonDev
        End If
        If Not counter > 1000000 Then
            For a = 2 To counter
                If commonNum Mod a = 0 Then
                    If commonDev Mod a = 0 Then
                        commonNum /= a
                        commonDev /= a
                    End If
                End If
            Next
        Else
            Debug.WriteLine(counter)
        End If
        FratNumber.Numerator = commonNum
        FratNumber.Denominator = commonDev

        Return FratNumber

    End Function
    Public Overrides Function ToString() As String
        Dim result As String
        If Me.Denominator = 0 Then
            result = "0"
        ElseIf Me.Numerator = 1 Then
            result = Me.Denominator.ToString
        Else
            result = Me.Denominator.ToString & "/" & Me.Numerator.ToString
        End If
        Return result
    End Function

End Structure

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Kenneth Haugland
Engineer
Norway Norway
I hope that you like the stuff I have created and if you do wish to say thank you then a donation is always appreciated.
You can donate here[^].

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Kenneth Haugland
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid