Project Euler: Problem 36 – Double-base palindromes

Problem 36:

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)


While researching how to convert a decimal to binary, I saw that the BigInteger class has a toString(int radix) method that can convert the output of a BigInteger into different bases (in this case, base 2). I then fed the decimal number and binary number into a palindrome method I wrote for Problem 4. If both were palindromes, I added the decimal number to a running total.

int answer = 0;

for(int i = 0; i < 1000000; i++) {
   if(isPalindrome(i) && isPalindrome(BigInteger.valueOf(i).toString(2))) {
      answer += i;

System.out.println("Answer: "+answer);

Note: “isPalindrome” is a helper method I wrote that checks if the given input (Number/String) is a palindrome.

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *