Skip to content

Add PrimeNumberUtils with isPrime and getPrimesUpTo methods #6371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/main/java/com/thealgorithms/math/PrimeNumberUtils.java
Original file line number Diff line number Diff line change
@@ -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<Integer> getPrimesUpTo(int limit) {
List<Integer> 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));
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/thealgorithms/others/EvenOddChecker.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/thealgorithms/strings/PalindromeChecker.java
Original file line number Diff line number Diff line change
@@ -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));
}
}