Created
January 29, 2014 03:50
-
-
Save trhura/8681516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
private static boolean isPalindrome (String string) { | |
String reversedString = new StringBuilder(string).reverse().toString(); | |
for (int i =0; i < string.length() / 2; i++) { | |
if (string.charAt(i) != reversedString.charAt(i)) | |
return false; | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
long sum = 0; | |
int upto = 1000000; // upto one million | |
for(int num = 0; num < upto; num++) { | |
String decimalString = Integer.toString(num); | |
String binaryString = Integer.toBinaryString(num); | |
if (isPalindrome(decimalString) && isPalindrome(binaryString)) { | |
sum += num; | |
} | |
} | |
System.out.println ("The sum of all palindromic number in both base 2" + | |
" and base 10 under 1,000,0000 is " + sum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment