How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the JavaScript 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 JavaScript 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 JavaScript programming language
The provided code implements two functions, base3Palindrome
and isBinPal
, to find and filter ternary palindromes that are also binary palindromes. It then tabulates these results in a nicely formatted table.
Here's a detailed breakdown of the code:
Generic Functions:
- range: Creates a range of numbers between
m
andn
. - compose: Composes two functions, returning a new function that applies the first function to the result of the second function.
- listApply: Applies a list of functions to a list of arguments, returning a list of results.
- pure: Wraps a value in a one-element list.
- curry: Curries a function, allowing it to be applied partially.
- transpose: Transposes a list of lists, swapping rows and columns.
- reverse: Reverses the order of elements in a list.
- take: Takes the first
n
elements from a list. - drop: Drops the first
n
elements from a list. - maximum: Finds the maximum element in a list.
- quotRem: Divides two integers, returning the quotient and remainder.
- length: Returns the length of a list.
- justifyLeft: Left-justifies a string by padding it with a specified character to reach a desired length.
- unwords: Joins a list of strings into a single string with spaces in between.
- unlines: Joins a list of strings into a single string with newlines in between.
Bases and Palindromes:
- show, showBinary, showTernary: Convert an integer to a string in base 10, 2, and 3, respectively.
- readBase3: Converts a string in base 3 to an integer.
- base3Palindrome: Creates a ternary palindrome by adding a "1" in the middle and reversing the first half.
- isBinPal: Checks if a binary number is a palindrome by comparing the first and second halves.
- solutions: A list of integers that are ternary palindromes and also binary palindromes.
Tabulation:
- cols: Transposes the rows of a table to create columns.
- cols.map(col => ...): Applies a function to each column of the table, transforming each column element.
- curry(justifyLeft)(...): Curries the
justifyLeft
function with the desired width, ensuring that all column elements are left-justified. - unlines(transpose(...)): Unlines the transposed table, creating each row as a string and combining them with newlines.
In summary, the code finds a list of numbers that are both ternary and binary palindromes, then creates a formatted table of these numbers and their corresponding representations in each base. The table is left-justified and aligned to ensure readability.
Source code in the javascript programming language
(() => {
'use strict';
// GENERIC FUNCTIONS
// range :: Int -> Int -> [Int]
const range = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// compose :: (b -> c) -> (a -> b) -> (a -> c)
const compose = (f, g) => x => f(g(x));
// listApply :: [(a -> b)] -> [a] -> [b]
const listApply = (fs, xs) =>
[].concat.apply([], fs.map(f =>
[].concat.apply([], xs.map(x => [f(x)]))));
// pure :: a -> [a]
const pure = x => [x];
// curry :: Function -> Function
const curry = (f, ...args) => {
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
function () {
return go(xs.concat([].slice.apply(arguments)));
};
return go([].slice.call(args, 1));
};
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
// reverse :: [a] -> [a]
const reverse = xs =>
typeof xs === 'string' ? (
xs.split('')
.reverse()
.join('')
) : xs.slice(0)
.reverse();
// take :: Int -> [a] -> [a]
const take = (n, xs) => xs.slice(0, n);
// drop :: Int -> [a] -> [a]
const drop = (n, xs) => xs.slice(n);
// maximum :: [a] -> a
const maximum = xs =>
xs.reduce((a, x) => (x > a || a === undefined ? x : a), undefined);
// quotRem :: Integral a => a -> a -> (a, a)
const quotRem = (m, n) => [Math.floor(m / n), m % n];
// length :: [a] -> Int
const length = xs => xs.length;
// justifyLeft :: Int -> Char -> Text -> Text
const justifyLeft = (n, cFiller, strText) =>
n > strText.length ? (
(strText + cFiller.repeat(n))
.substr(0, n)
) : strText;
// unwords :: [String] -> String
const unwords = xs => xs.join(' ');
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// BASES AND PALINDROMES
// show, showBinary, showTernary :: Int -> String
const show = n => n.toString(10);
const showBinary = n => n.toString(2);
const showTernary = n => n.toString(3);
// readBase3 :: String -> Int
const readBase3 = s => parseInt(s, 3);
// base3Palindrome :: Int -> String
const base3Palindrome = n => {
const s = showTernary(n);
return s + '1' + reverse(s);
};
// isBinPal :: Int -> Bool
const isBinPal = n => {
const
s = showBinary(n),
[q, r] = quotRem(s.length, 2);
return (r !== 0) && drop(q + 1, s) === reverse(take(q, s));
};
// solutions :: [Int]
const solutions = [0, 1].concat(range(1, 10E5)
.map(compose(readBase3, base3Palindrome))
.filter(isBinPal));
// TABULATION
// cols :: [[Int]]
const cols = transpose(
[
['Decimal', 'Ternary', 'Binary']
].concat(
solutions.map(
compose(
xs => listApply([show, showTernary, showBinary], xs),
pure
)
)
)
);
return unlines(
transpose(cols.map(col => col.map(
curry(justifyLeft)(maximum(col.map(length)) + 1, ' ')
)))
.map(unwords));
})();
You may also check:How to resolve the algorithm Wilson primes of order n step by step in the BASIC programming language
You may also check:How to resolve the algorithm Call a function step by step in the Lingo programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the CLIPS programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Picat programming language