Numbers in base 10 that are palindromic in bases 2, 4, and 16

From Rosetta Code
Revision as of 18:36, 24 June 2021 by Chunes (talk | contribs) (Add Factor)
Numbers in base 10 that are palindromic in bases 2, 4, and 16 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.
Task
Find numbers in base 10 that are palindromic in bases 2, 4, and 16, where n < 25000



ALGOL 68

<lang algol68>BEGIN # show numbers in decimal that are palindromic in bases 2, 4 and 16 #

   INT max number = 25 000;    # maximum number to consider #
   INT min base   = 2;         # smallest base needed #
   INT max digits = BEGIN      # number of digits max number has in the smallest base #
                        INT d := 1;
                        INT v := max number;
                        WHILE v >= min base DO
                            v OVERAB min base;
                            d PLUSAB 1
                        OD;
                        d
                    END;
   # returns the digits of n in the specified base #
   PRIO DIGITS = 9;
   OP   DIGITS = ( INT n, INT base )[]INT:
        IF   INT v := ABS n;
             v < base
        THEN v # single dogit #
        ELSE   # multiple digits #
           [ 1 : max digits ]INT result;
           INT d pos := UPB result + 1;
           INT v     := ABS n;
           WHILE v > 0 DO
               result[ d pos -:= 1 ] := v MOD base;
               v OVERAB base
           OD;
           result[ d pos : UPB result ]
        FI # DIGITS # ;
   # returns TRUE if the digits in d form a palindrome, FALSE otherwise #
   OP   PALINDROMIC = ( []INT d )BOOL:
        BEGIN
            INT  left := LWB d, right := UPB d;
            BOOL is palindromic := TRUE;
            WHILE left < right AND is palindromic DO
                is palindromic := d[ left ] = d[ right ];
                left          +:= 1;
                right         -:= 1
            OD;
            is palindromic
        END;
   # print the numbers in decimal that are palendromic primes in bases 2, 4 and 16 #
   FOR n FROM 0 TO max number DO
       IF PALINDROMIC ( n DIGITS 16 ) THEN
           IF PALINDROMIC ( n DIGITS 4 ) THEN
               IF PALINDROMIC ( n DIGITS 2 ) THEN
                   print( ( " ", whole( n, 0 ) ) )
               FI
           FI
       FI
   OD

END</lang>

Output:
 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: io kernel math.parser prettyprint sequences ;

25,000 <iota> [

   { 2 4 16 } [ >base ] with map [ dup reverse = ] all?

] filter [ pprint bl ] each nl</lang>

Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 

Go

Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "rcu"
   "strconv"

)

func reverse(s string) string {

   chars := []rune(s)
   for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
       chars[i], chars[j] = chars[j], chars[i]
   }
   return string(chars)

}

func main() {

   fmt.Println("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:")
   var numbers []int
   for i := int64(0); i < 25000; i++ {
       b2 := strconv.FormatInt(i, 2)
       if b2 == reverse(b2) {
           b4 := strconv.FormatInt(i, 4)
           if b4 == reverse(b4) {
               b16 := strconv.FormatInt(i, 16)
               if b16 == reverse(b16) {
                   numbers = append(numbers, int(i))
               }
           }
       }
   }
   for i, n := range numbers {
       fmt.Printf("%6s ", rcu.Commatize(n))
       if (i+1)%10 == 0 {
           fmt.Println()
       }
   }
   fmt.Println("\n\nFound", len(numbers), "such numbers.")

}</lang>

Output:
Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:
     0      1      3      5     15     17     51     85    255    257 
   273    771    819  1,285  1,365  3,855  4,095  4,097  4,369 12,291 
13,107 20,485 21,845 

Found 23 such numbers.

Phix

with javascript_semantics
function palindrome(string s) return s=reverse(s) end function
function p2416(integer n)
    return palindrome(sprintf("%a",{{2,n}}))
       and palindrome(sprintf("%a",{{4,n}}))
       and palindrome(sprintf("%a",{{16,n}}))
end function
sequence res = apply(filter(tagset(25000,0),p2416),sprint)
printf(1,"%d found: %s\n",{length(res),join(res)})
Output:
23 found: 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Raku

<lang perl6>put "{+$_} such numbers:\n", .batch(10)».fmt('%5d').join("\n") given (^25000).grep: -> $n { all (2,4,16).map: { $n.base($_) eq $n.base($_).flip } }</lang>

Output:
23 such numbers:
    0     1     3     5    15    17    51    85   255   257
  273   771   819  1285  1365  3855  4095  4097  4369 12291
13107 20485 21845

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Numbers in base 10 that are palindromic in bases 2, 4, and 16:" + nl

row = 0 limit = 25000

for n = 1 to limit

   base2 = decimaltobase(n,2)
   base4 = decimaltobase(n,4)
   base16 = hex(n)
   bool = ispalindrome(base2) and ispalindrome(base4) and ispalindrome(base16)
   if bool = 1
      see "" + n + " "
      row = row + 1
      if row%5 = 0
         see nl
      ok
   ok

next

see nl + "Found " + row + " numbers" + nl see "done..." + nl

func decimaltobase(nr,base)

    decList = 0:15
    baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
    binList = [] 
    binary = 0
    remainder = 1
    while(nr != 0)
         remainder = nr % base
         ind = find(decList,remainder)
         rem = baseList[ind]
         add(binList,rem)
         nr = floor(nr/base) 
    end
    binlist = reverse(binList)
    binList = list2str(binList)
    binList = substr(binList,nl,"")  
    return binList

</lang>

Output:
working...
Numbers in base 10 that are palindromic in bases 2, 4, and 16:
1 3 5 15 17 
51 85 255 257 273 
771 819 1285 1365 3855 
4095 4097 4369 12291 13107 
20485 21845 
Found 22 numbers
done...

Wren

Library: Wren-fmt
Library: Wren-seq

<lang ecmascript>import "/fmt" for Conv, Fmt import "/seq" for Lst

System.print("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:") var numbers = [] for (i in 0..24999) {

   var b2 = Conv.itoa(i, 2)
   if (b2 == b2[-1..0]) {
       var b4 = Conv.itoa(i, 4)
       if (b4 == b4[-1..0]) {
           var b16 = Conv.itoa(i, 16)
           if (b16 == b16[-1..0]) numbers.add(i)
       }
   }

} for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$,6d", chunk) System.print("\nFound %(numbers.count) such numbers.")</lang>

Output:
Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:
     0      1      3      5     15     17     51     85
   255    257    273    771    819  1,285  1,365  3,855
 4,095  4,097  4,369 12,291 13,107 20,485 21,845

Found 23 such numbers.

XPL0

<lang XPL0>func Reverse(N, Base); \Reverse order of digits in N for given Base int N, Base, M; [M:= 0; repeat N:= N/Base;

       M:= M*Base + rem(0);

until N=0; return M; ];

int Count, N; [Count:= 0; for N:= 1 to 25000-1 do

   if N = Reverse(N, 2) &
      N = Reverse(N, 4) &
      N = Reverse(N, 16) then
       [IntOut(0, N);
       Count:= Count+1;
       if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
       ];

CrLf(0); IntOut(0, Count); Text(0, " such numbers found. "); ]</lang>

Output:
1       3       5       15      17      51      85      255     257     273
771     819     1285    1365    3855    4095    4097    4369    12291   13107
20485   21845   
22 such numbers found.