Find palindromic numbers in both binary and ternary bases

From Rosetta Code
Revision as of 02:28, 27 March 2014 by Thundergnat (talk | contribs) (→‎{{header|Perl 6}}: Added Perl 6 implementation)
Find palindromic numbers in both binary and ternary bases is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
The task

Find and show (in decimal) the first six numbers (non-negative integers) that are palindromes in both base 2 and base 3.

Use zero (0) as the first number found, even though some other definitions ignore it.

Optionally, show the decimal number found in it's binary and ternary form.

It's permissible to assume the first two numbers and simply list them.

See also


Perl 6

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. Outputs the list in decimal, binary and trinary.

Note: finding the first six takes a depressingly long time with this implementation. Seven would be unbearable. :-(

<lang perl6>my @palindromes := 0, 1, gather for 1 .. * -> $n {

    my $p = $n.base(3);
    my $q = $p.flip;
    for "$p$q", "{$p}0$q", "{$p}1$q", "{$p}2$q" -> $r { 
       my $s = :3($r);
       next if $s %% 2;
       $s = $s.base(2);
       next unless $s eq $s.flip;
       take :2($s);
    }

}

printf "%d, %s, %s\n", $_, $_.base(2), $_.base(3) for @palindromes[^6]</lang>

Output:
0, 0, 0
1, 1, 1
6643, 1100111110011, 100010001
1422773, 101011011010110110101, 2200021200022
5415589, 10100101010001010100101, 101012010210101
90396755477, 1010100001100000100010000011000010101, 22122022220102222022122

REXX

Programming note:   This version of the REXX program doesn't stop, just in case it found another number.

For this REXX program, a few deterministic assumptions were made:

  • for the requirement of binary palindromes, the numbers have to be odd.
  • for the requirement of binary palindromes, the number of binary digits have to be odd.
  • for the requirement of ternary palindromes, the numbers can't end in zero (in base 3).

<lang rexx>/*REXX pgm finds decimal #s that are palindromic in binary and ternary.*/ numeric digits 50 /*being reasonable instead of 500*/ say right(0,digits()) /*display the 1st pal in B2 & B3.*/ say right(1,digits()) /*display the 2nd pal in B2 & B3.*/ !.= /* [↓] build list of powers of 3*/

    do i=1  until  pos('E',!.i)\==0;  !.i=3**i;   end  /*i*/

o=1 /* [↓] primary search: bin pals.*/

 do p=1                               /*P =power of 2 used to start #s.*/
 start=2**p + 1                       /*compute #s from  a  power of 2.*/
 b=strip(x2b(d2x(start)),'L',0)       /*express START number in binary.*/
 if length(b)//2==0  then iterate p   /*if bin# has even# digits, skip.*/
                                      /* [↓]   do searches in batches. */
     do m=0  for (2**p)%2  by 2       /*base 2 pals must be odd (|zero)*/
     n=start+m                        /*continue from START for odd #s.*/
     if n//3==0         then iterate  /*base 3 pals mustn't end in zero*/
     b=strip(x2b(d2x(n)),'L',0)       /*express the number in binary.  */
     if b\==reverse(b)  then iterate  /*is binary number palindromic?  */
     x=n                              /*convert the number to base 3.  */
       do j=o; if !.j>x then leave; end /*find upper limit of pow of 3.*/
     q=                               /*set base 3 result holder to nul*/
     o=j-1                            /*use this pow of 3 for next time*/
       do k=o  to 1  by -1;  _=!.k;  d=x%_;  q=q||d;  x=x//_;  end  /*k*/
     t=q||x                           /*handle the conversion residual.*/
     if t\==reverse(t)  then iterate  /*is this ternary # palindromic? */
     say right(n,digits())            /*display a decimal number.      */
     end   /*m*/                      /* [↑]  this process is sluggish.*/
 end       /*p*/                      /*no fork sticking, we ain't done*/</lang>

output

                                                 0
                                                 1
                                              6643
                                           1422773
                                           5415589
                                       90396755477