How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the Swift 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 Swift 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 Swift programming language

Source code in the swift programming language

import Foundation

func isPalin2(n: Int) -> Bool {
  var x = 0
  var n = n

  guard n & 1 != 0 else {
    return n == 0
  }

  while x < n {
    x = x << 1 | n & 1
    n >>= 1
  }

  return n == x || n == x >> 1
}

func reverse3(n: Int) -> Int {
  var x = 0
  var n = n

  while n > 0 {
    x = x * 3 + (n % 3)
    n /= 3
  }

  return x
}

func printN(_ n: Int, base: Int) {
  var n = n

  print(" ", terminator: "")

  repeat {
    print("\(n % base)", terminator: "")

    n /= base
  } while n > 0

  print("(\(base))", terminator: "")
}

func show(n: Int) {
  print(n, terminator: "")
  printN(n, base: 2)
  printN(n, base: 3)
  print()
}

private var count = 0
private var lo = 0
private var (hi, pow2, pow3) = (1, 1, 1)

show(n: 0)

while true {
  var n: Int

  for i in lo..<hi {
    n = (i * 3 + 1) * pow3 + reverse3(n: i)

    guard isPalin2(n: n) else {
      continue
    }

    show(n: n)
    count += 1

    guard count < 7 else {
      exit(0)
    }
  }

  if hi == pow3 {
    pow3 *= 3
  } else {
    pow2 *= 4
  }

  while true {
    while pow2 <= pow3 {
      pow2 *= 4
    }

    let lo2 = (pow2 / pow3 - 1) / 3
    let hi2 = (pow2 * 2 / pow3 - 1) / 3 + 1
    let lo3 = pow3 / 3
    let hi3 = pow3

    if lo2 >= hi3 {
      pow3 *= 3
    } else if lo3 >= hi2 {
      pow2 *= 4
    } else {
      lo = max(lo2, lo3)
      hi = min(hi2, hi3)
      break
    }
  }
}


  

You may also check:How to resolve the algorithm Bulls and cows/Player step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Use another language to call a function step by step in the Ruby programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Wren programming language
You may also check:How to resolve the algorithm Compound data type step by step in the PL/I programming language
You may also check:How to resolve the algorithm Anagrams step by step in the TUSCRIPT programming language