How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

It's permissible to assume the first two numbers and simply list them.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0 Win64

'converts decimal "n" to its ternary equivalent
Function Ter(n As UInteger) As String
  If n = 0 Then Return "0"
  Dim result As String = ""
  While n > 0
    result = (n Mod 3) & result
    n \= 3
  Wend
  Return result
End Function

' check if a binary or ternary numeric string "s" is palindromic
Function isPalindromic(s As String) As Boolean
  ' we can assume "s" will have an odd number of digits, so can ignore the middle digit
  Dim As UInteger length = Len(s)
  For i As UInteger = 0 To length \ 2 - 1
    If s[i] <> s[length - 1 - i] Then Return False
  Next
  Return True
End Function

' print a number which is both a binary and ternary palindrome in all three bases
Sub printPalindrome(n As UInteger)
  Print "Decimal : "; Str(n)
  Print "Binary  : "; bin(n)
  Print "Ternary : "; ter(n)
  Print
End Sub

' create a ternary palindrome whose left part is the ternary equivalent of "n" and return its decimal equivalent
Function createPalindrome3(n As UInteger) As UInteger
  Dim As String ternary = Ter(n)
  Dim As UInteger power3 = 1, sum = 0, length = Len(ternary)
  For i As Integer = 0 To Length - 1 ''right part of palindrome is mirror image of left part
    If ternary[i] > 48 Then  '' i.e. non-zero
      sum += (ternary[i] - 48) * power3
    End If
    power3 *= 3
  Next
  sum += power3 '' middle digit must be 1
  power3 *= 3
  sum += n * power3  '' value of left part is simply "n" multiplied by appropriate power of 3
  Return sum
End Function

Dim t As Double = timer
Dim As UInteger i = 1, p3, count = 2
Dim As String binStr
Print "The first 6 numbers which are palindromic in both binary and ternary are :"
Print
' we can assume the first two palindromic numbers as per the task description
printPalindrome(0) '' 0 is a palindrome in all 3 bases
printPalindrome(1) '' 1 is a palindrome in all 3 bases
Do
  p3 = createPalindrome3(i)
  If p3 Mod 2 > 0 Then ' cannot be even as binary equivalent would end in zero
    binStr = Bin(p3)  '' Bin function is built into FB
    If Len(binStr) Mod 2 = 1 Then  '' binary palindrome must have an odd number of digits
      If isPalindromic(binStr) Then
        printPalindrome(p3)
        count += 1
      End If
    End If
  End If
  i += 1
Loop Until count = 6
Print "Took ";
Print Using "#.###"; timer - t;
Print " seconds on i3 @ 2.13 GHz"
Print "Press any key to quit"
Sleep

  

You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Suffixation of decimal numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Scala programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Quackery programming language
You may also check:How to resolve the algorithm Empty string step by step in the VBScript programming language