Skip to main content

Egyptian fraction solution in vb.net

Vb program for Egyptian fraction solution. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net program for
'  Egyptian fraction solution
public Class Fraction
    Public Sub egyptianFraction(ByVal a As Integer, 
                                ByVal b As Integer)
        if (a = 0 OrElse b = 0) Then
            Return
        End If
        if (b >= a) Then
            '  Calculate remainder
            Dim remainder As Integer = b Mod a
            '  Calculate divisor
            Dim divisor As Integer = Int(b / a)
            if (remainder <> 0) Then
                divisor += 1
                Console.Write("1/" + divisor.ToString() + " + ")
                a = a * divisor - b
                b = b * divisor
                Me.egyptianFraction(a, b)
            Else
                '  When remainder is zero
                Console.Write("1/" + divisor.ToString())
            End IF
        Else
            '  When b < a
            Console.Write((a / b).ToString() + " + ")
            Me.egyptianFraction(a Mod b, b)
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Fraction = New Fraction()
        '  Two numbers
        Dim a As Integer = 7
        Dim b As Integer = 53
        task.egyptianFraction(a, b)
    End Sub
End Class

Output

1/8 + 1/142 + 1/30104




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment