Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add C) |
Not a robot (talk | contribs) (Add Python) |
||
Line 239: | Line 239: | ||
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 |
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 |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<lang python>def reverse(n, base): |
|||
r = 0 |
|||
while n > 0: |
|||
r = r*base + n%base |
|||
n = n//base |
|||
return r |
|||
def palindrome(n, base): |
|||
return n == reverse(n, base) |
|||
cnt = 0 |
|||
for i in range(25000): |
|||
if all(palindrome(i, base) for base in (2,4,16)): |
|||
cnt += 1 |
|||
print("{:5}".format(i), end=" \n"[cnt % 12 == 0]) |
|||
print()</lang> |
|||
{{out}} |
|||
<pre> 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|Raku}}== |
=={{header|Raku}}== |