Find palindromic numbers in both binary and ternary bases: Difference between revisions

m
 
(24 intermediate revisions by 11 users not shown)
Line 22:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V digits = ‘0123456789abcdefghijklmnopqrstuvwxyz’
 
F baseN(=num, b)
Line 55:
 
L(pal23) pal_23(6)
print(pal23‘ ’baseN(pal23, 3)‘ ’baseN(pal23, 2))</langsyntaxhighlight>
 
{{out}}
Line 69:
=={{header|Ada}}==
===Simple Technique (Brute Force)===
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Base_Conversion;
 
procedure Brute is
Line 95:
end if;
end loop;
end Brute;</langsyntaxhighlight>
{{out}}
<pre> 0: 0(2), 0(3)
Line 111:
The code is then very fast and also very much readable than if we had done the bit manipulations by hand.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Unchecked_Conversion;use Ada.Text_IO;
procedure Palindromic is
type Int is mod 2**64; -- the size of the unsigned values we will test doesn't exceed 64 bits
Line 159:
end loop;
end loop Process_Each_Power_Of_4;
end Palindromic;</langsyntaxhighlight>
{{out}}
On a modern machine, (core i5 for example), this code, compiled with the -O3 and -gnatp options, takes less than 5 seconds to give the seven first palindromes smaller than 2^64.
Line 169:
2#1010100001100000100010000011000010101# 3#22122022220102222022122# 90396755477
2#10101001100110110110001110011011001110001101101100110010101# 3#2112200222001222121212221002220022112# 381920985378904469</pre>
 
=={{header|AppleScript}}==
On my machine, this gets the first five results practically instantaneously and the sixth about eight seconds later.
 
<syntaxhighlight lang="applescript">on intToText(int, base) -- Simple version for brevity.
script o
property digits : {int mod base as integer}
end script
set int to int div base
repeat until (int = 0)
set beginning of o's digits to int mod base as integer
set int to int div base
end repeat
return join(o's digits, "")
end intToText
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set output to {"0 0 0", "1 1 1"} -- Results for 0 and 1 taken as read.
set b3Hi to 0 -- Integer value of a palindromic ternary number, minus its low end.
set msdCol to 1 -- Column number (0-based) of its leftmost ternary digit.
set lsdCol to 1 -- Column number of the high end's rightmost ternary digit.
set lsdUnit to 3 ^ lsdCol as integer -- Value of 1 in this column.
set lrdCol to 1 -- Column number of the rightmost hi end digit to mirror in the low end.
set lrdUnit to 3 ^ lrdCol as integer -- Value of 1 in this column.
set columnTrigger to 3 ^ (msdCol + 1) as integer -- Next value that will need an additional column.
repeat until ((count output) = 6)
-- Notionally increment the high end's rightmost column.
set b3Hi to b3Hi + lsdUnit
-- If this carries through to start an additional column, adjust the presets.
if (b3Hi = columnTrigger) then
set lrdCol to lrdCol + msdCol mod 2
set lrdUnit to 3 ^ lrdCol as integer
set msdCol to msdCol + 1
set lsdCol to (msdCol + 1) div 2
set lsdUnit to 3 ^ lsdCol as integer
set columnTrigger to columnTrigger * 3
end if
-- Work out a mirroring low end value and add it in.
set b3Lo to b3Hi div lrdUnit mod 3
repeat with p from (lrdCol + 1) to msdCol
set b3Lo to b3Lo * 3 + b3Hi div (3 ^ p) mod 3
end repeat
set n to b3Hi + b3Lo
-- See if the result's palindromic in base 2 too. It must be an odd number and the value of the
-- reverse of its low end (including any overlap) must match that of its truncated high end.
set oL2b to n mod 2
if (oL2b = 1) then
set b2Hi to n
repeat while (b2Hi > oL2b)
set b2Hi to b2Hi div 2
if (b2Hi > oL2b) then set oL2b to oL2b * 2 + b2Hi mod 2
end repeat
-- If it is, append its decimal, binary, and ternary representations to the output.
if (b2Hi = oL2b) then ¬
set end of output to join({intToText(n, 10), intToText(n, 2), intToText(n, 3)}, " ")
end if
end repeat
set beginning of output to "decimal binary ternary:"
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"decimal binary ternary:
0 0 0
1 1 1
6643 1100111110011 100010001
1422773 101011011010110110101 2200021200022
5415589 10100101010001010100101 101012010210101
90396755477 1010100001100000100010000011000010101 22122022220102222022122"</syntaxhighlight>
 
=={{header|Arturo}}==
{{trans|Ada}}
<syntaxhighlight lang="rebol">pal2?: function [n][
digs2: digits.base:2 n
return digs2 = reverse digs2
]
 
revNumber: function [z][
u: z
result: 0
while [u > 0][
result: result + (2*result) + u%3
u: u/3
]
return result
]
 
pal23: function [][
p3: 1
cnt: 1
print [
pad (to :string 0)++" :" 14
pad.right join to [:string] digits.base:2 0 37 "->"
join to [:string] digits.base:3 0
]
loop 0..31 'p [
while [(p3*(1+3*p3)) < shl 1 2*p]-> p3: p3*3
 
bound: (shl 1 2*p)/3*p3
limDown: max @[p3/3, bound]
limUp: min @[2*bound, p3-1]
if limUp >= limDown [
loop limDown..limUp 'k [
n: (revNumber k) + (1+3*k)*p3
if pal2? n [
print [
pad (to :string n)++" :" 14
pad.right join to [:string] digits.base:2 n 37 "->"
join to [:string] digits.base:3 n
]
cnt: cnt + 1
if cnt=6 -> return null
]
]
]
]
]
 
pal23</syntaxhighlight>
 
{{out}}
 
<pre> 0 : 0 -> 0
1 : 1 -> 1
6643 : 1100111110011 -> 100010001
1422773 : 101011011010110110101 -> 2200021200022
5415589 : 10100101010001010100101 -> 101012010210101
90396755477 : 1010100001100000100010000011000010101 -> 22122022220102222022122</pre>
 
=={{header|C}}==
Per the observations made by the Ruby code (which are correct), the numbers must have odd number of digits in base 3 with a 1 at the middle, and must have odd number of digits in base 2.
<langsyntaxhighlight lang="c">#include <stdio.h>
typedef unsigned long long xint;
 
Line 253 ⟶ 396:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>0 0(2) 0(3)
Line 263 ⟶ 406:
381920985378904469 10101001100110110110001110011011001110001101101100110010101(2) 2112200222001222121212221002220022112(3)</pre>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|3}}
No strings involved. Ternary numbers (only of odd length and with a 1 in the middle) are generated by permutating powers of 3<br/>
and then checked to see if they are palindromic in binary.<br/>
The first 6 numbers take about 1/10th of a second. The 7th number takes about 3 and a half minutes.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 350 ⟶ 493:
}
 
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 376 ⟶ 519:
Ternary: 22122022220102222022122
Binary: 1010100001100000100010000011000010101
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <algorithm>
#include <cstdint>
#include <iostream>
 
// Convert the given decimal number to the given number base
// and return it converted to a string
std::string to_base_string(const uint64_t& number, const uint32_t& base) {
uint64_t n = number;
if ( n == 0 ) {
return "0";
}
 
std::string result;
while ( n > 0 ) {
result += std::to_string(n % base);
n /= base;
}
std::reverse(result.begin(), result.end());
return result;
}
 
void display(const uint64_t& number) {
std::cout << "Decimal: " << number << std::endl;
std::cout << "Binary : " << to_base_string(number, 2) << std::endl;
std::cout << "Ternary: " << to_base_string(number, 3) << std::endl << std::endl;
}
 
bool is_palindromic(const std::string& number) {
std::string copy = number;
std::reverse(copy.begin(), copy.end());
return number == copy;
}
 
// Create a ternary palindrome whose left part is the ternary equivalent of the given number
// and return it converted to a decimal
uint64_t create_ternary_palindrome(const uint64_t& number) {
std::string ternary = to_base_string(number, 3);
uint64_t power_of_3 = 1;
uint64_t result = 0;
for ( uint64_t i = 0; i < ternary.length(); ++i ) { // Right part of palindrome is the mirror image of left part
if ( ternary[i] > '0' ) {
result += ( ternary[i] - '0' ) * power_of_3;
}
power_of_3 *= 3;
}
result += power_of_3; // Middle digit must be 1
power_of_3 *= 3;
result += number * power_of_3; // Left part is the given number multiplied by the appropriate power of 3
return result;
}
 
int main() {
std::cout << "The first 6 numbers which are palindromic in both binary and ternary are:" << std::endl;
display(0); // 0 is a palindrome in all 3 bases
display(1); // 1 is a palindrome in all 3 bases
 
uint64_t number = 1;
uint32_t count = 2;
do {
uint64_t ternary = create_ternary_palindrome(number);
if ( ternary % 2 == 1 ) { // Cannot be an even number since its binary equivalent would end in zero
std::string binary = to_base_string(ternary, 2);
if ( binary.length() % 2 == 1 ) { // Binary palindrome must have an odd number of digits
if ( is_palindromic(binary) ) {
display(ternary);
count++;
}
}
}
number++;
}
while ( count < 6 );
}
</syntaxhighlight>
{{ out }}
<pre>
The first 6 numbers which are palindromic in both binary and ternary are:
Decimal: 0
Binary : 0
Ternary: 0
 
Decimal: 1
Binary : 1
Ternary: 1
 
Decimal: 6643
Binary : 1100111110011
Ternary: 100010001
 
Decimal: 1422773
Binary : 101011011010110110101
Ternary: 2200021200022
 
Decimal: 5415589
Binary : 10100101010001010100101
Ternary: 101012010210101
 
Decimal: 90396755477
Binary : 1010100001100000100010000011000010101
Ternary: 22122022220102222022122
</pre>
 
=={{header|Common Lisp}}==
Unoptimized version
<langsyntaxhighlight lang="lisp">(defun palindromep (str)
(string-equal str (reverse str)) )
 
Line 391 ⟶ 639:
(palindromep (format nil "~3R" i)) )
(format t "n:~a~:* [2]:~B~:* [3]:~3R~%" i)
(incf results) ))</langsyntaxhighlight>
{{out}}
<pre>n:0 [2]:0 [3]:0
Line 403 ⟶ 651:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import core.stdc.stdio, std.ascii;
 
bool isPalindrome2(ulong n) pure nothrow @nogc @safe {
Line 461 ⟶ 709:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0 0(3) 0(2)
Line 469 ⟶ 717:
5415589 101012010210101(3) 10100101010001010100101(2)
90396755477 22122022220102222022122(3) 1010100001100000100010000011000010101(2)</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc ispalin2 n .
m = n
while m > 0
x = x * 2 + m mod 2
m = m div 2
.
if n = x
return 1
.
.
fastfunc reverse3 n .
while n > 0
r = r * 3 + n mod 3
n = n div 3
.
return r
.
func$ itoa n b .
if n > 0
return itoa (n div b) b & n mod b
.
.
proc main . .
print "0 0(2) 0(3)"
print "1 1(2) 1(3)"
pow3 = 3
while 1 = 1
for i = pow3 / 3 to pow3 - 1
# assumption that the middle digit must be 1
n = (i * 3 + 1) * pow3 + reverse3 i
if ispalin2 n = 1
print n & " " & itoa n 2 & "(2) " & itoa n 3 & "(3)"
cnt += 1
if cnt = 6 - 2
return
.
.
.
pow3 *= 3
.
.
main
</syntaxhighlight>
{{out}}
<pre>
0 0(2) 0(3)
1 1(2) 1(3)
6643 1100111110011(2) 100010001(3)
1422773 101011011010110110101(2) 2200021200022(3)
5415589 10100101010001010100101(2) 101012010210101(3)
90396755477 1010100001100000100010000011000010101(2) 22122022220102222022122(3)
</pre>
 
=={{header|Elixir}}==
{{trans|Ruby}}
{{works with|Elixir|1.3}}
<langsyntaxhighlight lang="elixir">defmodule Palindromic do
import Integer, only: [is_odd: 1]
 
Line 500 ⟶ 803:
end
 
Palindromic.task</langsyntaxhighlight>
{{out}}
<pre> decimal ternary binary
Line 511 ⟶ 814:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Find palindromic numbers in both binary and ternary bases. December 19th., 2018
let fG(n,g)=(Seq.unfold(fun(g,e)->if e<1L then None else Some((g%3L)*e,(g/3L,e/3L)))(n,g/3L)|>Seq.sum)+g+n*g*3L
Seq.concat[seq[0L;1L;2L];Seq.unfold(fun(i,e)->Some (fG(i,e),(i+1L,if i=e-1L then e*3L else e)))(1L,3L)]
|>Seq.filter(fun n->let n=System.Convert.ToString(n,2).ToCharArray() in n=Array.rev n)|>Seq.take 6|>Seq.iter (printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
Finding 6 takes no time.
Line 542 ⟶ 845:
=={{header|Factor}}==
This implementation uses the methods for reducing the search space discussed in the Ruby example.
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit formatting io kernel lists
lists.lazy literals math math.parser sequences tools.time ;
IN: rosetta-code.2-3-palindromes
Line 567 ⟶ 870:
] each ;
 
[ main ] time</langsyntaxhighlight>
{{out}}
<pre>The first 6 numbers which are palindromic in both binary and ternary:
Line 601 ⟶ 904:
and check if they are also binary palindromes using the optimizations which have been noted in some
of the other language solutions :
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
'converts decimal "n" to its ternary equivalent
Line 673 ⟶ 976:
Print " seconds on i3 @ 2.13 GHz"
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
{{out}}
<pre>The first 6 numbers which are palindromic in both binary and ternary are :
Line 706 ⟶ 1,009:
{{trans|C}}
On my modest machine (Intel Celeron @1.6ghz) this takes about 30 seconds to produce the 7th palindrome. Curiously, the C version (GCC 5.4.0, -O3) takes about 55 seconds on the same machine. As it's a faithful translation, I have no idea why.
<langsyntaxhighlight lang="go">package main
 
import (
Line 806 ⟶ 1,109:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 848 ⟶ 1,151:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.ListChar (transposedigitToInt, intToDigit, unwordsisDigit)
import NumericData.List (showIntAtBasetranspose, readIntunwords)
import Data.CharNumeric (intToDigitreadInt, isDigit, digitToIntshowIntAtBase)
 
---------- PALINDROMIC IN BOTH BINARY AND TERNARY --------
 
dualPalindromics :: [Integer]
dualPalindromics =
0 :
1 :
take
4
( filter
isBinPal
(readBase3 . base3Palindrome <$> [1 ..])
)
 
-- Member of ternary palindrome series.
base3Palindrome :: Integer -> String
base3Palindrome n =
let((<>) s<*> =(('1' :) . reverse)) . showBase 3 n
in s ++ ('1' : reverse s)
 
-- Test for binary palindrome.
isBinPal :: Integer -> Bool
isBinPal n =
( \s ->
let s = showIntAtBase 2 intToDigit n []
( \(q, r) = quotRem (length s) 2->
in (r == 1) && drop (succ q) s(1 == reverse (take q sr)
&& drop (succ q) s == reverse (take q s)
 
)
-- Integer value of ternary string.
$ quotRem (length s) 2
readBase3 :: String -> Integer
)
readBase3 = fst . head . readInt 3 isDigit digitToInt
$ showBase 2 n
 
showBase :: Integer -> Integer -> String
showBase base n = showIntAtBase base intToDigit n []
 
solutions :: [Integer]
solutions =
0 : 1 : take 4 (filter isBinPal ((readBase3 . base3Palindrome) <$> [1 ..]))
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
putStrLn
(unwords <$>unwords
<$> transpose
((fmap =<< flip justifyLeft( ' ' . succ . maximum .( fmap length) <$>
=<< flip justifyLeft ' '
transpose
(["Decimal", "Ternary", "Binary"] : . succ
fmap ((<*>) [show, showBase 3, showBase 2] . return) solutions)))maximum
. fmap length
)
<$> transpose
( ["Decimal", "Ternary", "Binary"] :
fmap
( (<*>) [show, showBase 3, showBase 2]
. return
)
dualPalindromics
)
)
)
where
justifyLeft n c s = take n (s ++<> replicate n c)</lang>
 
-------------------------- BASES -------------------------
 
readBase3 :: String -> Integer
readBase3 = fst . head . readInt 3 isDigit digitToInt
 
showBase :: Integer -> Integer -> String
showBase base n = showIntAtBase base intToDigit n []</syntaxhighlight>
{{Out}}
<pre>Decimal Ternary Binary
Line 899 ⟶ 1,227:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">isPalin=: -: |. NB. check if palindrome
toBase=: #.inv"0 NB. convert to base(s) in left arg
filterPalinBase=: ] #~ isPalin@toBase/ NB. palindromes for base(s)
Line 919 ⟶ 1,247:
end.
y{.res
)</langsyntaxhighlight>
'''Usage:'''
<langsyntaxhighlight lang="j"> find23Palindromes i. 2e6 NB. binary & ternary palindromes less than 2,000,000
0 1 6643 1422773
10 2 3 showBases find23Palindromes getfirst 6 NB. first 6 binary & ternary palindomes
Line 929 ⟶ 1,257:
1422773 101011011010110110101 2200021200022
5415589 10100101010001010100101 101012010210101
90396755477 1010100001100000100010000011000010101 22122022220102222022122</langsyntaxhighlight>
 
=={{header|Java}}==
This takes a while to get to the 6th one (I didn't time it precisely, but it was less than 2 hours on an i7)
<langsyntaxhighlight lang="java">public class Pali23 {
public static boolean isPali(String x){
return x.equals(new StringBuilder(x).reverse().toString());
Line 950 ⟶ 1,278:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0, 0, 0
Line 958 ⟶ 1,286:
5415589, 10100101010001010100101, 101012010210101
90396755477, 1010100001100000100010000011000010101, 22122022220102222022122</pre>
Alternatively, using a simple and efficient algorithm, the first six number are found in less than a second.
<syntaxhighlight lang="java">
 
public final class FindPalindromicNumbersBases23 {
 
public static void main(String[] aArgs) {
System.out.println("The first 7 numbers which are palindromic in both binary and ternary are:");
display(0); // 0 is a palindrome in all 3 bases
display(1); // 1 is a palindrome in all 3 bases
long number = 1;
int count = 2;
do {
long ternary = createTernaryPalindrome(number);
if ( ternary % 2 == 1 ) { // Cannot be an even number since its binary equivalent would end in zero
String binary = toBinaryString(ternary);
if ( binary.length() % 2 == 1 ) { // Binary palindrome must have an odd number of digits
if ( isPalindromic(binary) ) {
display(ternary);
count++;
}
}
}
number++;
}
while ( count < 7 );
}
// Create a ternary palindrome whose left part is the ternary equivalent of the given number
// and return its decimal equivalent
private static long createTernaryPalindrome(long aNumber) {
String ternary = toTernaryString(aNumber);
long powerOf3 = 1;
long sum = 0;
for ( int i = 0; i < ternary.length(); i++ ) { // Right part of a palindrome is the mirror image of left part
if ( ternary.charAt(i) > '0' ) {
sum += ( ternary.charAt(i) - '0' ) * powerOf3;
}
powerOf3 *= 3;
}
sum += powerOf3; // Middle digit must be 1
powerOf3 *= 3;
sum += aNumber * powerOf3; // Left part is the given number multiplied by the appropriate power of 3
return sum;
}
private static boolean isPalindromic(String aNumber) {
return aNumber.equals( new StringBuilder(aNumber).reverse().toString() );
}
private static String toTernaryString(long aNumber) {
if ( aNumber == 0 ) {
return "0";
}
StringBuilder result = new StringBuilder();
while ( aNumber > 0 ) {
result.append(aNumber % 3);
aNumber /= 3;
}
return result.reverse().toString();
}
private static String toBinaryString(long aNumber) {
return Long.toBinaryString(aNumber);
}
private static void display(long aNumber) {
System.out.println("Decimal: " + aNumber);
System.out.println("Binary : " + toBinaryString(aNumber));
System.out.println("Ternary: " + toTernaryString(aNumber));
System.out.println();
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The first 7 numbers which are palindromic in both binary and ternary are:
Decimal: 0
Binary : 0
Ternary: 0
 
Decimal: 1
Binary : 1
Ternary: 1
 
Decimal: 6643
Binary : 1100111110011
Ternary: 100010001
 
Decimal: 1422773
Binary : 101011011010110110101
Ternary: 2200021200022
 
Decimal: 5415589
Binary : 10100101010001010100101
Ternary: 101012010210101
 
Decimal: 90396755477
Binary : 1010100001100000100010000011000010101
Ternary: 22122022220102222022122
 
Decimal: 381920985378904469
Binary : 10101001100110110110001110011011001110001101101100110010101
Ternary: 2112200222001222121212221002220022112
</pre>
 
=={{header|JavaScript}}==
===ES6===
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,086 ⟶ 1,521:
)))
.map(unwords));
})();</langsyntaxhighlight>
{{Out}}
<pre>Decimal Ternary Binary
Line 1,095 ⟶ 1,530:
5415589 101012010210101 10100101010001010100101
90396755477 22122022220102222022122 1010100001100000100010000011000010101 </pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
The C (jq) and Go (gojq) implementations of jq produce correct results for the first 6 numbers,
as shown below.
jq's "number" type lacks the integer arithmetic precision required to compute the next number
in the sequence, and gojq's memory management is not up to the task even on a generously endowed machine.
 
'''Generic Utilities'''
<syntaxhighlight lang=jq>
# 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;
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
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)
</syntaxhighlight>
<pre>
The first 6 numbers which are palindromic in both binary and ternary are:
Decimal : 0
Binary : 0
Ternary : 0
 
Decimal : 1
Binary : 1
Ternary : 1
 
Decimal : 6643
Binary : 1100111110011
Ternary : 100010001
 
Decimal : 1422773
Binary : 101011011010110110101
Ternary : 2200021200022
 
Decimal : 5415589
Binary : 10100101010001010100101
Ternary : 101012010210101
 
Decimal : 90396755477
Binary : 1010100001100000100010000011000010101
Ternary : 22122022220102222022122
</pre>
 
=={{header|Julia}}==
{{trans|C}}
<langsyntaxhighlight lang="julia">ispalindrome(n, bas) = (s = string(n, base=bas); s == reverse(s))
prin3online(n) = println(lpad(n, 15), lpad(string(n, base=2), 40), lpad(string(n, base=3), 30))
reversebase3(n) = (x = 0; while n != 0 x = 3x + (n %3); n = div(n, 3); end; x)
Line 1,147 ⟶ 1,704:
 
printpalindromes(6)
</langsyntaxhighlight>{{out}}
<pre>
Number Base 2 Base 3
Line 1,160 ⟶ 1,717:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
/** converts decimal 'n' to its ternary equivalent */
Line 1,231 ⟶ 1,788:
}
while (count < 6)
}</langsyntaxhighlight>
{{out}}
<pre>The first 6 numbers which are palindromic in both binary and ternary are:
Line 1,259 ⟶ 1,816:
Ternary : 22122022220102222022122</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">palindromify3[n_] :=
Block[{digits},
If[Divisible[n, 3], {},
Line 1,269 ⟶ 1,826:
];
base2PalindromeQ[n_] := IntegerDigits[n, 2] === Reverse[IntegerDigits[n, 2]];
Select[Flatten[palindromify3 /@ Range[1000000]], base2PalindromeQ]</syntaxhighlight>
 
Select[Flatten[palindromify3 /@ Range[1000000]], base2PalindromeQ]</lang>
 
{{out}}
Line 1,277 ⟶ 1,833:
=={{header|Nim}}==
{{trans|Ada}}
<langsyntaxhighlight Nimlang="nim">import bitops, strformat, times
 
#---------------------------------------------------------------------------------------------------
Line 1,353 ⟶ 1,909:
let t0 = cpuTime()
findPal23()
echo fmt"\nTime: {cpuTime() - t0:.2f}s"</langsyntaxhighlight>
 
{{out}}
Line 1,367 ⟶ 1,923:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">check(n)={ \\ Check for 2n+1-digit palindromes in base 3
my(N=3^n);
forstep(i=N+1,2*N,[1,2],
Line 1,377 ⟶ 1,933:
)
};
print1("0, 1"); for(i=1,11,check(i))</langsyntaxhighlight>
{{out}}
<pre>0, 1, 6643, 1422773, 5415589, 90396755477</pre>
Line 1,383 ⟶ 1,939:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/fromdigits todigitstring/;
 
print "0 0 0\n"; # Hard code the 0 result
Line 1,394 ⟶ 1,950:
# Print results (including base 10) if base-2 palindrome
print fromdigits($b2,2)," $b3 $b2\n" if $b2 eq reverse($b2);
}</langsyntaxhighlight>
{{out}}
<pre>0 0 0
Line 1,412 ⟶ 1,968:
that turned out noticeably slower.
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- widths and limits for 32/64 bit running (see output below):
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
constant {dsize,w3,w2,limit} = iff(machine_bits()=32?{12,23,37,6}
<span style="color: #000080;font-style:italic;">-- widths and limits for 32/64 bit running (see output below):</span>
:{18,37,59,7}),
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dsize</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">37</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}</span>
-- [atoms on 32-bit have only 53 bits of precision, but 7th ^^^^ requires 59]
<span style="color: #0000FF;">:{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">37</span><span style="color: #0000FF;">,</span><span style="color: #000000;">59</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}),</span>
dfmt = sprintf("%%%dd",dsize), -- ie "%12d" or "%18d"
<span style="color: #000080;font-style:italic;">-- [atoms on 32-bit have only 53 bits of precision, but 7th ^^^^ requires 59]</span>
esc = #1B
<span style="color: #000000;">dfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%%dd"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dsize</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- ie "%12d" or "%18d"</span>
 
<span style="color: #000000;">esc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#1B</span>
function center(string s, integer l)
l = max(0,floor((l-length(s))/2))
<span style="color: #008080;">function</span> <span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
string pad = repeat(' ',l)
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
s = pad & s & pad
<span style="color: #004080;">string</span> <span style="color: #000000;">space</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
return s
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">space</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">space</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
integer count = 1
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
procedure show(atom n, string p2, p3)
if count=1 then
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span>
printf(1," %s %s %s\n",{pad_head("decimal",dsize),center("ternary",w3),center(" binary",w2)})
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s %s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"decimal"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dsize</span><span style="color: #0000FF;">),</span><span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"ternary"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w3</span><span style="color: #0000FF;">),</span><span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" binary"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">)})</span>
string ns = sprintf(dfmt,n)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"%2d: %s %s %s\n",{count, ns, center(p3,w3), center(p2,w2)})
<span style="color: #004080;">string</span> <span style="color: #000000;">ns</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
count += 1
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %s %s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ns</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w3</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">)})</span>
end procedure
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
procedure progress(string e, p2, p3)
e = pad_head(e,dsize)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">progress64</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span>
printf(1,"--: %s %s %s\r",{e, center(p3,w3), center(p2,w2)})
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dsize</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"--: %s %s %s\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w3</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">)})</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function to_base(atom i, integer base)
string s = ""
<span style="color: #008080;">function</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
while i>0 do
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
s = append(s,remainder(i,base)+'0')
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
i = floor(i/base)
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
s = reverse(s)
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
if s="" then
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
s = "0"
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span>
return s
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function from_base(string s, integer base)
atom res = 0
<span style="color: #008080;">function</span> <span style="color: #000000;">from_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
res = res*base+s[i]-'0'
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">*</span><span style="color: #000000;">base</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function sn(string s, integer f, base)
-- helper function, return s mirrored (if f!=0)
<span style="color: #008080;">function</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
-- and as a (decimal) number (if base!=0)
<span style="color: #000080;font-style:italic;">-- helper function, return s mirrored (if f!=0)
-- all returns from next_palindrome() get fed through here.
-- and as a (decimal) number (if base!=0)
if f then
-- all returns from next_palindrome() get fed through here.</span>
s[f+2..$] = reverse(s[1..f])
<span style="color: #008080;">if</span> <span style="color: #000000;">f</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">f</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">f</span><span style="color: #0000FF;">])</span>
atom n = iff(base?from_base(s,base):0)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return {s,n}
<span style="color: #004080;">atom</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">?</span><span style="color: #000000;">from_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">):</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function next_palindrome(integer base, object s)
--
<span style="color: #008080;">function</span> <span style="color: #000000;">next_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
-- base is 2 or 3
<span style="color: #000080;font-style:italic;">--
-- s is not usually a palindrome, but derived from one in <5-base>
-- base is 2 or 3
--
-- s is not usually a palindrome, but derived from one in &lt;5-base&gt;
-- all done with very obvious string manipulations, plus a few
--
-- less obvious optimisations (odd length, middle 1 in base 3).
-- all done with very obvious string manipulations, plus a few
--
-- less obvious optimisations (odd length, middle 1 in base 3).
-- example: next_palindrome(2,"10001000100") -> "10001010001"
--
-- example: next_palindrome(2,"10001000100") -&gt; "10001010001"
if not string(s) then s = to_base(s,base) end if
--</span>
integer l = length(s),
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
f = floor(l/2),
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span>
m = f+1, c
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
if mod(l,2) then -- optimisation: palindromes must be odd-length
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span>
-- 1) is a plain mirror greater? (as in the example just given)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- optimisation: palindromes must be odd-length
{string r} = sn(s,f,0)
-- optimisation:1) baseis 3a palindromesplain havemirror '1'greater? (as in the middleexample just given)</span>
<span style="color: #0000FF;">{</span><span style="color: #004080;">string</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
if base=3 and r[m]!='1' then r[m] = '1' end if
<span style="color: #000080;font-style:italic;">-- optimisation: base 3 palindromes have '1' in the middle</span>
if r>s then return sn(r,0,base) end if
<span style="color: #008080;">if</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">and</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'1'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
-- 2) can we (just) increment the middle digit?
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">></span><span style="color: #000000;">s</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
c = s[m]-'0'+1
<span style="color: #000080;font-style:italic;">-- 2) can we (just) increment the middle digit?</span>
if base=2 or c=1 then
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
if c<base then
<span style="color: #008080;">if</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">or</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
s[m] = c+'0'
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><</span><span style="color: #000000;">base</span> <span style="color: #008080;">then</span>
return sn(s,f,base)
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
s[m] = '0'
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
elsif base=3 then
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'0'</span>
s[m] = '1'
<span style="color: #008080;">elsif</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
-- 3) can we increment left half (or is it all <base-1>s?)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=f to 1 by -1 do
<span style="color: #000080;font-style:italic;">-- 3) can we increment left half (or is it all &lt;base-1&gt;s?)</span>
if s[i]<base-1+'0' then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">f</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
s[i] += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">base</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
return sn(s,f,base)
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
else
<span style="color: #008080;">return</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
s[i] = '0'
end if<span style="color: #008080;">else</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'0'</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
l += 2 -- (stay odd)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
else
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span> <span style="color: #000080;font-style:italic;">-- (stay odd)</span>
l += 1 -- (even->odd)
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (even-&gt;odd)</span>
-- 4) well then, next palindrome is longer, 1000..0001-style
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
s = sprintf("1%s1",{repeat('0',l-2)})
<span style="color: #000080;font-style:italic;">-- 4) well then, next palindrome is longer, 1000..0001-style</span>
-- optimisation: base 3 palindromes have '1' in the middle
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1%s1"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span>
if base=3 then
<span style="color: #000080;font-style:italic;">-- optimisation: base 3 palindromes have '1' in the middle</span>
m = (l+1)/2
<span style="color: #008080;">if</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span>
s[m] = '1'
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
end if
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
return sn(s,0,base)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">sn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
string p2 = "0", p3 = "0" -- palindromes as strings in base 2 and 3
atom n2 = 0, n3 = 0, -- decimal equivalents of the above.
<span style="color: #004080;">string</span> <span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span> <span style="color: #000080;font-style:italic;">-- palindromes as strings in base 2 and 3</span>
t0 = time(),
<span style="color: #004080;">atom</span> <span style="color: #000000;">n2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- decimal equivalents of the above.</span>
t1 = time()+1
<span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span>
 
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
while count<=limit do
if n2=n3 then
<span style="color: #008080;">while</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
show(n2,p2,p3)
<span style="color: #008080;">if</span> <span style="color: #000000;">n2</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n3</span> <span style="color: #008080;">then</span>
{p2,n2} = next_palindrome(2,p2)
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span>
{p3,n3} = next_palindrome(3,p3)
<span style="color: #0000FF;">{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span>
elsif n2<n3 then
<span style="color: #0000FF;">{</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span>
{p2,n2} = next_palindrome(2,n3-1)
<span style="color: #008080;">elsif</span> <span style="color: #000000;">n2</span><span style="color: #0000FF;"><</span><span style="color: #000000;">n3</span> <span style="color: #008080;">then</span>
elsif n2>n3 then
<span style="color: #0000FF;">{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
{p3,n3} = next_palindrome(3,n2-1)
<span style="color: #008080;">elsif</span> <span style="color: #000000;">n2</span><span style="color: #0000FF;">></span><span style="color: #000000;">n3</span> <span style="color: #008080;">then</span>
end if
<span style="color: #0000FF;">{</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
if time()>t1 then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
progress(elapsed_short(time()-t0),p2,p3)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
t1 = time()+1
<span style="color: #000000;">progress64</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">elapsed_short</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span>
if find(get_key(),{'q','Q',esc}) then exit end if
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_key</span><span style="color: #0000FF;">(),{</span><span style="color: #008000;">'q'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'Q'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">esc</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
?elapsed(time()-t0)</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
32 bit:
Line 1,579 ⟶ 2,138:
=== much simpler version ===
(slightly but not alot faster)
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function to_base(atom n, integer base)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
string result = ""
<span style="color: #008080;">function</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
while true do
<span style="color: #004080;">string</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
result &= remainder(n,base)
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
n = floor(n/base)
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
if n=0 then exit end if
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return result
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure show(integer count, atom n)
string n2 = sq_add('0',to_base(n,2)),
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
n3 = sq_add('0',to_base(n,3)),
<span style="color: #004080;">string</span> <span style="color: #000000;">n2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)),</span>
p2 = repeat(' ',(37-length(n2))/2),
<span style="color: #000000;">n3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)),</span>
p3 = repeat(' ',(23-length(n3))/2)
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,(</span><span style="color: #000000;">37</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
printf(1,"%2d: %12d %s%s%s %s%s\n",{count, n, p3,n3,p3, p2,n2})
<span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,(</span><span style="color: #000000;">23</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %12d %s%s%s %s%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">})</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function createpalindrome3(integer n)
atom tot = 0, power3 = 1
<span style="color: #008080;">function</span> <span style="color: #000000;">createpalindrome3</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
string ternary = to_base(n,3)
<span style="color: #004080;">atom</span> <span style="color: #000000;">tot</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">power3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
for i=length(ternary) to 1 by -1 do
<span style="color: #004080;">string</span> <span style="color: #000000;">ternary</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
tot += ternary[i] * power3
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ternary</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
power3 *= 3
<span style="color: #000000;">tot</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">ternary</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">power3</span>
end for
<span style="color: #000000;">power3</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">3</span>
return tot + power3 + n*power3*3
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">tot</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">power3</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">power3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
atom t0 = time()
printf(1,"%16s %15s %30s\n",{"decimal","ternary","binary"})
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
show(0,0)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%16s %15s %30s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"decimal"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ternary"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"binary"</span><span style="color: #0000FF;">})</span>
show(1,1)
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
integer count = 2, n = 1
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
while count<6 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
atom n3 = createpalindrome3(n)
<span style="color: #008080;">while</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">6</span> <span style="color: #008080;">do</span>
if remainder(n3,2) then
<span style="color: #004080;">atom</span> <span style="color: #000000;">n3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">createpalindrome3</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
string n2 = to_base(n3,2)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if n2[$]=1 and n2=reverse(n2) then
<span style="color: #004080;">string</span> <span style="color: #000000;">n2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
show(count,n3)
<span style="color: #008080;">if</span> <span style="color: #000000;">n2</span><span style="color: #0000FF;">[$]=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">n2</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
count += 1
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
n += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
?elapsed(time()-t0)</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,638 ⟶ 2,200:
=== much faster version ===
Inspired by Scala 😏
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function to_base(string s, integer base)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
-- convert decimal string s to specified base
<span style="color: #008080;">function</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
string res = ""
<span style="color: #000080;font-style:italic;">-- convert decimal string s to specified base</span>
while length(s) do
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #000000;">base</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (&gt;9 as below)</span>
integer q, r = 0
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
for i=1 to length(s) do
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
q = r*10+s[i]-'0'
<span style="color: #004080;">integer</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
s[i] = floor(q/base)+'0'
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
r = mod(q,base)
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span>
end for
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">/</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)+</span><span style="color: #008000;">'0'</span>
res &= r+'0'
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
while length(s) and s[1]='0' do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
s = s[2..$]
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span> <span style="color: #000080;font-style:italic;">-- +(r&gt;9)*('A'-'9'-1)</span>
end while
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return res
<span style="color: #000080;font-style:italic;">-- res = reverse(res) -- (if not palindromic!)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure center(string s, integer l)
l = max(0,floor((l-length(s))/2))
<span style="color: #008080;">constant</span> <span style="color: #000000;">A</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"6643"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1422773"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"5415589"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"90396755477"</span><span style="color: #0000FF;">,</span>
string pad = repeat(' ',l)
<span style="color: #008000;">"381920985378904469"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1922624336133018996235"</span><span style="color: #0000FF;">,</span>
puts(1,pad & s & pad & "\n")
<span style="color: #008000;">"2004595370006815987563563"</span><span style="color: #0000FF;">,</span>
end procedure
<span style="color: #008000;">"8022581057533823761829436662099"</span><span style="color: #0000FF;">,</span>
 
<span style="color: #008000;">"392629621582222667733213907054116073"</span><span style="color: #0000FF;">,</span>
constant A = {"0","1","6643","1422773","5415589","90396755477",
<span style="color: #008000;">"32456836304775204439912231201966254787"</span><span style="color: #0000FF;">,</span>
"381920985378904469","1922624336133018996235",
<span style="color: #008000;">"428027336071597254024922793107218595973"</span><span style="color: #0000FF;">,</span>
"2004595370006815987563563",
<span style="color: #008000;">"1597863243206403857787246920544522912361"</span><span style="color: #0000FF;">,</span>
"8022581057533823761829436662099",
<span style="color: #008000;">"30412638162199251273509758127730300026189"</span><span style="color: #0000FF;">,</span>
"392629621582222667733213907054116073",
<span style="color: #008000;">"32345684491703244980406880704479906642045"</span><span style="color: #0000FF;">,</span>
"32456836304775204439912231201966254787",
<span style="color: #008000;">"24014998963383302600955162866787153652444049"</span><span style="color: #0000FF;">}</span>
"428027336071597254024922793107218595973",
"1597863243206403857787246920544522912361",
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">A</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
"30412638162199251273509758127730300026189",
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%=145s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
"32345684491703244980406880704479906642045",
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%=145s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">))</span>
"24014998963383302600955162866787153652444049"}
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<!--</syntaxhighlight>-->
for i=1 to length(A) do
center(to_base(A[i],2),145)
center(to_base(A[i],3),145)
end for</lang>
{{out}}
<pre style="font-size: 11px">
<pre>
0
0
Line 1,715 ⟶ 2,274:
2202021211210100110100002202101000110000220121210220000110001012022000010110010121121202022
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">
import sat.
to_num(List, Base, Num) =>
Len = length(List),
Num #= sum([List[I] * Base**(Len-I) : I in 1..Len]).
 
palindrom(S) =>
N = len(S),
Start :: 1..N, % start at the first non-zero position:
foreach(I in 1..N)
I1 #= max(1, min(N, N-(I-Start))), % I1 is the symmetry index partner of I (if relevant)
element(I1, S, S1), % S1 is the respective digit
I #< Start #=> S[I] #= 0, % skip leading 0´s
I #= Start #=> S[I] #> 0, % Start points to the first non-zero digit
I #>= Start #=> S[I] #= S1 % palindromic symmetry
end.
 
constrain(Max, B, X) =>
Len = floor(log(Max) / log(B)) + 1, % length of Max in Base B representation
Digits = new_list(Len), Digits :: 0..B-1,
to_num(Digits, B, X), % Digits show the Base B representation of X
palindrom(Digits).
 
main =>
N = 11, % maximum number of decimal digits for search, can be set freely
Max = 10**N - 1, % maximum number
X :: 2..Max,
constrain(Max, 2, X),
constrain(Max, 3, X),
Pnumbers = solve_all([X]),
foreach([Y] in [[0], [1]] ++ Pnumbers.sort()) % start with 0 and 1, then show solutions > 1
printf("%w %s %s%n", Y, to_radix_string(Y,2), to_radix_string(Y,3))
end.
</syntaxhighlight>
Output:
<pre>0 0 0
1 1 1
6643 1100111110011 100010001
1422773 101011011010110110101 2200021200022
5415589 10100101010001010100101 101012010210101
90396755477 1010100001100000100010000011000010101 22122022220102222022122</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de ternary (N)
(if (=0 N)
(cons N)
Line 1,734 ⟶ 2,336:
(println N (pack B2) (pack B3))
(inc 'I) )
(inc 'N) ) )</langsyntaxhighlight>
{{out}}
<pre>0 "0" "0"
Line 1,745 ⟶ 2,347:
=={{header|Python}}==
===Imperative===
<langsyntaxhighlight lang="python">from itertools import islice
 
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
Line 1,778 ⟶ 2,380:
 
for pal23 in islice(pal_23(), 6):
print(pal23, baseN(pal23, 3), baseN(pal23, 2))</langsyntaxhighlight>
{{out}}
<pre>0 0 0
Line 1,789 ⟶ 2,391:
===Functional===
{{Works with|Python|3.7}}
<langsyntaxhighlight Pythonlang="python">'''Numbers with palindromic digit strings in both binary and ternary'''
 
from itertools import (islice)
Line 1,797 ⟶ 2,399:
def palinBoth():
'''Non finite stream of dually palindromic integers.'''
yield (0, '0', '0')
ibt = (1, '1', '1')
 
yield ibt
while True:
ibt = until(isBoth)(psucc)(psucc(ibt))
yield (int(ibt[2], 3), ibt[1], ibt[2])
 
 
Line 1,823 ⟶ 2,425:
s = showBase3(d)
pal = s + '1' + s[::-1]
return (d, bin(int(pal, 3))[2:], pal)
 
 
Line 1,834 ⟶ 2,436:
 
 
# TEST --------------------------- TEST -------------------------
def main():
'''Integers with palindromic digits in both binary and ternary'''
both binary and ternary bases.
'''
 
xs = take(6)(palinBoth())
Line 1,857 ⟶ 2,461:
 
 
# GENERIC ------------------------- GENERIC ------------------------
 
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 1,903 ⟶ 2,507:
or xs itself if n > length xs.
'''
return lambdadef go(xs): (
xs[0:n]return (
if isinstance(xs, (list, tuple)) xs[0:n]
else list(islice if isinstance(xs, n(list, tuple))
else list(islice(xs, n))
)
)
return go
 
 
Line 1,915 ⟶ 2,521:
The initial seed value is x.
'''
def go(f, x):
v =def g(x):
while not p( v): = x
vwhile =not fp(v):
return v = f(v)
return lambda f: lambda x: go(f, x) return v
return g
return go
 
 
# FORMATTING ------------------------ FORMATTING ----------------------
 
# label :: Method String -> (String, Int)
Line 1,934 ⟶ 2,542:
s, w = sw
return getattr(s, k)(w, ' ') + ' '
return lambda sw: go(sw)
 
 
Line 1,957 ⟶ 2,565:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> Decimal Binary Ternary
Line 1,969 ⟶ 2,577:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require racket/generator)
 
Line 2,055 ⟶ 2,663:
(map (curryr number->string 2) (for/list ((i 16) (p (in-producer (b-palindromes-generator 2)))) p))
(list "0" "1" "11" "101" "111" "1001" "1111" "10001" "10101" "11011"
"11111" "100001" "101101" "110011" "111111" "1000001")))</langsyntaxhighlight>
{{out}}
<pre> 1: 0_10 0_3 0_2
Line 2,068 ⟶ 2,676:
Instead of searching for numbers that are palindromes in one base then checking the other, generate palindromic trinary numbers directly, then check to see if they are also binary palindromes (with additional simplifying constraints as noted in other entries). Outputs the list in decimal, binary and trinary.
 
<syntaxhighlight lang="raku" perl6line>constant palindromes = 0, 1, |gather for 1 .. * -> $p {
my $pal = $p.base(3);
my $n = :3($pal ~ '1' ~ $pal.flip);
Line 2,078 ⟶ 2,686:
}
 
printf "%d, %s, %s\n", $_, .base(2), .base(3) for palindromes[^6];</langsyntaxhighlight>
{{out}}
<pre>0, 0, 0
Line 2,104 ⟶ 2,712:
::* &nbsp; convert the decimal numbers to base 3,
::* &nbsp; ensure that the numbers in base 3 are palindromic.
<langsyntaxhighlight lang="rexx">/*REXX program finds numbers that are palindromic in both binary and ternary. */
digs=50; numeric digits digs /*biggest known B2B3 palindrome: 44 dig*/
parse arg maxHits .; if maxHits=='' then maxHits=6 /*use six as a limit.*/
Line 2,133 ⟶ 2,741:
if hits>2 then if hits//2 then #=#'0'
if hits<maxHits then return /*Not enough palindromes? Keep looking*/
exit /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 7 </tt>}}
<pre> [1] 0 (decimal), ternary= 0
Line 2,159 ⟶ 2,767:
===version 2===
This REXX version takes advantage that the palindromic numbers (in both binary and ternary bases) &nbsp; ''seem'' &nbsp; to only have a modulus nine residue of &nbsp; 1, 5, 7, or 8. &nbsp; With this assumption, the following REXX program is about 25% faster.
<langsyntaxhighlight lang="rexx">/*REXX program finds numbers that are palindromic in both binary and ternary. */
digs=50; numeric digits digs /*biggest known B2B3 palindrome: 44 dig*/
parse arg maxHits .; if maxHits=='' then maxHits=6 /*use six as a limit.*/
Line 2,189 ⟶ 2,797:
if hits>2 then if hits//2 then #=#'0'
if hits<maxHits then return /*Not enough palindromes? Keep looking*/
exit /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project: Find palindromic numbers in both binary and ternary bases
 
Line 2,241 ⟶ 2,849:
return 0
ok
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,266 ⟶ 2,874:
This program constructs base 3 palindromes using the above "rules" and checks if they happen to be binary palindromes.
 
<langsyntaxhighlight lang="ruby">pal23 = Enumerator.new do |y|
y << 0
y << 1
Line 2,281 ⟶ 2,889:
n = pal23.next
puts "%2d: %12d %s %s" % [i, n, n.to_s(3).center(25), n.to_s(2).center(39)]
end</langsyntaxhighlight>
{{out}}
<pre> decimal ternary binary
Line 2,294 ⟶ 2,902:
===Functional programmed, (tail) recursive===
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/ZYCqm7p/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/WIL3oAwYSRy4Kl918u13CA Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
import scala.compat.Platform.currentTime
 
Line 2,331 ⟶ 2,939:
println(s"Successfully completed without errors. [total ${currentTime - executionStartTime} ms]")
 
}</langsyntaxhighlight>
 
===Fastest and high yields (17) solution 😏===
{{Out}}Best seen running in your browser either by [https://scastie.scala-lang.org/en0ZiqDETCuWO6avhTi9YQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import scala.io.Source
 
object FastPalindrome23 extends App {
Line 2,357 ⟶ 2,965:
println(s"${count} palindromes found.")
 
}</langsyntaxhighlight>
{{Out}}
<pre>Decimal : 0 , Central binary digit: 0
Line 2,447 ⟶ 3,055:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(import (scheme base)
(scheme write)
(srfi 1 lists)) ; use 'fold' from SRFI 1
Line 2,498 ⟶ 3,106:
(r-number->string n 3)))
(newline))
(get-series 6))</langsyntaxhighlight>
 
{{out}}
Line 2,510 ⟶ 3,118:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var format = "%11s %24s %38s\n"
format.printf("decimal", "ternary", "binary")
format.printf(0, 0, 0)
Line 2,521 ⟶ 3,129:
format.printf(Num(b2, 2), b3, b2)
}
}</langsyntaxhighlight>
{{out}}
<pre> decimal ternary binary
Line 2,535 ⟶ 3,143:
{{trans|C}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
func isPalin2(n: Int) -> Bool {
Line 2,636 ⟶ 3,244:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,650 ⟶ 3,258:
=={{header|Tcl}}==
We can use <tt>[format %b]</tt> to format a number as binary, but ternary requires a custom proc:
<langsyntaxhighlight Tcllang="tcl">proc format_%t {n} {
while {$n} {
append r [expr {$n % 3}]
Line 2,657 ⟶ 3,265:
if {![info exists r]} {set r 0}
string reverse $r
}</langsyntaxhighlight>
 
Identifying palindromes is simple. This form is O(n) with a large constant factor, but good enough:
 
<langsyntaxhighlight Tcllang="tcl">proc pal? {s} {expr {$s eq [string reverse $s]}}</langsyntaxhighlight>
 
The naive approach turns out to be very slow:
<langsyntaxhighlight Tcllang="tcl">proc task {{find 6}} {
for {set i 0} {$find} {incr i} {
set b [format %b $i]
Line 2,675 ⟶ 3,283:
}
 
puts [time {task 4}]</langsyntaxhighlight>
{{out}}
<pre>Palindrome: 0 (0) (0)
Line 2,686 ⟶ 3,294:
We can do much better than that by naively iterating the binary palindromes. This is nice to do in a coroutine:
 
<langsyntaxhighlight Tcllang="tcl">package require Tcl 8.5 ;# for coroutines
 
proc 2pals {} {
Line 2,699 ⟶ 3,307:
yield ${a}1$b
}
}</langsyntaxhighlight>
 
The binary strings emitted by this generator are not in increasing order, but for this particular task, that turns out to be unimportant.
 
Our main loop needs only minor changes:
<langsyntaxhighlight Tcllang="tcl">proc task {{find 6}} {
coroutine gen apply {{} {yield; 2pals}}
while {$find} {
Line 2,718 ⟶ 3,326:
}
 
puts [time task]</langsyntaxhighlight>
This version finds the first 6 in under 4 seconds, which is good enough for the task at hand:
{{out}}
Line 2,732 ⟶ 3,340:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
'palindromes both in base3 and base2
'using Decimal data type to find number 6 and 7, although slowly
Line 2,865 ⟶ 3,473:
Debug.Print "Completed in"; (Time3 - Time1) / 1000; "seconds"
Application.ScreenUpdating = True
End Sub</langsyntaxhighlight>{{out}}<pre>' iter decimal binary ternary
' 0 0 0 0
' 0 1 1 1
Line 2,881 ⟶ 3,489:
{{libheader|Wren-fmt}}
Just the first 6 palindromes as the 7th is too large for Wren to process without resorting to BigInts.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var isPalindrome2 = Fn.new { |n|
Line 2,954 ⟶ 3,562:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,994 ⟶ 3,602:
{{trans|Ruby}}
VERY slow after six but does find it.
<langsyntaxhighlight lang="zkl">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..*]){
Line 3,006 ⟶ 3,614:
}
}.fp(Ref(3),Ref(3))).push(T(1,0),T(2,1)) // seed with first two results
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach idx,n in (pal23W().walk(6)){
println("%2d: %,d == %.3B(3) == %.2B(2)".fmt(idx,n,n,n))
}</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits