Jump to content

Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions

(Realize in F#)
Line 343:
Found 23 such numbers.
</pre>
 
=={{header|Nim}}==
<lang Nim>import strutils, sugar
 
type Digit = 0..15
 
func toBase(n: Natural; b: Positive): seq[Digit] =
if n == 0: return @[Digit 0]
var n = n
while n != 0:
result.add n mod b
n = n div b
 
func isPalindromic(s: seq[Digit]): bool =
for i in 1..(s.len div 2):
if s[i-1] != s[^i]: return false
result = true
 
let list = collect(newSeq):
for n in 0..<25_000:
if n.toBase(2).isPalindromic and
n.toBase(4).isPalindromic and
n.toBase(16).isPalindromic: n
 
echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo list.join(" ")</lang>
 
{{out}}
<pre>Found 23 numbers which are palindromic in bases 2, 4 and 16:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845</pre>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.