How to find A and B where A and B are prime numbers and A * B = 12349 in VB.NET

1 Answer

0 votes
Imports System

Module FindABProgram

    ' Function to check if a number is prime
    Function isPrime(n As Integer) As Boolean
        If n <= 1 Then Return False
        If n <= 3 Then Return True
        If (n Mod 2 = 0) OrElse (n Mod 3 = 0) Then Return False

        Dim limit As Integer = CInt(Math.Sqrt(n))
        Dim i As Integer = 5

        While i <= limit
            If (n Mod i = 0) OrElse (n Mod (i + 2) = 0) Then
                Return False
            End If
            i += 6
        End While

        Return True
    End Function

    ' Function to find the two prime factors A and B
    Sub findAB(N As Integer, ByRef A As Integer, ByRef B As Integer)
        Dim limit As Integer = CInt(Math.Sqrt(N))

        For i As Integer = 2 To limit
            If N Mod i = 0 Then
                Dim j As Integer = N \ i
                If isPrime(i) AndAlso isPrime(j) Then
                    A = i
                    B = j
                    Exit Sub
                End If
            End If
        Next

        A = -1
        B = -1 ' No prime factors found
    End Sub

    Sub Main()
        Dim N As Integer = 12349
        Dim A As Integer
        Dim B As Integer

        findAB(N, A, B)

        If A <> -1 Then
            Console.WriteLine("A = " & A & ", B = " & B)
        Else
            Console.WriteLine("Not found.")
        End If
    End Sub

End Module

			

' run:
'
' A = 53, B = 233
'

 



answered Jun 5 by avibootz

Related questions

...