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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,916 questions

51,849 answers

573 users

How to find the first 18 circular prime numbers (cyclically rotate left will also be prime) in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Function is_prime(ByVal n As Integer) As Boolean
        If n = 2 Then
            Return True
        End If

        If n < 2 OrElse n Mod 2 = 0 Then
            Return False
        End If

        Dim i As Integer = 3

        While i * i <= n
            If n Mod i = 0 Then
                Return False
            End If

            i += 2
        End While

        Return True
    End Function

    Public Shared Function cyclically_rotate_left(ByVal n As Integer) As Integer
        Dim m As Integer = n
        Dim p As Integer = 1

        While m >= 10 
			p = p * 10
			m = m \ 10
        End While

        Return CInt((m + 10 * (n Mod p)))
    End Function

    Public Shared Function is_circular_prime(ByVal n As Integer) As Boolean
        If Not is_prime(n) Then
            Return False
        End If

        Dim rotated_n As Integer = cyclically_rotate_left(n)

        While rotated_n <> n
            If rotated_n < n OrElse Not is_prime(rotated_n) Then
                Return False
            End If

            rotated_n = cyclically_rotate_left(rotated_n)
        End While

        Return True
    End Function

    Public Shared Sub Main()
        Dim n As Integer = 2
		Dim i As Integer = 0

		While i < 18  
            If is_circular_prime(n) Then
                If i > 0 Then
                    Console.Write(", ")
                End If

                Console.Write(n)
                i = i + 1
            End If
			n = n + 1
		End While
        
    End Sub
End Class



' run:
' 
' 2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939
'

 



answered Mar 24, 2023 by avibootz
...