diff --git a/src/main/java/com/thealgorithms/math/PrimeNumberUtils.java b/src/main/java/com/thealgorithms/math/PrimeNumberUtils.java new file mode 100644 index 000000000000..0c5e3ad1f307 --- /dev/null +++ b/src/main/java/com/thealgorithms/math/PrimeNumberUtils.java @@ -0,0 +1,49 @@ +package com.thealgorithms.math; + +import java.util.ArrayList; +import java.util.List; + +/** + * Utility class to work with prime numbers. + */ +public class PrimeNumberUtils { + + /** + * Checks if a number is a prime. + * + * @param n the number to check + * @return true if prime, false otherwise + */ + public static boolean isPrime(int n) { + if (n <= 1) return false; + if (n == 2) return true; + if (n % 2 == 0) return false; + + for (int i = 3; i <= Math.sqrt(n); i += 2) { + if (n % i == 0) return false; + } + + return true; + } + + /** + * Returns a list of prime numbers up to a given number. + * + * @param limit the upper bound (inclusive) + * @return list of prime numbers <= limit + */ + public static List getPrimesUpTo(int limit) { + List primes = new ArrayList<>(); + for (int i = 2; i <= limit; i++) { + if (isPrime(i)) { + primes.add(i); + } + } + return primes; + } + + public static void main(String[] args) { + System.out.println("Is 17 prime? " + isPrime(17)); + System.out.println("Primes up to 30: " + getPrimesUpTo(30)); + } +} diff --git a/src/main/java/com/thealgorithms/others/EvenOddChecker.java b/src/main/java/com/thealgorithms/others/EvenOddChecker.java new file mode 100644 index 000000000000..8c32d41b7c90 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/EvenOddChecker.java @@ -0,0 +1,12 @@ +package com.thealgorithms.others; + +public class EvenOddChecker { + public static String checkEvenOdd(int number) { + return number % 2 == 0 ? "Even" : "Odd"; + } + + public static void main(String[] args) { + int test = 7; + System.out.println(test + " is " + checkEvenOdd(test)); + } +} diff --git a/src/main/java/com/thealgorithms/strings/PalindromeChecker.java b/src/main/java/com/thealgorithms/strings/PalindromeChecker.java new file mode 100644 index 000000000000..39715c15c83e --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/PalindromeChecker.java @@ -0,0 +1,41 @@ +package com.thealgorithms.strings; + +/** + * Utility class to check if a string is a palindrome. + */ +public class PalindromeChecker { + + /** + * Checks if the given input is a palindrome. + * Ignores spaces, punctuation, and case. + * + * @param input the string to check + * @return true if it's a palindrome, false otherwise + */ + public static boolean isPalindrome(String input) { + if (input == null) { + return false; + } + // Remove non-letter characters and convert to lowercase + String cleaned = input.replaceAll("[^a-zA-Z]", "").toLowerCase(); + int left = 0; + int right = cleaned.length() - 1; + + while (left < right) { + if (cleaned.charAt(left) != cleaned.charAt(right)) { + return false; + } + left++; + right--; + } + + return true; + } + + public static void main(String[] args) { + String test1 = "A man a plan a canal Panama"; + String test2 = "OpenAI"; + System.out.println("\"" + test1 + "\" is palindrome? " + isPalindrome(test1)); + System.out.println("\"" + test2 + "\" is palindrome? " + isPalindrome(test2)); + } +}