Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Module PalindromeChecker
Function ReverseString(input As String) As String
If String.IsNullOrEmpty(input) Then
Return input ' Return as-is if null or empty
End If
' Convert to character array
Dim chars() As Char = input.ToCharArray()
' Reverse the array in place
Array.Reverse(chars)
' Return the reversed string
Return New String(chars)
End Function
' Function to check if a number is a palindrome in a given base
Function IsPalindrome(ByVal num As UInteger, ByVal baseNum As UInteger) As Boolean
If baseNum < 2 Then Return False ' Invalid base
Dim strnumreversed As New StringBuilder()
Dim temp As UInteger = num
' Convert number to string in given base
Do
Dim digit As UInteger = temp Mod baseNum
strnumreversed.Append(If(digit < 10, ChrW(AscW("0"c) + digit), ChrW(AscW("A"c) + (digit - 10))))
temp \= baseNum
Loop While temp > 0
' Check palindrome
Dim strnum As String = strnumreversed.ToString()
Dim reversed As String = ReverseString(strnum)
Return strnum = reversed
End Function
Sub Main()
Dim limit As UInteger = 1000
Console.WriteLine("Numbers that are palindromes in both base 10 and base 2:")
For i As UInteger = 1 To limit
If IsPalindrome(i, 10) AndAlso IsPalindrome(i, 2) Then
Console.Write(i & " (binary: ")
' Print binary representation
Dim binary As New StringBuilder()
Dim temp As UInteger = i
Do
binary.Append(If(temp Mod 2 = 1, "1"c, "0"c))
temp \= 2
Loop While temp > 0
' Reverse binary string
Dim binaryStr As String = ReverseString(binary.ToString())
Console.WriteLine(binaryStr & ")")
End If
Next
End Sub
End Module
' run:
'
' Numbers that are palindromes in both base 10 and base 2:
' 1 (binary: 1)
' 3 (binary: 11)
' 5 (binary: 101)
' 7 (binary: 111)
' 9 (binary: 1001)
' 33 (binary: 100001)
' 99 (binary: 1100011)
' 313 (binary: 100111001)
' 585 (binary: 1001001001)
' 717 (binary: 1011001101)
'