Project Euler: Problem 36

By | 31. August 2013

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.

long result = 0;
for (long i = 0; i < 1000000; i++) {
	StringBuilder tempdec = new StringBuilder(Long.toString(i));
	if (tempdec.toString().equals(tempdec.reverse().toString())) {
		StringBuilder tempbin = new StringBuilder(Long.toBinaryString(i));
		if (tempbin.toString().equals(tempbin.reverse().toString())) {
			result = result + i;
		}
	} 
}
System.out.println(result);

Lösung: 872187

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert