Hello, and welcome to the coding test course. Today, we will solve an interesting and challenging algorithm problem called ‘Finding Curious Primes’. The goal of this problem is to find curious primes within a given range. A curious prime is a prime number that has specific properties, and solving this can help develop algorithmic thinking necessary for coding tests.
Problem Description
Given an integer N, write a program to determine whether each prime number from 1 to N is a curious prime. A curious prime must satisfy the following conditions:
- The sum of the digits of the prime number p must be another prime number q.
- A prime number is only recognized as a curious prime if the sum of its digits is prime.
For example, for 13, the sum of the digits is 1 + 3 = 4, and since 4 is not a prime number, 13 is not a curious prime. However, for 29, the sum of the digits is 2 + 9 = 11, and since 11 is a prime, 29 is recognized as a curious prime.
Input and Output Format
Input:
- The first line contains an integer N (1 ≤ N ≤ 1000).
Output:
- Print all curious primes less than or equal to N in ascending order.
Problem Solving Strategy
To solve this problem, several steps are necessary:
- Generate primes from 1 to N.
- Calculate the sum of the digits for each prime.
- Check if the calculated sum is a prime.
- Collect and output the curious primes.
Algorithm Implementation
Now let’s implement the algorithm in code. We will use Kotlin to solve this problem. The implementation aims to maintain a functional programming style and be written clearly.
fun main() {
val n = readLine()!!.toInt()
val primes = generatePrimes(n)
val curiousPrimes = mutableListOf()
for (prime in primes) {
if (isCuriousPrime(prime)) {
curiousPrimes.add(prime)
}
}
println(curiousPrimes.joinToString(" "))
}
fun generatePrimes(n: Int): List {
val isPrime = BooleanArray(n + 1) { true }
isPrime[0] = false
isPrime[1] = false
for (i in 2..Math.sqrt(n.toDouble()).toInt()) {
if (isPrime[i]) {
for (j in i * i..n step i) {
isPrime[j] = false
}
}
}
return (2..n).filter { isPrime[it] }
}
fun sumOfDigits(prime: Int): Int {
return prime.toString().map { it.toString().toInt() }.sum()
}
fun isCuriousPrime(prime: Int): Boolean {
val sum = sumOfDigits(prime)
return isPrime(sum)
}
fun isPrime(num: Int): Boolean {
if (num < 2) return false
for (i in 2..Math.sqrt(num.toDouble()).toInt()) {
if (num % i == 0) return false
}
return true
}
Code Explanation
I will explain the above code step by step.
Main Function
The main function, which is the entry point of the program, receives user input and calls the necessary functions to find curious primes. It generates primes based on the input value of N and checks each prime against the criteria for being a curious prime while saving the results in a list.
GeneratePrimes Function
This function generates primes up to the given N. It efficiently calculates primes using the Sieve of Eratosthenes algorithm and checks for prime status using a Boolean array. Ultimately, it returns the list of discovered primes.
SumOfDigits Function
This function adds the digits of the entered prime and returns their sum. It converts each digit to an integer and calculates the total using string transformation and mapping.
IsCuriousPrime Function
This function calculates the sum of the digits for a given prime and checks if this sum is prime. It thus determines whether the prime is a curious prime.
IsPrime Function
This function determines whether a given number is prime. According to the definition of prime numbers, it only verifies for numbers greater than or equal to 2 and checks divisibility through numbers from 2 up to the square root of the number.
Example Execution
For the given example with N being 30, the result of the execution is as follows:
2 3 5 11 23
Conclusion
Today, we solved the problem of ‘Finding Curious Primes’, enhancing our understanding of primes and the sum of digits. Through this process, we learned algorithmic thinking using Kotlin and acquired useful techniques applicable in real coding tests. In the next session, we will explore another interesting algorithm problem. Thank you!
Additional Learning Resources: