Created
February 21, 2025 17:01
-
-
Save icarito/6e0380d449c0de47a9e119dc24cc8a56 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.util.ArrayList; | |
class Solution{ | |
private static boolean isPalindrome(int num) { | |
String s = Integer.toString(num); | |
return s.equals(new StringBuilder(s).reverse().toString()); | |
} | |
public static int values (int n){ | |
ArrayList<Integer> validPalindromes = new ArrayList<>(); | |
// recorrer todos los números desde 1 hasta n | |
for (int a = 1; a * a < n; a++) { | |
// calcular desde el numero a hasta n | |
// las posibles sumas de cuadrados | |
int sum = a * a; | |
int next = a + 1; | |
while (true) { | |
sum += next * next; | |
if (sum >= n) { | |
break; | |
} | |
// chequeamos si son palíndromos | |
if (isPalindrome(sum)) { | |
// si son palíndromos los añadimos a un ArrayList (para evitar duplicados) | |
if (!validPalindromes.contains(sum)) { | |
validPalindromes.add(sum); | |
} | |
} | |
next++; | |
} | |
} | |
return validPalindromes.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment