JavaScript Coding Test Course, Finding the Minimum Among Prime & Palindrome Numbers

In this course, we will cover how to solve JavaScript coding test problems. The topic of the problem is to find the minimum value among prime and palindromic numbers. We will guide you through the problem-solving process step by step, along with the necessary algorithms.

Problem Definition

We have a given integer N. We need to find the minimum value among all numbers that are both prime and palindromic from 1 to N. If such a number does not exist, return -1.

Example Problems

  • Input: N = 31
    Output: 3 (Among the primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, the palindromic number is 3)
  • Input: N = 11
    Output: 11 (The prime number 11 is a palindromic number)
  • Input: N = 1
    Output: -1 (1 is neither a prime nor a palindromic number)

Problem Solving Approach

To solve the problem, we will use the following methods.

  1. Find prime numbers from 1 to N.
  2. Filter the prime numbers to find those that are palindromic.
  3. Find the minimum value among the palindromic numbers.

Step-by-Step Implementation

Step 1: Finding Prime Numbers

To find prime numbers, we can use a simple Sieve of Eratosthenes algorithm. This algorithm is an efficient way to find prime numbers with a time complexity of O(n log log n).

function findPrimes(n) {
        const isPrime = Array(n + 1).fill(true);
        isPrime[0] = isPrime[1] = false; // 0 and 1 are not prime numbers.
        
        for (let i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (let j = i * i; j <= n; j += i) {
                    isPrime[j] = false; // Exclude multiples of i from primes
                }
            }
        }
        
        const primes = [];
        for (let i = 2; i <= n; i++) {
            if (isPrime[i]) {
                primes.push(i);
            }
        }
        return primes;
    }

Step 2: Finding Palindromic Numbers

A palindromic number is an integer that reads the same forwards and backwards. For example, 121 and 1331 are palindromic numbers.

function isPalindrome(num) {
        const str = num.toString();
        return str === str.split('').reverse().join('');
    }

Step 3: Finding the Minimum Value

Now we gather the numbers that are both prime and palindromic and find the minimum value among them.

function findMinPalindromicPrime(n) {
        const primes = findPrimes(n);
        const palindromicPrimes = primes.filter(isPalindrome);
        
        return palindromicPrimes.length > 0 ? Math.min(...palindromicPrimes) : -1;
    }

Complete Code

Now let's look at the final code that combines all the steps.

function isPalindrome(num) {
        const str = num.toString();
        return str === str.split('').reverse().join('');
    }

    function findPrimes(n) {
        const isPrime = Array(n + 1).fill(true);
        isPrime[0] = isPrime[1] = false;

        for (let i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (let j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }

        const primes = [];
        for (let i = 2; i <= n; i++) {
            if (isPrime[i]) {
                primes.push(i);
            }
        }
        return primes;
    }

    function findMinPalindromicPrime(n) {
        const primes = findPrimes(n);
        const palindromicPrimes = primes.filter(isPalindrome);
        
        return palindromicPrimes.length > 0 ? Math.min(...palindromicPrimes) : -1;
    }

    // Example usage
    console.log(findMinPalindromicPrime(31)); // Output: 3
    console.log(findMinPalindromicPrime(11)); // Output: 11
    console.log(findMinPalindromicPrime(1));  // Output: -1

Conclusion

In this course, we implemented an algorithm to solve the problem of finding the minimum value among prime and palindromic numbers. We explored the process of efficiently finding prime numbers using the Sieve of Eratosthenes, checking for palindromic numbers, and ultimately finding the minimum value that meets the criteria. To tackle such problems, both algorithmic thinking and programming skills are essential. I hope you continue to enhance your skills through various coding challenges.