Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 78: | Line 78: | ||
Found 22 numbers |
Found 22 numbers |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-fmt}} |
|||
{{libheader|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> |
|||
{{out}} |
|||
<pre> |
|||
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. |
|||
</pre> |
</pre> |
Revision as of 15:54, 24 June 2021
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
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
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
<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.