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

Source code in the zkl programming language

fcn pal23W{  //--> iterator returning (index,palindromic number)
   Walker.tweak(fcn(ri,r){  // references to loop start and count of palindromes
      foreach i in ([ri.value..*]){
	 n3:=i.toString(3);
	 n:=String(n3,"1",n3.reverse()).toInt(3);  // create base 3 palindrome
	 n2:= n.toString(2);
	 if(n2.len().isOdd and n2==n2.reverse()){  // stop here, return answer
	    ri.set(i+1);    // continue loop from this value at next iteration
	    return(r.inc(),n);
	 }
      }
   }.fp(Ref(3),Ref(3))).push(T(1,0),T(2,1))  // seed with first two results
}

foreach idx,n in (pal23W().walk(6)){
   println("%2d: %,d == %.3B(3) == %.2B(2)".fmt(idx,n,n,n))
}

  

You may also check:How to resolve the algorithm Longest increasing subsequence step by step in the Clojure programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the BCPL programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Go programming language
You may also check:How to resolve the algorithm Digital root step by step in the Rust programming language