📜  计算[L,R]范围内K的完美幂

📅  最后修改于: 2021-04-29 14:09:35             🧑  作者: Mango

给定三个整数L,R和K,任务是找到L到R之间的整数数目的计数,这是K的完美幂。即,形式为K的整数数目,其中一个可以是任何数字。
例子:

方法:我们的想法是分别以找到K的L和R的次方根,其中K一批次方根N是实数,让N,当我们把它提高到整数次幂N.然后是整数的计数K在L和R范围内的幂可以定义为–

Count = ( floor(Kthroot(R)) - ceil(Kthroot(L)) + 1 )

可以使用牛顿公式来计算数字N的K根,其中可以使用以下公式来计算i迭代–

下面是上述方法的实现:

C++
// C++ implementation to find the
// count of numbers those are
// powers of K in range L to R
 
#include 
using namespace std;
 
// Function to find the
// Nth root of the number
double nthRoot(int A, int N)
{
    // intially guessing a random
    // number between 0 to 9
    double xPre = rand() % 10;
 
    // Smaller eps,
    // denotes more accuracy
    double eps = 1e-3;
 
    // Initializing difference between two
    // roots by INT_MAX
    double delX = INT_MAX;
 
    // xK denotes
    // current value of x
    double xK;
 
    // loop untill we reach desired accuracy
    while (delX > eps) {
        // calculating current value
        // from previous value
        xK = ((N - 1.0) * xPre +
        (double)A / pow(xPre, N - 1))
                        / (double)N;
        delX = abs(xK - xPre);
        xPre = xK;
    }
    return xK;
}
 
// Function to count the perfect
// powers of K in range L to R
int countPowers(int a, int b, int k)
{
    return (floor(nthRoot(b, k)) -
           ceil(nthRoot(a, k)) + 1);
}
 
// Driver code
int main()
{
    int a = 7, b = 28, k = 2;
    cout << "Count of Powers is "
        << countPowers(a, b, k);
    return 0;
}


Java
// Java implementation to find the
// count of numbers those are
// powers of K in range L to R
class GFG{
  
// Function to find the
// Nth root of the number
static double nthRoot(int A, int N)
{
    // intially guessing a random
    // number between 0 to 9
    double xPre = Math.random()*10 % 10;
  
    // Smaller eps,
    // denotes more accuracy
    double eps = 1e-3;
  
    // Initializing difference between two
    // roots by Integer.MAX_VALUE
    double delX = Integer.MAX_VALUE;
  
    // xK denotes
    // current value of x
    double xK = 0;
  
    // loop untill we reach desired accuracy
    while (delX > eps)
    {
        // calculating current value
        // from previous value
        xK = ((N - 1.0) * xPre +
        (double)A / Math.pow(xPre, N - 1))
                        / (double)N;
        delX = Math.abs(xK - xPre);
        xPre = xK;
    }
    return xK;
}
  
// Function to count the perfect
// powers of K in range L to R
static int countPowers(int a, int b, int k)
{
    return (int) (Math.floor(nthRoot(b, k)) -
           Math.ceil(nthRoot(a, k)) + 1);
}
  
// Driver code
public static void main(String[] args)
{
    int a = 7, b = 28, k = 2;
    System.out.print("Count of Powers is "
        + countPowers(a, b, k));
}
}
 
// This code is contributed by 29AjayKumar


Python 3
# Python 3 implementation to find the
# count of numbers those are
# powers of K in range L to R
import sys
from math import pow,ceil,floor
import random
 
# Function to find the
# Nth root of the number
def nthRoot(A,N):
 
    # intially guessing a random
    # number between 0 to 9
    xPre = (random.randint(0, 9))%10
 
    # Smaller eps,
    # denotes more accuracy
    eps = 1e-3
 
    # Initializing difference between two
    # roots by INT_MAX
    delX = sys.maxsize
 
    # xK denotes
    # current value of x
 
    # loo3p untill we reach desired accuracy
    while (delX > eps):
         
        # calculating current value
        # from previous value
        xK = ((N - 1.0) * xPre + A / pow(xPre, N - 1))/ N
        delX = abs(xK - xPre)
        xPre = xK
    return xK
 
# Function to count the perfect
# powers of K in range L to R
def countPowers(a, b, k):
    return (floor(nthRoot(b, k)) - ceil(nthRoot(a, k)) + 1)
 
# Driver code
if __name__ == '__main__':
    a = 7
    b = 28
    k = 2
    print("Count of Powers is",countPowers(a, b, k))
     
# This code is contributed by Surendra_Gangwar


Javascript


输出
Count of Powers is 3

性能分析:

  • 时间复杂度:与上述方法一样,有两个函数调用来查找需要O(logN)时间的数字的N根,因此时间复杂度将为O(logN)
  • 空间复杂度:与上述方法一样,没有使用额外的空间,因此空间复杂度将为O(1)