Last active
December 20, 2015 07:49
-
-
Save dmadisetti/6096611 to your computer and use it in GitHub Desktop.
Quick hack to eval largest Palindrome in products of first 1000 numbers
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 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