📌  相关文章
📜  给定以十进制为底的数字N,请以任意底数(以b为底)找到其位数

📅  最后修改于: 2021-05-04 19:00:19             🧑  作者: Mango

给定以10为底的数字n,请找到以b为底的数字位数。
限制条件: N \in \mathbb{W}  所有的
例子 :

Input : Number = 48 
        Base = 4
Output: 3
Explanation : (48)10 = (300)4

Input : Number = 1446
        Base = 7
Output: 4
Explanation : (446)10 = (4134)7

一种简单的方法:将十进制数转换为给定的基数r,然后计算位数。
一种有效的方法:它取决于数字的基数与该数字的位数之间的关系。
通常:令n为正整数。基地b  代表n  具有d  如果数字b^{d-1}\leq n < b^d  ,如果是这样的话d-1 \leq \log_b n < d  或者\lfloor log_b n \rfloor = d-1  因此,以n为底的b表示形式的位数是
\lfloor log_b N \rfloor + 1 = \left \lfloor \dfrac {ln N}{ln b} \right \rfloor + 1 = \left \lfloor \dfrac {log N}{log b} \right \rfloor + 1
在上面的方程中,使用了基本变化对数性质。因此,我们计算要计算位数的那个底数的对数。并取其底值,然后加1。
该想法可以进一步用于在基数r中找到基数b的给定数n的位数。所有要做的就是转换以10为底的数字,然后应用上面的查找数字的公式。当数字以10为底时,计算任何底数的对数会更容易。

C++
// C++ program to Find Number of digits
// in base b.
#include 
#include 
using namespace std;
 
// function to print number of
// digits
void findNumberOfDigits(long n, int base)
{
    // Calculating log using base
    // changing property and then
    // taking it floor and then
    // adding 1.
    int dig = (int)(floor( log(n) /
                         log(base)) + 1);
     
    // printing output
    cout << "The Number of digits of "
         << "Number " << n << " in base "
         << base << " is " << dig;
}
 
// Driver method
int main()
{
    // taking inputs
    long n = 1446;
    int base = 7;
     
    // calling the method
    findNumberOfDigits(n, base);
    return 0;
}
 
// This code is contributed by Manish Shaw
// (manishshaw1)


Java
// Java program to Find Number
// of digits in base b.
class GFG {
     
    // function to print number of digits
    static void findNumberOfDigits(long n, int base)
    {
         
        // Calculating log using base changing
        // property and then taking it
        // floor and then adding 1.
        int dig = (int)(Math.floor(
                        Math.log(n) / Math.log(base))
                        + 1);
         
         
        // printing output
        System.out.println("The Number of digits of Number "
                            + n + " in base " + base
                            + " is " + dig);
    }
 
    // Driver method   
    public static void main(String[] args)
    {
        // taking inputs
        long n = 1446;
        int base = 7;
         
        // calling the method
        findNumberOfDigits(n, base);
    }
}


Python3
# Python3 program to Find Number of digits
# in base b.
 
import math
 
# function to print number of
# digits
def findNumberOfDigits(n, base):
     
    # Calculating log using base
    # changing property and then
    # taking it floor and then
    # adding 1.
    dig = (math.floor(math.log(n) /
                 math.log(base)) + 1)
     
    # printing output
    print ("The Number of digits of"
      " Number {} in base {} is {}"
            . format(n, base, dig))
 
# Driver method
 
# taking inputs
n = 1446
base = 7
 
# calling the method
findNumberOfDigits(n, base)
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#
// C# program to Find Number of digits
// in base b.
using System;
 
class GFG {
     
    // function to print number of
    // digits
    static void findNumberOfDigits(long n,
                                    int b)
    {
        // Calculating log using base
        // changing property and then
        // taking it floor and then
        // adding 1.
        int dig = (int)(Math.Floor(
          Math.Log(n) / Math.Log(b)) + 1);
         
        // printing output
        Console.Write("The Number of digits"
           + " of Number " + n + " in base "
                        + b + " is " + dig);
    }
 
    // Driver method
    public static void Main()
    {
        // taking inputs
        long n = 1446;
        int b = 7;
         
        // calling the method
        findNumberOfDigits(n, b);
    }
}
 
// This code is contributed by Manish Shaw
// (manishshaw1)


PHP


Javascript


输出 :

The Number of digits of Number 1446 in base 7 is 4