How to check if number can be made prime by deleting a single digit in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function remove_the_N_digit(ByVal num As Integer, ByVal N As Integer) As Integer
        Dim str As String = Convert.ToString(num)
		
        str = str.Substring(0, N) & str.Substring(N + 1)
		
        Return Integer.Parse(str)
    End Function

    Public Shared Function isPrime(ByVal n As Integer) As Boolean
        If n < 2 OrElse (n Mod 2 = 0 AndAlso n <> 2) Then
            Return False
        End If

        Dim count As Integer = CInt(Math.Floor(Math.Sqrt(n)))

        For i As Integer = 3 To count Step 2
            If n Mod i = 0 Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim n As Integer = 78919
        Dim total_digits As Integer = CInt(Math.Log10(n)) + 1
        Dim i As Integer = 0

        While i < total_digits
            Dim tmp As Integer = remove_the_N_digit(n, i)

            If isPrime(tmp) Then
                Console.Write("yes number = " & tmp)
                Exit While
            End If

            i += 1
        End While
    End Sub
End Class



' run:
'
' yes number = 7919
'

 



answered Jan 18, 2024 by avibootz

Related questions

...