Open In App

Finding largest palindromic number divisible by smallest prime

Last Updated : 26 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a range [start, end], find the largest palindromic number in the range and check if it is divisible by the smallest prime number in the range.

Examples:

Input: start = 1, end = 500
Output: true
Explanation: The largest palindromic number in the range is 484, which is divisible by the smallest prime number 2 in the range.

Input: start = 50, end = 1000
Output: false
Explanation: The largest palindromic number in the range is 999, which is not divisible by the smallest prime number 53 in the range.

Input: start = 1, end = 10
Output: false
Explanation: The largest palindromic number in the range is 9, which is not divisible by the smallest prime number 2 in the range.

Approach: This can be solved with the following idea:

  • Start iterating from the end toward the start of the given range.
  • For each number, check if it is a palindrome. If yes, store it as the largest palindrome number found so far and break the loop.
  • Find the smallest prime number in the range.
  •  Check if the largest palindrome number found in step 2 is divisible by the smallest prime number found in step 3. 
  •  Return the result.

Below are the steps for the above approach :

  • Define a function isPalindrome to check if a given number is a palindrome or not.
  • Define a function smallestPrimeInRange to find the smallest prime number in a given range.
  • Define a function largestPalindromicDivisibleBySmallestPrime to implement the above approach.

Below is the code for the above approach :

C++




// C++ code for the above approach:
#include <iostream>
#include <string>
using namespace std;
 
// Function to check if a given number
// is a palindrome or not
bool isPalindrome(int num)
{
    int reverseNum = 0;
    int tempNum = num;
 
    while (tempNum != 0) {
        int remainder = tempNum % 10;
        reverseNum = reverseNum * 10 + remainder;
        tempNum /= 10;
    }
 
    return num == reverseNum;
}
 
// Function to find the smallest prime
// number in a given range
int smallestPrimeInRange(int start, int end)
{
 
    for (int num = start; num <= end; num++) {
        bool isPrime = true;
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            return num;
        }
    }
 
    // no prime number found in range
    return -1;
}
 
// Function to find the largest
// palindromic number in a given range and
// check if  it is divisible by the
// smallest prime number in the range
string largestPalindromicDivisibleBySmallestPrime(int start,
                                                  int end)
{
    int largestPalindrome = -1;
    for (int num = end; num >= start; num--) {
        if (isPalindrome(num)) {
            largestPalindrome = num;
            break;
        }
    }
 
    int smallestPrime = smallestPrimeInRange(start, end);
    if (smallestPrime == -1) {
        return "False";
    }
 
    if (largestPalindrome % smallestPrime == 0) {
        return "True";
    }
    else {
        return "False";
    }
}
 
// Driver code to test the function
int main()
{
    int start = 1;
    int end = 500;
 
    // Function call
    string result
        = largestPalindromicDivisibleBySmallestPrime(start,
                                                     end);
    cout << result << endl;
 
    start = 50;
    end = 1000;
 
    // Function call
    result = largestPalindromicDivisibleBySmallestPrime(
        start, end);
    cout << result << endl;
 
    return 0;
}


Java




// JAVA code for the above approach:
 
class GFG {
 
    // Function to check if a given number is a palindrome
    // or not
    static boolean isPalindrome(int num)
    {
        int reverseNum = 0;
        int tempNum = num;
 
        while (tempNum != 0) {
            int remainder = tempNum % 10;
            reverseNum = reverseNum * 10 + remainder;
            tempNum /= 10;
        }
 
        return num == reverseNum;
    }
 
    // Function to find the smallest prime number in a given
    // range
    static int smallestPrimeInRange(int start, int end)
    {
 
        for (int num = start; num <= end; num++) {
            boolean isPrime = true;
            for (int i = 2; i < num; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                return num;
            }
        }
 
        // No prime number found in the range
        return -1;
    }
 
    // Function to find the largest palindromic number in a
    // given range and check if it is divisible by the
    // smallest prime number in the range
    static String
    largestPalindromicDivisibleBySmallestPrime(int start,
                                               int end)
    {
        int largestPalindrome = -1;
        for (int num = end; num >= start; num--) {
            if (isPalindrome(num)) {
                largestPalindrome = num;
                break;
            }
        }
 
        int smallestPrime
            = smallestPrimeInRange(start, end);
        if (smallestPrime == -1) {
            return "False";
        }
 
        if (largestPalindrome % smallestPrime == 0) {
            return "True";
        }
        else {
            return "False";
        }
    }
 
    // Driver code to test the function
    public static void main(String[] args)
    {
        int start = 1;
        int end = 500;
 
        // Function call
        String result
            = largestPalindromicDivisibleBySmallestPrime(
                start, end);
        System.out.println(result);
 
        start = 50;
        end = 1000;
 
        // Function call
        result = largestPalindromicDivisibleBySmallestPrime(
            start, end);
        System.out.println(result);
    }
}
 
// This code is contributed by shivamgupta310570


Python3




# Python3 code for the above approach:
# Function to check if a given number
# is a palindrome or not
 
 
def isPalindrome(num):
    reverseNum = 0
    tempNum = num
 
    while tempNum != 0:
        remainder = tempNum % 10
        reverseNum = reverseNum * 10 + remainder
        tempNum //= 10
 
    return num == reverseNum
 
# Function to find the smallest prime
# number in a given range
 
 
def smallestPrimeInRange(start, end):
    for num in range(start, end + 1):
        isPrime = True
        for i in range(2, num):
            if num % i == 0:
                isPrime = False
                break
        if isPrime:
            return num
 
    # No prime number found in range
    return -1
 
# Function to find the largest
# palindromic number in a given range and
# check if it is divisible by the
# smallest prime number in the range
 
 
def largestPalindromicDivisibleBySmallestPrime(start, end):
    largestPalindrome = -1
    for num in range(end, start - 1, -1):
        if isPalindrome(num):
            largestPalindrome = num
            break
 
    smallestPrime = smallestPrimeInRange(start, end)
    if smallestPrime == -1:
        return "False"
 
    if largestPalindrome % smallestPrime == 0:
        return "True"
    else:
        return "False"
 
 
# Driver code to test the function
start = 1
end = 500
 
# Function call
result = largestPalindromicDivisibleBySmallestPrime(start, end)
print(result)
 
start = 50
end = 1000
 
# Function call
result = largestPalindromicDivisibleBySmallestPrime(start, end)
print(result)
 
# This code is contributed by rambabuguphka


C#




// C# code for the above approach
using System;
 
namespace PalindromicAndDivisible {
public class GFG {
    // Function to check if a given number
    // is a palindrome or not
    static bool IsPalindrome(int num)
    {
        int reverseNum = 0;
        int tempNum = num;
 
        while (tempNum != 0) {
            int remainder = tempNum % 10;
            reverseNum = reverseNum * 10 + remainder;
            tempNum /= 10;
        }
 
        return num == reverseNum;
    }
 
    // Function to find the smallest prime
    // number in a given range
    static int SmallestPrimeInRange(int start, int end)
    {
        for (int num = start; num <= end; num++) {
            bool isPrime = true;
            for (int i = 2; i < num; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                return num;
            }
        }
 
        // no prime number found in range
        return -1;
    }
 
    // Function to find the largest
    // palindromic number in a given range and
    // check if it is divisible by the
    // smallest prime number in the range
    static string
    LargestPalindromicDivisibleBySmallestPrime(int start,
                                               int end)
    {
        int largestPalindrome = -1;
        for (int num = end; num >= start; num--) {
            if (IsPalindrome(num)) {
                largestPalindrome = num;
                break;
            }
        }
 
        int smallestPrime
            = SmallestPrimeInRange(start, end);
        if (smallestPrime == -1) {
            return "False";
        }
 
        if (largestPalindrome % smallestPrime == 0) {
            return "True";
        }
        else {
            return "False";
        }
    }
 
    // Driver code to test the function
    static void Main(string[] args)
    {
        int start = 1;
        int end = 500;
 
        // Function call
        string result
            = LargestPalindromicDivisibleBySmallestPrime(
                start, end);
        Console.WriteLine(result);
 
        start = 50;
        end = 1000;
 
        // Function call
        result = LargestPalindromicDivisibleBySmallestPrime(
            start, end);
        Console.WriteLine(result);
    }
}
}
 
// This code is contributed by Susobhan Akhuli


Javascript




// JavaScript code for the above approach
 
// Function to check if a given number is a palindrome or not
function isPalindrome(num) {
    let reverseNum = 0;
    let tempNum = num;
 
    while (tempNum !== 0) {
        const remainder = tempNum % 10;
        reverseNum = reverseNum * 10 + remainder;
        tempNum = Math.floor(tempNum / 10);
    }
 
    return num === reverseNum;
}
 
// Function to find the smallest prime number in a given range
function smallestPrimeInRange(start, end) {
    for (let num = start; num <= end; num++) {
        let isPrime = true;
        for (let i = 2; i < num; i++) {
            if (num % i === 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            return num;
        }
    }
 
    // No prime number found in the range
    return -1;
}
 
// Function to find the largest palindromic number
// in a given range and check if it is divisible by
// the smallest prime number in the range
function largestPalindromicDivisibleBySmallestPrime(start, end) {
    let largestPalindrome = -1;
    for (let num = end; num >= start; num--) {
        if (isPalindrome(num)) {
            largestPalindrome = num;
            break;
        }
    }
 
    const smallestPrime = smallestPrimeInRange(start, end);
    if (smallestPrime === -1) {
        return "False";
    }
 
    if (largestPalindrome % smallestPrime === 0) {
        return "True";
    } else {
        return "False";
    }
}
 
// Driver code to test the function
const start1 = 1;
const end1 = 500;
const result1 = largestPalindromicDivisibleBySmallestPrime(start1, end1);
console.log(result1)
 
const start2 = 50;
const end2 = 1000;
const result2 = largestPalindromicDivisibleBySmallestPrime(start2, end2);
console.log(result2);
 
// This code is contributed by Susobhan Akhuli


Output

True
False







Time Complexity: O(N2)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads