Solution to Project Euler Problem 36: Double-base palindromes - 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.)
Updated: Aug. 15, 2022 — Training Time: 1 Minute
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 36: Double-base palindromes.
Difficulty: Easy.
Objective: 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.)
Input: None.
Expected Output: 872187.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
public class SikademyEulerSolution {
public String run() {
long sum = 0;
for (int i = 1; i < 1000000; i++) {
if (Library.isPalindrome(Integer.toString(i, 10)) && Library.isPalindrome(Integer.toString(i, 2)))
sum += i;
}
return Long.toString(sum);
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
def compute():
ans = sum(i for i in range(1000000) if is_decimal_binary_palindrome(i))
return str(ans)
def is_decimal_binary_palindrome(n):
s = str(n)
if s != s[ : : -1]:
return False
t = bin(n)[2 : ]
return t == t[ : : -1]
if __name__ == "__main__":
print(compute())