📜  双基地回文

📅  最后修改于: 2021-04-24 22:12:33             🧑  作者: Mango

顾名思义,双碱基回文报是一个以2个碱基为单位的回文数。一个底数是10,即十进制,另一个底数是k(可以是2或其他)。
注意:回文数(以任何一个为底)可能不包含前导零。
示例:十进制数585 = 1001001001 2 (二进制)在两个碱基中都是回文的。
回文报是一个单词,词组,数字或其他字符序列,向后或向前读相同,例如madam或12321。

求出以n为底数和以k为底数的所有小于n的数字之和。

例子:

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

方法1:此方法很简单。对于每个小于n的数字:

  • 检查它是否是以10为底的回文
  • 如果是,则将其转换为基数k
  • 检查是否在基数k中为palindrom
  • 如果是,则将其相加。

这种方法相当冗长,因为它会检查每个数字是否是回文。因此,对于最大为1000000的数字,它将检查每个数字。
如果k = 2,则以2为底的回文数只能是奇数,这可能会使比较减少到1000000/2 = 500000(仍然很大)。

下面是上述方法的实现:

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


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. 
?>


输出:

Total sum is 157

方法2:此方法的理解有点复杂,但比方法1更先进。而不是检查回文法是否有两个碱基。该方法在给定范围内产生回文。
假设我们的底数为k的回文形式为123321 ,则前3个数字定义了回文。然而,这三个数字123也定义回文12321 。因此,3位数123定义了5位数的回文和6位数的回文。由此得出,每个小于k n的正数都会产生两个小于k 2n的回文。这适用于每个基数k。示例:假设k = 10,即十进制。那么对于n = 1,所有小于10 n的数在10 2n中具有2个回文数,1个偶数长度和1个奇数长度。这些是1、11或2、22或3、33,依此类推。因此,对于1000000,我们生成大约2000,对于10 8,我们生成大约20000回文。

  • 从i = 1开始,并生成奇数回文。
  • 检查生成的奇数回文是否也是以k为底的回文
  • 如果是,则将该数字相加。
  • 通过更改i = i + 1重复上述三个步骤,直到最后生成的奇数回文数超过极限。
  • 现在,再次从i = 1开始,甚至生成回文。
  • 检查此生成的回文是否也是基数为k的回文
  • 如果是,则将该数字相加。
  • 通过更改i = i + 1重复上述三个步骤,直到最后生成的偶数回文数超过限制。

下面是上述方法的实现:

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. 
?>

输出:

Total sum is 872187