Finding largest palindromic number divisible by smallest prime
Last Updated :
26 Sep, 2023
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++
#include <iostream>
#include <string>
using namespace std;
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;
}
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;
}
}
return -1;
}
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" ;
}
}
int main()
{
int start = 1;
int end = 500;
string result
= largestPalindromicDivisibleBySmallestPrime(start,
end);
cout << result << endl;
start = 50;
end = 1000;
result = largestPalindromicDivisibleBySmallestPrime(
start, end);
cout << result << endl;
return 0;
}
|
Java
class GFG {
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;
}
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;
}
}
return - 1 ;
}
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" ;
}
}
public static void main(String[] args)
{
int start = 1 ;
int end = 500 ;
String result
= largestPalindromicDivisibleBySmallestPrime(
start, end);
System.out.println(result);
start = 50 ;
end = 1000 ;
result = largestPalindromicDivisibleBySmallestPrime(
start, end);
System.out.println(result);
}
}
|
Python3
def isPalindrome(num):
reverseNum = 0
tempNum = num
while tempNum ! = 0 :
remainder = tempNum % 10
reverseNum = reverseNum * 10 + remainder
tempNum / / = 10
return num = = reverseNum
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
return - 1
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"
start = 1
end = 500
result = largestPalindromicDivisibleBySmallestPrime(start, end)
print (result)
start = 50
end = 1000
result = largestPalindromicDivisibleBySmallestPrime(start, end)
print (result)
|
C#
using System;
namespace PalindromicAndDivisible {
public class GFG {
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;
}
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;
}
}
return -1;
}
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" ;
}
}
static void Main( string [] args)
{
int start = 1;
int end = 500;
string result
= LargestPalindromicDivisibleBySmallestPrime(
start, end);
Console.WriteLine(result);
start = 50;
end = 1000;
result = LargestPalindromicDivisibleBySmallestPrime(
start, end);
Console.WriteLine(result);
}
}
}
|
Javascript
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 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;
}
}
return -1;
}
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" ;
}
}
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);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...