error in the code computing Totient Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sara samy
    New Member
    • Nov 2010
    • 1

    #1

    error in the code computing Totient Function

    i made application for computing totient function
    but it doesn't give me any result
    this is a code
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim n As Integer
            n = CStr(TextBox1.Text)
    
            Dim phi As Integer
            Dim i As Integer
    
            phi = 0
            'Dim i As Integer
            i = 1
            Do While (i < n)
                If (gcd(i, n) = 1) Then
                    phi = phi + 1
                    
                Else
                    Continue Do
                    
                End If
            Loop
    
            Label3.Text = phi
        End Sub
    
    Private Function gcd(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Dim i As Integer
            Dim n As Integer
            
            Dim Remainder As Integer
    
            Do While (n <> 0)
    
                Remainder = i Mod n
                i = n
                n = Remainder
            Loop
    
            gcd = i
    
    
            Return gcd
    Last edited by MMcCarthy; Nov 25 '10, 02:46 PM. Reason: added code tags and changed title
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    At first glance I would say your problem is your variable initialization:

    Code:
          Dim phi As Integer
            Dim i As Integer
     
            phi = 0
            'Dim i As Integer
            i = 1
            'Do While (1 < 0) = no execution!!!
            Do While (i < n)
    Check your variable initialization and re-think your loop conditions. One of these things is wrong.

    Comment

    Working...