Skip to content

Instantly share code, notes, and snippets.

@benmccormick
Created April 14, 2013 22:39
Show Gist options
  • Save benmccormick/5384531 to your computer and use it in GitHub Desktop.
Save benmccormick/5384531 to your computer and use it in GitHub Desktop.
A java equivalent of my coffeescript solution for the Google Code Jam qualification problem
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Palindrome {
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new FileReader(new File("bigpalindrome.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output2.txt")));
int numcase = Integer.parseInt(br.readLine());
for(int i=0; i<numcase; i++)
{
String[] nums = br.readLine().split(" ");
System.out.println(i+1);
long a= Long.parseLong(nums[0]);
long b = Long.parseLong(nums[1]);
int val = findNums(a,b);
bw.write("Case #"+(i+1)+": "+val);
bw.newLine();
}
bw.close();
}
private static int findNums(long a, long b)
{
int count = 0;
long x = (long)Math.ceil(Math.sqrt((double)a));
long y = (long)Math.floor(Math.sqrt((double)b));
for (long num = x; num <=y; num++)
{
if(isPalindrome(num+""))
{
count += isPalindrome(num*num+"") ? 1 : 0;
}
}
return count;
}
private static boolean isPalindrome(String num)
{
String reversed = new StringBuilder(num).reverse().toString();
return reversed.equals(num);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment