Open In App

Double Base Palindrome

Improve
Improve
Like Article
Like
Save
Share
Report

Double base Palindrome as the name suggest is a number which is Palindrome in 2 bases. One of the base is 10 i.e. decimal and another base is k.(which can be 2 or others). 
Note : The palindromic number, in either base, may not include leading zeros. 
Example : The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. 

A Palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or 12321.

Find the sum of all numbers less than n which are palindromic in base 10 and base k.

Examples: 

Input :  10 2
Output : 25
Explanation : (here n = 10 and k = 2)
              1 3 5 7 9 (they are all palindrome 
              in base 10 and 2) so sum is :
              1 + 3 + 5 + 7 + 9 = 25

Input :  100 2
Output : 157
Explanation : 1 + 3 + 5 + 7 + 9 + 33 + 99 = 157

Method 1 : This method is simple. For every number less than n : 

  • Check if it is a palindrome in base 10
  • If yes, then convert it into base k
  • Check if it is palindrome in base k
  • If yes, then add it in sum.

This method is quite lengthy as it checks for every number whether it is a palindrome or not. So, for number as large as 1000000, it checks for every number. 
If k = 2, then a palindrome in base 2 can only be odd number, which might reduce the comparisons to 1000000 / 2 = 500000 (which is still large).

Below is the implementation of the above approach :  

C++




 


Java




// Java Program for Checking double base
// Palindrome.
import java.util.*;
 
class GFG{
// converts number to base k by changing
// it into string.
static String integer_to_string(int n, int base)
{
    String str="";
    while (n > 0) {
        int digit = n % base;
        n /= base;
        str+=(char)(digit+'0');
    }
    return str;
}
 
// function to check for palindrome
static int isPalindrome(int i, int k)
{
    int temp = i;
     
    // m stores reverse of a number
    int m = 0;
    while (temp > 0) {
        m = temp % 10 + m * 10;
        temp /= 10;
    }
     
    // if reverse is equal to number
    if (m == i) {
     
        // converting to base k
        String str = integer_to_string(m, k);
        StringBuilder sb = new StringBuilder(str);
        String str1=sb.reverse().toString();
        if (str.equals(str1)) {
            return i;
        }
    }
    return 0;
}
 
// function to find sum of palindromes
static void sumPalindrome(int n, int k){
     
    int sum = 0;
    for (int i = 1; i < n; i++) {
        sum += isPalindrome(i, k);
    }
    System.out.println("Total sum is "+sum);
}
 
// driver function
public static void main(String[] args)
{
    int n = 100;
    int k = 2;
 
    sumPalindrome(n, k);
}
}
// This code is contributed by mits


Python3




# Python3 Program for Checking
# double base Palindrome.
 
# converts number to base
# k by changing it into string.
def integer_to_string(n, base):
 
    str = "";
    while (n > 0):
        digit = n % base;
        n = int(n / base);
        str = chr(digit + ord('0')) + str;
    return str;
 
# function to check for palindrome
def isPalindrome(i, k):
    temp = i;
     
    # m stores reverse of a number
    m = 0;
    while (temp > 0):
        m = (temp % 10) + (m * 10);
        temp = int(temp / 10);
     
    # if reverse is equal to number
    if (m == i):
     
        # converting to base k
        str = integer_to_string(m, k);
        str1 = str;
     
        # reversing number in base k
        # str=str[::-1];
     
        # checking palindrome
        # in base k
        if (str[::-1] == str1):
            return i;
    return 0;
 
# function to find sum of palindromes
def sumPalindrome(n, k):
     
    sum = 0;
    for i in range(n):
        sum += isPalindrome(i, k);
    print("Total sum is", sum);
 
# Driver code
n = 100;
k = 2;
 
sumPalindrome(n, k);
 
# This code is contributed
# by mits


C#





PHP





Javascript





Output

Total sum is 157

Time Complexity: O(n*log(n))
Auxiliary Space: O(log(n))

Method 2 : This method is a little complex to understand but more advance than method 1. Rather than checking palindrome for two bases. This method generates palindrome in given range. 
Suppose we have a palindrome of the form 123321 in base k, then the first 3 digits define the palindrome. However, the 3 digits 123 also define the palindrome 12321. So the 3-digit number 123 defines a 5-digit palindrome and a 6 digit palindrome. From which follows that every positive number less than kn generates two palindromes less than k2n . This holds for every base k. Example : let’s say k = 10 that is decimal. Then for n = 1, all numbers less than 10n have 2 palindrome, 1 even length and 1 odd length in 102n. These are 1, 11 or 2, 22 or 3, 33 and so on. So, for 1000000 we generate around 2000 and for 108 we generate around 20000 palindromes. 

  • Start from i=1 and generate odd palindrome of it.
  • Check if this generated odd palindrome is also palindrome in base k
  • If yes, then add this number to sum.
  • Repeat the above three steps by changing i=i+1 until the last generated odd palindrome has crossed limit.
  • Now, again start from i=1 and generate even palindrome of it.
  • Check if this generated even palindrome is also palindrome in base k
  • If yes, then add this number to sum.
  • Repeat the above three steps by changing i=i+1 until the last generated even palindrome has crossed limit.

Below is the implementation of the above approach :  

C++





Java





Python3





C#





PHP





Javascript





Output

Total sum is 872187

Time Complexity: O(n*log(n))
Auxiliary Space: O(1)

 



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads