Problem Definition
This is a problem of finding the minimum value among numbers that are both prime and palindromic within a given range. A prime number is a natural number that has only 1 and itself as divisors, while a palindrome is a number that reads the same forward and backward. For example, 121 is a palindromic number, and 131, 151, 181, and 191 are also primes and palindromes.
Problem Approach
To solve this problem, we will follow these steps:
- Implement a method to find prime numbers within the given range.
- Implement a method to check for palindromic numbers among the found primes.
- Find the minimum value among the palindromic primes.
Step 1: Implementing a Function to Find Prime Numbers
We can use a simple prime number detection algorithm to find primes. This algorithm determines if a number is prime by checking whether it is divisible by any other numbers. Below is the prime detection code implemented in Java.
public class PrimeUtil {
public static boolean isPrime(int number) {
if (number <= 1) return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) return false;
}
return true;
}
}
Step 2: Implementing a Function to Check for Palindromes
To check for palindromic numbers, we can convert the number to a string and compare the original string with the reversed string. Here is the Java code implementing this.
public class PalindromeUtil {
public static boolean isPalindrome(int number) {
String str = Integer.toString(number);
String reversedStr = new StringBuilder(str).reverse().toString();
return str.equals(reversedStr);
}
}
Step 3: Finding the Minimum Value
After finding the numbers that are both prime and palindromic, we write the code to find the minimum value among these. This part will also be implemented in Java.
import java.util.ArrayList;
import java.util.List;
public class MinPrimePalindromeFinder {
public static int findMinPrimePalindrome(int start, int end) {
List<Integer> primePalindromes = new ArrayList<>();
for (int i = start; i <= end; i++) {
if (PrimeUtil.isPrime(i) && PalindromeUtil.isPalindrome(i)) {
primePalindromes.add(i);
}
}
if (primePalindromes.isEmpty()) {
return -1; // No prime palindromic numbers
}
return primePalindromes.stream().min(Integer::compare).get();
}
}
Complete Code
Now, let’s integrate all the parts we have implemented into a single program. Below is the final code.
public class Main {
public static void main(String[] args) {
int start = 10;
int end = 200;
int minPrimePalindrome = MinPrimePalindromeFinder.findMinPrimePalindrome(start, end);
if (minPrimePalindrome != -1) {
System.out.println("Minimum value among numbers that are both prime and palindromic: " + minPrimePalindrome);
} else {
System.out.println("There are no numbers that are both prime and palindromic in the given range.");
}
}
}
Test Cases
You can test with ranges such as:
- Range: 10 ~ 200 (Result: 101)
- Range: 1 ~ 1000 (Result: 101)
- Range: 101 ~ 150 (Result: 131)
- Range: 200 ~ 300 (Result: None)
Conclusion
In this post, we discussed how to solve the problem of finding the minimum value among numbers that are both prime and palindromic using Java. After implementing separate algorithms for prime detection and palindrome checking, we combined them to achieve the desired result. We also validated the accuracy with test cases.
The topics of primes and palindromes frequently appear in algorithm examinations; therefore, the methods used to solve these problems can be applied to other similar problems. I hope this article helps you in solving your own problems.