Project Euler – Problem 55

Problem 55:
A Lychrel number is a natural number which cannot form a palindrome through the iterative process of repeatedly reversing its base 10 digits and adding the resulting numbers.

Assume that after 50 steps of running the above iterative process, the number is deemed Lychrel…

How many Lychrel numbers are there below ten-thousand?

package runner;
 
import java.math.BigInteger;
 
class runner
{	
	public static boolean isPalindrome(String s){
		for(int j=0;j<s.length()/2;j++){
			if(s.charAt(j) != s.charAt(s.length()-j-1))
				return false;
		}
		return true;
	}
 
	public static String reverse(String s){
		StringBuffer x = new StringBuffer();
		for(int j=s.length()-1;j>-1;j--){
			x.append(s.charAt(j));
		}
		return x.toString();
	}
 
	public static void main (String[] args) throws java.lang.Exception
	{
		long time = System.currentTimeMillis();
 
		boolean[] isLychrel = new boolean[10000];
 
		for(int n=0;n<isLychrel.length;n++){
			if(isLychrel[n]) continue;
			String current = ""+n;
 
			int[] removal = new int[50]; 
			boolean foundPalin = false;
 
			BigInteger sum = new BigInteger(current);
			String sumString = sum.toString();
			int k=0;
			for(int i=0;i<50;i++){
				if(sumString.length() < 5){
					removal[k++] = sum.intValue();
				}
 
				String reverseSumStr = reverse(sumString);
				BigInteger reverseSum = new BigInteger(reverseSumStr);
 
				if(reverseSumStr.length() < 5){
					//This check is needed to remove issues like 10->01 (where 01 !->10)
					if(reverseSum.toString().length() == sumString.length())
						removal[k++] = reverseSum.intValue();
				}
 
				sum = sum.add(reverseSum);
				sumString = sum.toString();
				if(isPalindrome(sumString)){
					foundPalin = true;
					break;
				}
			}
 
			if(foundPalin){
				for(int i=0;i<removal.length;i++){
					isLychrel[removal[i]] = true;
				}
			}
		}
 
		int c = 0;
		for(int n=1;n<isLychrel.length;n++){
			if(!isLychrel[n]) c++;
		}
		System.out.println("c:"+c);
 
		System.out.println("time: "+(System.currentTimeMillis() - time));
	}
}