Hello! In this lecture, we will examine an algorithmic problem that finds the minimum value among prime numbers and palindrome numbers. Both prime numbers and palindrome numbers are basic mathematical concepts, but the combination of these two to solve specific requirements is common in coding tests. Therefore, develop your skills in efficient algorithm design and implementation through this problem.
Problem Description
Write a function that finds the minimum value of numbers that are both prime and palindrome among all integers within the given range.
For example, if we list the numbers that are both prime and palindrome in the range from 1 to 100, the minimum value among them is 2.
Input
An integer n (2 <= n <= 10,000) is provided. You need to find the numbers that are both prime and palindrome within this range.
Output
Output the minimum value of numbers that are both prime and palindrome. If there are no such numbers that meet the condition, output the message “There are no numbers that satisfy the condition.”
Problem Solving Process
To solve the problem, it can be divided into the following steps:
- Write a function to determine if a number is prime
- Write a function to determine if a number is a palindrome
- Find prime and palindrome numbers within the given range
- Return the minimum value
1. Write a function to determine if a number is prime
A prime number is a number that has only 1 and itself as divisors. To determine this, you can check if the number can be evenly divided by any number from 2 to sqrt(n).
func isPrime(_ number: Int) -> Bool {
guard number > 1 else { return false }
for i in 2...Int(sqrt(Double(number))) {
if number % i == 0 {
return false
}
}
return true
}
2. Write a function to determine if a number is a palindrome
A palindrome is a number that reads the same forwards and backwards. To check this, convert the number to a string and compare it with its reversed version.
func isPalindrome(_ number: Int) -> Bool {
let string = String(number)
return string == String(string.reversed())
}
3. Find prime and palindrome numbers within the given range
Utilize the two previously written functions (isPrime
, isPalindrome
) to verify numbers from 2 to n that satisfy both conditions.
func findMinPrimePalindrome(upTo n: Int) -> Int? {
var minPrimePalindrome: Int? = nil
for i in 2...n {
if isPrime(i) && isPalindrome(i) {
if minPrimePalindrome == nil || i < minPrimePalindrome! {
minPrimePalindrome = i
}
}
}
return minPrimePalindrome
}
4. Return the minimum value
After finding the minimum value, return it. If there is no minimum value, ensure an appropriate message is output.
let n = 100
if let minValue = findMinPrimePalindrome(upTo: n) {
print("The minimum value of numbers that are both prime and palindrome is: \(minValue)")
} else {
print("There are no numbers that satisfy the condition.")
}
Final Code
Integrating all parts, the final code is as follows:
func isPrime(_ number: Int) -> Bool {
guard number > 1 else { return false }
for i in 2...Int(sqrt(Double(number))) {
if number % i == 0 {
return false
}
}
return true
}
func isPalindrome(_ number: Int) -> Bool {
let string = String(number)
return string == String(string.reversed())
}
func findMinPrimePalindrome(upTo n: Int) -> Int? {
var minPrimePalindrome: Int? = nil
for i in 2...n {
if isPrime(i) && isPalindrome(i) {
if minPrimePalindrome == nil || i < minPrimePalindrome! {
minPrimePalindrome = i
}
}
}
return minPrimePalindrome
}
let n = 100
if let minValue = findMinPrimePalindrome(upTo: n) {
print("The minimum value of numbers that are both prime and palindrome is: \(minValue)")
} else {
print("There are no numbers that satisfy the condition.")
}
Conclusion
In this lecture, we covered the problem of finding numbers that are both prime and palindrome using the Swift language. I hope you were able to learn basic algorithm design and implementation skills through this problem. Additionally, you learned how to combine various functions to solve the problem. I look forward to encountering even more interesting and challenging problems in the next lecture!
Thank you!