Open in App
Not now

# Double Base Palindrome

• Difficulty Level : Medium
• Last Updated : 28 Mar, 2023

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++

 `// CPP Program for Checking double base` `// Palindrome.` `#include ` `using` `namespace` `std;`   `// converts number to base k by changing` `// it into string.` `string integer_to_string(``int` `n, ``int` `base)` `{` `    ``string str;` `    ``while` `(n > 0) {` `        ``int` `digit = n % base;` `        ``n /= base;` `        ``str.push_back(digit + ``'0'``);` `    ``}` `    ``return` `str;` `}`   `// function to check for palindrome` `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);` `        ``string str1 = str;` `    `  `        ``// reversing number in base k` `        ``reverse(str.begin(), str.end());` `    `  `        ``// checking palindrome in base k` `        ``if` `(str == str1) {` `            ``return` `i;` `        ``}` `    ``}` `    ``return` `0;` `}`   `// function to find sum of palindromes` `void` `sumPalindrome(``int` `n, ``int` `k){` `    `  `    ``int` `sum = 0;` `    ``for` `(``int` `i = 1; i < n; i++) {` `        ``sum += isPalindrome(i, k);` `    ``}` `    ``cout << ``"Total sum is "` `<< sum;` `}`   `// driver function` `int` `main()` `{` `    ``int` `n = 100;` `    ``int` `k = 2;`   `    ``sumPalindrome(n, k);` `    ``return` `0;` `}`

## 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#

 `// C# Program for Checking double base ` `// Palindrome. ` `using` `System;`   `class` `GFG{` `// converts number to base k by changing ` `// it into string. ` `static` `string` `integer_to_string(``int` `n, ``int` `base1) ` `{ ` `    ``string` `str=``""``; ` `    ``while` `(n > 0) { ` `        ``int` `digit = n % base1; ` `        ``n /= base1; ` `        ``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);` `        ``char``[] ch = str.ToCharArray();` `        ``Array.Reverse(ch);` `        ``string` `str1=``new` `String(ch);;` `        ``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); ` `    ``} ` `    ``Console.WriteLine(``"Total sum is "``+sum); ` `} `   `// driver function ` ` ``static` `void` `Main() ` `{ ` `    ``int` `n = 100; ` `    ``int` `k = 2; `   `    ``sumPalindrome(n, k); ` `} ` `}` `// This code is contributed by mits`

## PHP

 ` 0)` `    ``{` `        ``\$digit` `= ``\$n` `% ``\$base``;` `        ``\$n` `= (int)(``\$n` `/ ``\$base``);` `        ``\$str` `= (``\$digit` `+ ``'0'``) . ``\$str``;` `    ``}` `    ``return` `\$str``;` `}`   `// function to check` `// for palindrome` `function` `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=strrev(\$str);` `    `  `        ``// checking palindrome` `        ``// in base k` `        ``if` `(``strcmp``(``strrev``(``\$str``), ``\$str1``) == 0) ` `        ``{` `            ``return` `\$i``;` `        ``}` `    ``}` `    ``return` `0;` `}`   `// function to find` `// sum of palindromes` `function` `sumPalindrome(``\$n``, ``\$k``)` `{` `    `  `    ``\$sum` `= 0;` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``{` `        ``\$sum` `+= isPalindrome(``\$i``, ``\$k``);` `    ``}` `    ``echo` `"Total sum is "` `. ``\$sum``;` `}`   `// Driver code` `\$n` `= 100;` `\$k` `= 2;`   `sumPalindrome(``\$n``, ``\$k``);`   `// This code is contributed ` `// by mits ` `?>`

## 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++

 `// CPP Program for Checking double` `// base Palindrome.` `#include ` `using` `namespace` `std;`   `// generates even and odd palindromes` `int` `makePalindrome(``int` `n, ``bool` `odd)` `{` `    ``int` `res = n;` `    ``if` `(odd)` `        ``n = n / 10;` `    ``while` `(n > 0) {` `        ``res = 10 * res + n % 10;` `        ``n /= 10;` `    ``}` `    ``return` `res;` `}`   `// Check if a number is palindrome` `// in base k` `bool` `isPalindrome(``int` `n, ``int` `base)` `{` `    ``int` `reversed = 0;` `    ``int` `temp = n;` `    ``while` `(temp > 0) {` `        ``reversed = reversed * base + ` `                   ``temp % base;` `        ``temp /= base;` `    ``}` `    ``return` `reversed == n;` `}`   `// function to print sum of Palindromes` `void` `sumPalindrome(``int` `n, ``int` `k){` `    `  `    ``int` `sum = 0, i = 1;` `    `  `    ``int` `p = makePalindrome(i, ``true``);` `    `  `    ``// loop for odd generation of` `    ``// odd palindromes` `    ``while` `(p < n) {` `        ``if` `(isPalindrome(p, k))` `            ``sum += p;` `        ``i++;` `    `  `        ``// cout << p << " ";` `        ``p = makePalindrome(i, ``true``);` `    ``}` `    `  `    ``i = 1;`   `    ``// loop for generation of ` `    ``// even palindromes` `    ``p = makePalindrome(i, ``false``);` `    ``while` `(p < n) {` `        ``if` `(isPalindrome(p, k))` `            ``sum += p;` `        ``i++;` `        ``p = makePalindrome(i, ``false``);` `    ``}` `    `  `    ``// result of all palindromes in ` `    ``// both bases.` `    ``cout << ``"Total sum is "` `<< sum ` `         ``<< endl;` `}`   `// driver code` `int` `main()` `{` `    ``int` `n = 1000000, k = 2;` `    ``sumPalindrome(n ,k);` `    ``return` `0;` `}`

## Java

 `// Java Program for Checking double ` `// base Palindrome.`   `public` `class` `GFG {`   `// generates even and odd palindromes ` `    ``static` `int` `makePalindrome(``int` `n, ``boolean` `odd) {` `        ``int` `res = n;` `        ``if` `(odd) {` `            ``n = n / ``10``;` `        ``}` `        ``while` `(n > ``0``) {` `            ``res = ``10` `* res + n % ``10``;` `            ``n /= ``10``;` `        ``}` `        ``return` `res;` `    ``}`   `// Check if a number is palindrome ` `// in base k ` `    ``static` `boolean` `isPalindrome(``int` `n, ``int` `base) {` `        ``int` `reversed = ``0``;` `        ``int` `temp = n;` `        ``while` `(temp > ``0``) {` `            ``reversed = reversed * base` `                    ``+ temp % base;` `            ``temp /= base;` `        ``}` `        ``return` `reversed == n;` `    ``}`   `// function to print sum of Palindromes ` `    ``static` `void` `sumPalindrome(``int` `n, ``int` `k) {`   `        ``int` `sum = ``0``, i = ``1``;`   `        ``int` `p = makePalindrome(i, ``true``);`   `        ``// loop for odd generation of ` `        ``// odd palindromes ` `        ``while` `(p < n) {` `            ``if` `(isPalindrome(p, k)) {` `                ``sum += p;` `            ``}` `            ``i++;`   `            ``// cout << p << " "; ` `            ``p = makePalindrome(i, ``true``);` `        ``}`   `        ``i = ``1``;`   `        ``// loop for generation of ` `        ``// even palindromes ` `        ``p = makePalindrome(i, ``false``);` `        ``while` `(p < n) {` `            ``if` `(isPalindrome(p, k)) {` `                ``sum += p;` `            ``}` `            ``i++;` `            ``p = makePalindrome(i, ``false``);` `        ``}`   `        ``// result of all palindromes in ` `        ``// both bases. ` `        ``System.out.println(``"Total sum is "` `+ sum);` `    ``}`   `// driver code ` `    ``public` `static` `void` `main(String[] args) {` `        ``int` `n = ``1000000``, k = ``2``;` `        ``sumPalindrome(n, k);`   `    ``}` `}`

## Python3

 `# Python3 Program for Checking double ` `# base Palindrome.`   `# Function generates even and ` `# odd palindromes ` `def` `makePalindrome(n, odd):`   `    ``res ``=` `n;` `    ``if` `(odd):` `        ``n ``=` `int``(n ``/` `10``);` `    ``while` `(n > ``0``):` `        ``res ``=` `10` `*` `res ``+` `n ``%` `10``;` `        ``n ``=` `int``(n ``/` `10``);` `    ``return` `res;`   `# Check if a number is palindrome ` `# in base k ` `def` `isPalindrome(n, base):` `    ``reversed` `=` `0``;` `    ``temp ``=` `n;` `    ``while` `(temp > ``0``): ` `        ``reversed` `=` `reversed` `*` `base ``+` `temp ``%` `base;` `        ``temp ``=` `int``(temp ``/` `base);` `    `  `    ``return` `reversed` `=``=` `n;`   `# function to print sum of Palindromes ` `def` `sumPalindrome(n, k): `   `    ``sum` `=` `0``; ` `    ``i ``=` `1``;`   `    ``p ``=` `makePalindrome(i, ``True``);`   `    ``# loop for odd generation of ` `    ``# odd palindromes ` `    ``while` `(p < n):` `        ``if` `(isPalindrome(p, k)):` `            ``sum` `+``=` `p;` `        ``i ``+``=` `1``;`   `        ``p ``=` `makePalindrome(i, ``True``);`   `    ``i ``=` `1``;`   `    ``# loop for generation of ` `    ``# even palindromes ` `    ``p ``=` `makePalindrome(i, ``False``);` `    ``while` `(p < n):` `        ``if` `(isPalindrome(p, k)):` `            ``sum` `+``=` `p;` `        ``i ``+``=` `1``;` `        ``p ``=` `makePalindrome(i, ``False``);`   `    ``# result of all palindromes in ` `    ``# both bases. ` `    ``print``(``"Total sum is"``, ``sum``);`   `# Driver code ` `n ``=` `1000000``; ` `k ``=` `2``;` `sumPalindrome(n, k);`   `# This code is contributed by mits`

## C#

 `// C# Program for Checking double ` `// base1 Palindrome.`   `public` `class` `GFG {`   `// generates even and odd palindromes ` `    ``static` `int` `makePalindrome(``int` `n, ``bool` `odd) {` `        ``int` `res = n;` `        ``if` `(odd) {` `            ``n = n / 10;` `        ``}` `        ``while` `(n > 0) {` `            ``res = 10 * res + n % 10;` `            ``n /= 10;` `        ``}` `        ``return` `res;` `    ``}`   `// Check if a number is palindrome ` `// in base1 k ` `    ``static` `bool` `isPalindrome(``int` `n, ``int` `base1) {` `        ``int` `reversed = 0;` `        ``int` `temp = n;` `        ``while` `(temp > 0) {` `            ``reversed = reversed * base1 + temp % base1;` `            ``temp /= base1;` `        ``}` `        ``return` `reversed == n;` `    ``}`   `// function to print sum of Palindromes ` `    ``static` `void` `sumPalindrome(``int` `n, ``int` `k) {`   `        ``int` `sum = 0, i = 1;`   `        ``int` `p = makePalindrome(i, ``true``);`   `        ``// loop for odd generation of ` `        ``// odd palindromes ` `        ``while` `(p < n) {` `            ``if` `(isPalindrome(p, k)) {` `                ``sum += p;` `            ``}` `            ``i++;`   `            ``p = makePalindrome(i, ``true``);` `        ``}`   `        ``i = 1;`   `        ``// loop for generation of ` `        ``// even palindromes ` `        ``p = makePalindrome(i, ``false``);` `        ``while` `(p < n) {` `            ``if` `(isPalindrome(p, k)) {` `                ``sum += p;` `            ``}` `            ``i++;` `            ``p = makePalindrome(i, ``false``);` `        ``}`   `        ``// result of all palindromes in ` `        ``// both base1s. ` `        ``System.Console.WriteLine(``"Total sum is "` `+ sum);` `    ``}`   `// driver code ` `    ``public` `static` `void` `Main() {` `        ``int` `n = 1000000, k = 2;` `        ``sumPalindrome(n, k);`   `    ``}` `}`   `// This code is contributed by mits`

## PHP

 ` 0)` `    ``{` `        ``\$res` `= 10 * ``\$res` `+ ``\$n` `% 10;` `        ``\$n` `= (int)(``\$n` `/ 10);` `    ``}` `    ``return` `\$res``;` `}`   `// Check if a number is palindrome ` `// in base k ` `function` `isPalindrome(``\$n``, ``\$base``) ` `{` `    ``\$reversed` `= 0;` `    ``\$temp` `= ``\$n``;` `    ``while` `(``\$temp` `> 0) ` `    ``{` `        ``\$reversed` `= ``\$reversed` `* ``\$base` `+ ` `                        ``\$temp` `% ``\$base``;` `        ``\$temp` `= (int)(``\$temp` `/ ``\$base``);` `    ``}` `    ``return` `\$reversed` `== ``\$n``;` `}`   `// function to print sum of Palindromes ` `function` `sumPalindrome(``\$n``, ``\$k``) ` `{` `    ``\$sum` `= 0; ``\$i` `= 1;`   `    ``\$p` `= makePalindrome(``\$i``, true);`   `    ``// loop for odd generation of ` `    ``// odd palindromes ` `    ``while` `(``\$p` `< ``\$n``)` `    ``{` `        ``if` `(isPalindrome(``\$p``, ``\$k``))` `        ``{` `            ``\$sum` `+= ``\$p``;` `        ``}` `        ``\$i``++;`   `        ``\$p` `= makePalindrome(``\$i``, true);` `    ``}`   `    ``\$i` `= 1;`   `    ``// loop for generation of ` `    ``// even palindromes ` `    ``\$p` `= makePalindrome(``\$i``, false);` `    ``while` `(``\$p` `< ``\$n``)` `    ``{` `        ``if` `(isPalindrome(``\$p``, ``\$k``)) ` `        ``{` `            ``\$sum` `+= ``\$p``;` `        ``}` `        ``\$i``++;` `        ``\$p` `= makePalindrome(``\$i``, false);` `    ``}`   `    ``// result of all palindromes in ` `    ``// both bases. ` `    ``echo``(``"Total sum is "` `. ``\$sum``);` `}`   `// Driver code ` `\$n` `= 1000000; ``\$k` `= 2;` `sumPalindrome(``\$n``, ``\$k``);`   `// This code is contributed` `// by Mukul Singh. ` `?>`

## Javascript

 ``

Output

```Total sum is 872187
```

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

This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up
Related Articles