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

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

# Convert the input integer to a string in the specified base (2 to 36 inclusive)
def convert(base):
  def stream:
    recurse(if . >= base then ./base|floor else empty end) | . % base ;
  [stream] | reverse
  | if   base <  10 then map(tostring) | join("")
    elif base <= 36 then map(if . < 10 then 48 + . else . + 87 end) | implode
    else error("base too large")
    end;

# integer division using integer operations only
def idivide($i; $j):
  ($i % $j) as $mod
  | ($i - $mod) / $j ;

def idivide($j):
  idivide(.; $j);

# If cond then show the result of update before recursing
def iterate(cond; update):
  def i: select(cond) | update | (., i);
  i;

def isPalindrome2:
    if (. % 2 == 0) then . == 0
    else {x:0, n: .}
    | until(.x >= .n;
        .x = .x*2 + (.n % 2)
        | .n |= idivide(2) )
    | .n == .x or .n == (.x|idivide(2))
    end;

def reverse3:
  {n: ., x: 0}
  | until (.n == 0;
      .x = .x*3 + (.n % 3)
      | .n |= idivide(3) )
  | .x;

def show:
  "Decimal : \(.)",
  "Binary  : \(convert(2))",
  "Ternary : \(convert(3))",
  "";

def task($count):
  "The first \($count) numbers which are palindromic in both binary and ternary are:",
  (0|show),
  ({cnt:1,  lo:0, hi:1, pow2:1, pow3:1}
   | iterate( .cnt < $count;
       .emit = null
       | .i = .lo
       | until (.i >= .hi or .emit;
           ((.i*3+1)*.pow3 + (.i|reverse3)) as $n
           | if $n|isPalindrome2 
             then .emit = [$n|show]
             | .cnt += 1
             else .
             end
           | .i += 1 )
       | if .cnt == $count then . # all done
         else if .i == .pow3
              then .pow3 *= 3
              else .pow2 *= 4
              end
         | .break = false
         | until( .break;
             until(.pow2 > .pow3; .pow2 *= 4)
             | .lo2 = idivide( idivide(.pow2;.pow3) - 1; 3)
             | .hi2 = (idivide(idivide(.pow2*2;.pow3)-1;3) + 1)
             | .lo3 = (.pow3|idivide(3))
             | .hi3 = .pow3
             | if   .lo2 >= .hi3 then .pow3 *= 3
               elif .lo3 >= .hi2 then .pow2 *= 4
               else .lo = ([.lo2, .lo3]|max)
               | .hi = ([.hi2, .hi3]|min)
               | .break = true
               end )
	 end)
     | select(.emit).emit[] );

task(6)

  

You may also check:How to resolve the algorithm Reverse words in a string step by step in the Pascal programming language
You may also check:How to resolve the algorithm Binary search step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the Factor programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the i programming language