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

From Rosetta Code
Revision as of 13:15, 24 June 2021 by CalmoSoft (talk | contribs) (Created page with "{{Draft task}} ;Task: Find numbers in base 10 that are palindromic in bases 2, 4, and 16, where '''n < 25000''' <br><br> =={{header|Ring}}== <lang ring> load "stdlib.ring"...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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



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...