Euler Prob 36

Had to use Java, so yes the code can be optimized…

package euler;

public class Prob36 {

	private static final int SUM = 1000000;
	
	private String reverse(String s) {
		char[] copyStr = s.toCharArray();
		
		int len = copyStr.length - 1;
		char[] str = new char[len + 1];
		
		for (int i = len, j = 0; i >= 0; i--, j++) {
			str[j] = copyStr[i];
			//System.out.println(copyStr[i]);
		}
		
		return String.copyValueOf(str);
		
	}

	public static void main(String[] args) {
		
		Prob36 p = new Prob36();
		long sumPalindromes = 0;
		
		for (int i = 0; i < SUM; i++) {
			if (Integer.toString(i).equals(p.reverse(Integer.toString(i))) 
					&& Integer.toBinaryString(i).equals(p.reverse(Integer.toBinaryString(i)))) {
				System.out.println(i);
				sumPalindromes += i;
			}
		}
		
		System.out.println("Sum is " + sumPalindromes);
	}

}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s