Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Last active December 20, 2015 07:49
Show Gist options
  • Save dmadisetti/6096611 to your computer and use it in GitHub Desktop.
Save dmadisetti/6096611 to your computer and use it in GitHub Desktop.
Quick hack to eval largest Palindrome in products of first 1000 numbers
public class Palindrome{
int firstNum = 999;
int secondNum = 100;
int result = 0;
int x = 0;
int holder;
public Palindrome(){
while(firstNum > 100){
x = firstNum * secondNum;
if(result < x && isPalindrome(x)){
result = x;
}else
changeNums();
}
System.out.println(result);
}
public boolean isPalindrome(int num){
char[] stringed = Integer.toString(num).toCharArray();;
int len = stringed.length - 1;
int halflen = Math.round(stringed.length/2);
for(int i = 0;i < halflen;i++){
if(stringed[i] == stringed[len-i])
continue;
return false;
}
return true;
}
public void changeNums(){
// Potentially do binary like jumps
if(firstNum == secondNum){
firstNum--;
secondNum = 100;
}else{
secondNum++;
}
}
public static void main(String[] args) {
new Palindrome();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment