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

Source code in the factor programming language

USING: combinators.short-circuit formatting io kernel lists
lists.lazy literals math math.parser sequences tools.time ;
IN: rosetta-code.2-3-palindromes

CONSTANT: info $[
    "The first 6 numbers which are palindromic in both binary "
    "and ternary:" append
]

: expand ( n -- m ) 3 >base dup <reversed> "1" glue 3 base> ;

: 2-3-pal? ( n -- ? )
    expand >bin
    { [ length odd? ] [ dup <reversed> sequence= ] } 1&& ;

: first6 ( -- seq )
    4 0 lfrom [ 2-3-pal? ] lfilter ltake list>array
    [ expand ] map { 0 1 } prepend ;

: main ( -- )
    info print nl first6 [
        dup [ >bin ] [ 3 >base ] bi
        "Decimal : %d\nBinary  : %s\nTernary : %s\n\n" printf
    ] each ;

[ main ] time


  

You may also check:How to resolve the algorithm Yin and yang step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Perl programming language
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the Sidef programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the Logo programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Smalltalk programming language