Last active
December 16, 2015 13:49
-
-
Save danielgpm/5444402 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.io.BufferedReader; | |
import java.io.FileReader; | |
public class JavaPalindrome { | |
public static void main(String[] args) throws Exception { | |
String line = ""; | |
BufferedReader reader = new BufferedReader(new FileReader("seed.txt")); | |
int totalPalindromes = 0; | |
long ini = System.currentTimeMillis(); | |
while ((line = reader.readLine()) != null) { | |
String[] rango = line.split(" "); | |
int rango_ini = Integer.parseInt(rango[0]); | |
int rango_final = Integer.parseInt(rango[1]); | |
for (int num = rango_ini; num <= rango_final; num++) { | |
totalPalindromes += checkPalindrome(Integer.toString(num)); | |
} | |
} | |
System.out.format("Total Palindromes %d:\n ", totalPalindromes); | |
System.out.format("Duracion: %f\n" , (System.currentTimeMillis() - ini) / 1000.0); | |
} | |
public static int checkPalindrome(String sNum) { | |
for (int i = 0; i < sNum.length() / 2; i++) { | |
if (sNum.charAt(i) != sNum.charAt(sNum.length() - i - 1)) { | |
return 0; | |
} | |
} | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment