

Finding largest palindromic number divisible by smallest prime
source link: https://www.geeksforgeeks.org/finding-largest-palindromic-number-divisible-by-smallest-prime/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Finding largest palindromic number divisible by smallest prime
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++ 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; }
True False
Time Complexity: O(N2)
Auxiliary Space: O(1)
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK