Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,849 questions

55,678 answers

573 users

How to find the first 4-digit prime number where all digits are unique in VB.NET

1 Answer

0 votes
Imports System

Module Program

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

        Dim limit As Integer = CInt(Math.Sqrt(n))
        For i As Integer = 3 To limit Step 2
            If n Mod i = 0 Then Return False
        Next
        Return True
    End Function

    ' Function to check if all digits are unique
    Function HasUniqueDigits(n As Integer) As Boolean
        Dim seen(9) As Boolean  ' track digits 0–9

        While n > 0
            Dim d As Integer = n Mod 10
            If seen(d) Then Return False  ' duplicate found
            seen(d) = True
            n \= 10
        End While
        Return True
    End Function

    Sub Main()
        For num As Integer = 1000 To 9999
            If IsPrime(num) AndAlso HasUniqueDigits(num) Then
                Console.WriteLine($"First 4-digit prime with all unique digits: {num}")
                Return  ' stop after finding the first one
            End If
        Next

        Console.WriteLine("No such number found.")
    End Sub

End Module



' run:
'
' First 4-digit prime with all unique digits: 1039
'

 



answered Nov 20, 2025 by avibootz
...