Skip to content

Instantly share code, notes, and snippets.

@trhura
Created January 29, 2014 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trhura/8681516 to your computer and use it in GitHub Desktop.
Save trhura/8681516 to your computer and use it in GitHub Desktop.
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