📜  竞争编程的对数技巧

📅  最后修改于: 2021-05-20 07:55:22             🧑  作者: Mango

对数是幂运算的反函数,这意味着给定数字x的对数值是另一个数的指数。
log_b(x) = y, \to b^{y} = x
以下是使用对数函数的一些技巧,这些技巧在竞争性编程中非常有用。

检查数字是否为2的幂:

给定整数N ,任务是检查数字N是否为2的幂。
例子:

方法:一种简单的方法是简单地以2为底的数字取对数,如果得到整数,则该数字为2的幂。
下面是上述方法的实现:

C++
// C++ implementation to check that
// a integer is a power of Two
 
#include 
 
using namespace std;
 
// Function to check if the number
// is a power of two
bool isPowerOfTwo(int n)
{
    return (ceil(log2(n)) == floor(log2(n)));
}
 
// Driver Code
int main()
{
    int N = 8;
 
    if (isPowerOfTwo(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


C
// C implementation to check that
// a integer is a power of Two
#include 
#include 
 
// Function to check if the number
// is a power of two
_Bool isPowerOfTwo(int n)
{
    return (ceil(log2(n)) == floor(log2(n)));
}
 
// Driver Code
int main()
{
    int N = 8;
 
    if (isPowerOfTwo(N))
    {
        printf("Yes");
    }
    else
    {
        printf("No");
    }
}
 
// This code is contributed by vikas_g


Java
// Java implementation to check that
// a integer is a power of Two
import java.lang.Math;
 
class GFG{
     
// Function to check if the number
// is a power of two
public static boolean isPowerOfTwo(int n)
{
    return(Math.ceil(Math.log(n) /
                     Math.log(2)) ==
          Math.floor(Math.log(n) /
                     Math.log(2)));
}
     
// Driver Code
public static void main(String[] args)
{
    int N = 8;
 
    if (isPowerOfTwo(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to check that
# a integer is a power of two
import math
 
# Function to check if the number
# is a power of two            
def isPowerOfTwo(n):
     
    return(math.ceil(math.log(n) //
                     math.log(2)) ==
           math.floor(math.log(n) //
                      math.log(2)));
                     
# Driver code
if __name__=='__main__':
     
    N = 8
     
    if isPowerOfTwo(N):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by rutvik_56


C#
// C# implementation to check that
// a integer is a power of Two
using System;
 
class GFG{
     
// Function to check if the number
// is a power of two
public static bool isPowerOfTwo(int n)
{
    return(Math.Ceiling(Math.Log(n) /
                        Math.Log(2)) ==
             Math.Floor(Math.Log(n) /
                        Math.Log(2)));
}
     
// Driver Code
public static void Main(String[] args)
{
    int N = 8;
 
    if (isPowerOfTwo(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ implementation to find
// Kth root of the number
 
#include 
 
using namespace std;
 
// Function to find the
// Kth root of the number
double kthRoot(double n, int k)
{
    return pow(k,
               (1.0 / k)
                   * (log(n)
                      / log(k)));
}
 
// Driver Code
int main()
{
    double N = 8.0;
    int K = 3;
 
    cout << kthRoot(N, K);
 
    return 0;
}


Java
// Java implementation to find
// Kth root of the number
class GFG{
 
// Function to find the
// Kth root of the number
static double kthRoot(double n, int k)
{
    return Math.pow(k, (1.0 / k) *
                    (Math.log(n) / Math.log(k)));
}
 
// Driver Code
public static void main(String[] args)
{
    double N = 8.0;
    int K = 3;
 
    System.out.print(kthRoot(N, K));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 implementation to find
# Kth root of the number
import math
 
# Function to find the
# Kth root of the number
def kth_root(n, k):
     
    return(pow(k, ((1.0 / k) * (math.log(n) /
                                math.log(k)))))
 
# Driver code
if __name__=="__main__":
     
    n = 8.0
    k = 3
     
    print(round(kth_root(n, k)))
 
# This code is contributed by dipesh99kumar


C#
// C# implementation to find
// Kth root of the number
using System;
 
// Function to find the
// Kth root of the number
class GFG{
     
static double kthRoot(double n, int k)
{
    return Math.Pow(k, (1.0 / k) *
                   (Math.Log(n) / Math.Log(k)));
}
 
// Driver Code
public static void Main()
{
    double N = 8.0;
    int K = 3;
 
    Console.Write(kthRoot(N, K));
}
}
 
// This code is contributed by vikas_g


Javascript


C++
// C++ implementation count the
// number of digits in a number
 
#include 
 
using namespace std;
 
// Function to count the
// number of digits in a number
int countDigit(long long n)
{
    return floor(log10(n) + 1);
}
 
// Driver Code
int main()
{
    double N = 80;
 
    cout << countDigit(N);
 
    return 0;
}


C
// C implementation count the
// number of digits in a number
#include 
#include 
 
// Function to count the
// number of digits in a number
int countDigit(long long n)
{
    return (floor(log10(n) + 1));
}
 
// Driver Code
int main()
{
    double N = 80;
 
    printf("%d", countDigit(N));
 
    return 0;
}
 
// This code is contributed by vikas_g


Java
// Java implementation to count the
// number of digits in a number
class GFG{
 
// Function to count the
// number of digits in a number
static int countDigit(double n)
{
    return((int)Math.floor(Math.log10(n) + 1));
 
}
 
// Driver Code
public static void main(String[] args)
{
    double N = 80;
     
    System.out.println(countDigit(N));
}
}
 
// This code is contributed by vikas_g


Python3
# Python3 implementation count the
# number of digits in a number
import math
 
# Function to count the
# number of digits in a number
def countDigit(n):
     
    return(math.floor(math.log10(n) + 1))
 
# Driver code
if __name__=="__main__":
     
    n = 80
 
    print(countDigit(n))
 
# This code is contributed by dipesh99kumar


C#
// C# implementation count the
// number of digits in a number
using System;
 
// Function to count the
// number of digits in a number
class GFG{
     
static int countDigit(double n)
{
    return((int)Math.Floor(Math.Log10(n) + 1));
}
 
// Driver Code
public static void Main()
{
    double N = 80;
     
    Console.Write(countDigit(N));
}
}
 
// This code is contributed by vikas_g


C++
// C++ implementation to check if
// the number is power of K
 
#include 
 
using namespace std;
 
// Function to check if
// the number is power of K
bool isPower(int N, int K)
{
    // logarithm function to
    // calculate value
    int res1 = log(N) / log(K);
    double res2 = log(N) / log(K);
 
    // compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
int main()
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}


C
// C implementation to check if
// the number is power of K
#include 
#include 
 
// Function to check if
// the number is power of K
_Bool isPower(int N, int K)
{
     
    // Logarithm function to
    // calculate value
    int res1 = log(N) / log(K);
    double res2 = log(N) / log(K);
 
    // Compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
int main()
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K))
    {
        printf("Yes");
    }
    else
    {
        printf("No");
    }
    return 0;
}
 
// This code is contributed by vikas_g


Java
// Java implementation to check if
// the number is power of K
class GFG{
 
// Function to check if
// the number is power of K
static boolean isPower(int N, int K)
{
     
    // Logarithm function to
    // calculate value
    int res1 = (int)(Math.log(N) / Math.log(K));
    double res2 = Math.log(N) / Math.log(K);
 
    // Compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by vikas_g


Python3
# Python3 implementation to check if a
# number is a power of the other number
from math import log
 
# Function to check if
# the number is power of K
def isPower(n, k):
     
    # Logarithm function to
    # calculate value
    res1 = int(log(n) / log(k))
    res2 = log(n) / log(k)
     
    # Compare to the result1
    # or result2 both are equal
    return(res1 == res2)
 
# Driver code
if __name__=="__main__":
     
    n = 8
    k = 2
     
    if (isPower(n, k)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by dipesh99kumar


C#
// C# implementation to check if
// the number is power of K
using System;
 
// Function to count the
// number of digits in a number
class GFG{
     
static bool isPower(int N, int K)
{
     
    // Logarithm function to
    // calculate value
    int res1 = (int)(Math.Log(N) / Math.Log(K));
    double res2 = Math.Log(N) / Math.Log(K);
 
    // Compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
public static void Main()
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by vikas_g


C++
// C++ implementation to find the
// previous and next power of K
 
#include 
 
using namespace std;
 
// Function to return the highest power
// of k less than or equal to n
int prevPowerofK(int n, int k)
{
    int p = (int)(log(n) / log(k));
    return (int)pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
}
 
// Driver Code
int main()
{
    int N = 7;
    int K = 2;
 
    cout << prevPowerofK(N, K) << " ";
 
    cout << nextPowerOfK(N, K) << endl;
    return 0;
}


C
// C implementation to find the
// previous and next power of K
#include 
#include 
 
// Function to return the highest power
// of k less than or equal to n
int prevPowerofK(int n, int k)
{
    int p = (int)(log(n) / log(k));
    return (int)pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
}
 
// Driver Code
int main()
{
    int N = 7;
    int K = 2;
 
    printf("%d ", prevPowerofK(N, K));
    printf("%d\n", nextPowerOfK(N, K));
     
    return 0;
}
 
// This code is contributed by vikas_g


Java
// Java implementation to find the
// previous and next power of K
class GFG{
 
// Function to return the highest power
// of k less than or equal to n
static int prevPowerofK(int n, int k)
{
    int p = (int)(Math.log(n) / Math.log(k));
    return (int)Math.pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
static int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 7;
    int K = 2;
     
    System.out.print(prevPowerofK(N, K) + " ");
    System.out.println(nextPowerOfK(N, K));
}
}
 
// This code is contributed by vikas_g


Python3
# Python3 implementation to find the
# previous and next power of K
from math import log
 
# Function to return the highest power
# of k less than or equal to n
def prevPowerofK(n, k):
     
    p = (int)(log(n) / log(k));
    return pow(k, p);
 
# Function to return the smallest power
# of k greater than or equal to n
def nextPowerOfK(n, k):
     
    return prevPowerofK(n, k) * k;
 
# Driver Code
if __name__=="__main__":
     
    N = 7
    K = 2
     
    print(prevPowerofK(N, K), end = " ")
    print(nextPowerOfK(N, K))
 
# This code is contributed by dipesh99kumar


C#
// C# implementation to find the
// previous and next power of K
using System;
 
// Function to count the
// number of digits in a number
class GFG{
     
// Function to return the highest power
// of k less than or equal to n
static int prevPowerofK(int n, int k)
{
    int p = (int)(Math.Log(n) / Math.Log(k));
    return (int)Math.Pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
static int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
 
}
 
// Driver Code
public static void Main()
{
    int N = 7;
    int K = 2;
     
    Console.Write(prevPowerofK(N, K) + " ");
    Console.Write(nextPowerOfK(N, K));
}
}
 
// This code is contributed by vikas_g


C++
// C++ implementation to find the
// rightmost set bit
 
#include 
 
using namespace std;
 
// Function to find the rightmost
// bit set of the integer N
unsigned int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Driver Code
int main()
{
    int N = 8;
 
    cout << getFirstSetBitPos(N);
    return 0;
}


C
// C implementation to find the
// rightmost set bit
#include 
#include 
 
// Function to find the rightmost
// bit set of the integer N
unsigned int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Driver Code
int main()
{
    int N = 8;
 
    printf("%d", getFirstSetBitPos(N));
    return 0;
}
 
// This code is contributed by vikas_g


Java
// Java implementation to find the
// rightmost set bit
class GFG{
 
// Function to find the rightmost
// bit set of the integer N
static int getFirstSetBitPos(int n)
{
    return (int)(Math.log(n & -n) /
                 Math.log(2)) + 1;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8;
 
    System.out.println(getFirstSetBitPos(N));
}
}
 
// This code is contributed by vikas_g


Python3
# Python3 implementation to find the
# rightmost set bit
import math
 
# Function to find the rightmost
# bit set of the integer N
def getFirstSetBitPos(n):
 
    return math.log2(n & -n) + 1;
 
# Driver Code
if __name__=="__main__":
     
    N = 8
 
    print(int(getFirstSetBitPos(N)))
 
# This code is contributed by dipesh99kumar


C#
// C# implementation to find the
// rightmost set bit
using System;
 
class GFG{
 
// Function to find the rightmost
// bit set of the integer N
static int getFirstSetBitPos(int n)
{
    return (int)(Math.Log(n & -n) /
                 Math.Log(2)) + 1;
}
 
// Driver Code
public static void Main()
{
    int N = 8;
 
    Console.Write(getFirstSetBitPos(N));
}
}
 
// This code is contributed by vikas_g


输出:
Yes

数字的Kth根

给定两个整数NK ,任务是找到数字N的K根。
例子:

方法:一种简单的解决方案是使用对数函数来找到数字的第K根。下面是该方法的说明:

下面是上述方法的实现:

C++

// C++ implementation to find
// Kth root of the number
 
#include 
 
using namespace std;
 
// Function to find the
// Kth root of the number
double kthRoot(double n, int k)
{
    return pow(k,
               (1.0 / k)
                   * (log(n)
                      / log(k)));
}
 
// Driver Code
int main()
{
    double N = 8.0;
    int K = 3;
 
    cout << kthRoot(N, K);
 
    return 0;
}

Java

// Java implementation to find
// Kth root of the number
class GFG{
 
// Function to find the
// Kth root of the number
static double kthRoot(double n, int k)
{
    return Math.pow(k, (1.0 / k) *
                    (Math.log(n) / Math.log(k)));
}
 
// Driver Code
public static void main(String[] args)
{
    double N = 8.0;
    int K = 3;
 
    System.out.print(kthRoot(N, K));
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 implementation to find
# Kth root of the number
import math
 
# Function to find the
# Kth root of the number
def kth_root(n, k):
     
    return(pow(k, ((1.0 / k) * (math.log(n) /
                                math.log(k)))))
 
# Driver code
if __name__=="__main__":
     
    n = 8.0
    k = 3
     
    print(round(kth_root(n, k)))
 
# This code is contributed by dipesh99kumar

C#

// C# implementation to find
// Kth root of the number
using System;
 
// Function to find the
// Kth root of the number
class GFG{
     
static double kthRoot(double n, int k)
{
    return Math.Pow(k, (1.0 / k) *
                   (Math.Log(n) / Math.Log(k)));
}
 
// Driver Code
public static void Main()
{
    double N = 8.0;
    int K = 3;
 
    Console.Write(kthRoot(N, K));
}
}
 
// This code is contributed by vikas_g

Java脚本


输出:
2

计算数字中的数字:

给定整数N ,任务是对数字N中的数字进行计数。
例子:

方法:想法是找到以10为底的对数以计算位数。
下面是上述方法的实现:

C++

// C++ implementation count the
// number of digits in a number
 
#include 
 
using namespace std;
 
// Function to count the
// number of digits in a number
int countDigit(long long n)
{
    return floor(log10(n) + 1);
}
 
// Driver Code
int main()
{
    double N = 80;
 
    cout << countDigit(N);
 
    return 0;
}

C

// C implementation count the
// number of digits in a number
#include 
#include 
 
// Function to count the
// number of digits in a number
int countDigit(long long n)
{
    return (floor(log10(n) + 1));
}
 
// Driver Code
int main()
{
    double N = 80;
 
    printf("%d", countDigit(N));
 
    return 0;
}
 
// This code is contributed by vikas_g

Java

// Java implementation to count the
// number of digits in a number
class GFG{
 
// Function to count the
// number of digits in a number
static int countDigit(double n)
{
    return((int)Math.floor(Math.log10(n) + 1));
 
}
 
// Driver Code
public static void main(String[] args)
{
    double N = 80;
     
    System.out.println(countDigit(N));
}
}
 
// This code is contributed by vikas_g

Python3

# Python3 implementation count the
# number of digits in a number
import math
 
# Function to count the
# number of digits in a number
def countDigit(n):
     
    return(math.floor(math.log10(n) + 1))
 
# Driver code
if __name__=="__main__":
     
    n = 80
 
    print(countDigit(n))
 
# This code is contributed by dipesh99kumar

C#

// C# implementation count the
// number of digits in a number
using System;
 
// Function to count the
// number of digits in a number
class GFG{
     
static int countDigit(double n)
{
    return((int)Math.Floor(Math.Log10(n) + 1));
}
 
// Driver Code
public static void Main()
{
    double N = 80;
     
    Console.Write(countDigit(N));
}
}
 
// This code is contributed by vikas_g
输出:
2

检查N是否为K的幂:

给定两个整数NK ,任务是检查Y是否为X的幂。
例子:

方法:想法是取N为底的K的对数。如果结果是整数,则N为K的幂。
下面是上述方法的实现:

C++

// C++ implementation to check if
// the number is power of K
 
#include 
 
using namespace std;
 
// Function to check if
// the number is power of K
bool isPower(int N, int K)
{
    // logarithm function to
    // calculate value
    int res1 = log(N) / log(K);
    double res2 = log(N) / log(K);
 
    // compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
int main()
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}

C

// C implementation to check if
// the number is power of K
#include 
#include 
 
// Function to check if
// the number is power of K
_Bool isPower(int N, int K)
{
     
    // Logarithm function to
    // calculate value
    int res1 = log(N) / log(K);
    double res2 = log(N) / log(K);
 
    // Compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
int main()
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K))
    {
        printf("Yes");
    }
    else
    {
        printf("No");
    }
    return 0;
}
 
// This code is contributed by vikas_g

Java

// Java implementation to check if
// the number is power of K
class GFG{
 
// Function to check if
// the number is power of K
static boolean isPower(int N, int K)
{
     
    // Logarithm function to
    // calculate value
    int res1 = (int)(Math.log(N) / Math.log(K));
    double res2 = Math.log(N) / Math.log(K);
 
    // Compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by vikas_g

Python3

# Python3 implementation to check if a
# number is a power of the other number
from math import log
 
# Function to check if
# the number is power of K
def isPower(n, k):
     
    # Logarithm function to
    # calculate value
    res1 = int(log(n) / log(k))
    res2 = log(n) / log(k)
     
    # Compare to the result1
    # or result2 both are equal
    return(res1 == res2)
 
# Driver code
if __name__=="__main__":
     
    n = 8
    k = 2
     
    if (isPower(n, k)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by dipesh99kumar

C#

// C# implementation to check if
// the number is power of K
using System;
 
// Function to count the
// number of digits in a number
class GFG{
     
static bool isPower(int N, int K)
{
     
    // Logarithm function to
    // calculate value
    int res1 = (int)(Math.Log(N) / Math.Log(K));
    double res2 = Math.Log(N) / Math.Log(K);
 
    // Compare to the result1
    // or result2 both are equal
    return (res1 == res2);
}
 
// Driver Code
public static void Main()
{
    int N = 8;
    int K = 2;
 
    if (isPower(N, K))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by vikas_g
输出:
Yes

要找到大于等于N且小于N的K的幂,请执行以下操作:

给定两个整数NK ,任务是找到K的大于等于N和小于N的幂。
例子:

方法:想法是找到给定整数N的log K值的下限值,然后计算该数字的K次方,以计算前K次方和后K次方
下面是上述方法的实现:

C++

// C++ implementation to find the
// previous and next power of K
 
#include 
 
using namespace std;
 
// Function to return the highest power
// of k less than or equal to n
int prevPowerofK(int n, int k)
{
    int p = (int)(log(n) / log(k));
    return (int)pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
}
 
// Driver Code
int main()
{
    int N = 7;
    int K = 2;
 
    cout << prevPowerofK(N, K) << " ";
 
    cout << nextPowerOfK(N, K) << endl;
    return 0;
}

C

// C implementation to find the
// previous and next power of K
#include 
#include 
 
// Function to return the highest power
// of k less than or equal to n
int prevPowerofK(int n, int k)
{
    int p = (int)(log(n) / log(k));
    return (int)pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
}
 
// Driver Code
int main()
{
    int N = 7;
    int K = 2;
 
    printf("%d ", prevPowerofK(N, K));
    printf("%d\n", nextPowerOfK(N, K));
     
    return 0;
}
 
// This code is contributed by vikas_g

Java

// Java implementation to find the
// previous and next power of K
class GFG{
 
// Function to return the highest power
// of k less than or equal to n
static int prevPowerofK(int n, int k)
{
    int p = (int)(Math.log(n) / Math.log(k));
    return (int)Math.pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
static int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 7;
    int K = 2;
     
    System.out.print(prevPowerofK(N, K) + " ");
    System.out.println(nextPowerOfK(N, K));
}
}
 
// This code is contributed by vikas_g

Python3

# Python3 implementation to find the
# previous and next power of K
from math import log
 
# Function to return the highest power
# of k less than or equal to n
def prevPowerofK(n, k):
     
    p = (int)(log(n) / log(k));
    return pow(k, p);
 
# Function to return the smallest power
# of k greater than or equal to n
def nextPowerOfK(n, k):
     
    return prevPowerofK(n, k) * k;
 
# Driver Code
if __name__=="__main__":
     
    N = 7
    K = 2
     
    print(prevPowerofK(N, K), end = " ")
    print(nextPowerOfK(N, K))
 
# This code is contributed by dipesh99kumar

C#

// C# implementation to find the
// previous and next power of K
using System;
 
// Function to count the
// number of digits in a number
class GFG{
     
// Function to return the highest power
// of k less than or equal to n
static int prevPowerofK(int n, int k)
{
    int p = (int)(Math.Log(n) / Math.Log(k));
    return (int)Math.Pow(k, p);
}
 
// Function to return the smallest power
// of k greater than or equal to n
static int nextPowerOfK(int n, int k)
{
    return prevPowerofK(n, k) * k;
 
}
 
// Driver Code
public static void Main()
{
    int N = 7;
    int K = 2;
     
    Console.Write(prevPowerofK(N, K) + " ");
    Console.Write(nextPowerOfK(N, K));
}
}
 
// This code is contributed by vikas_g
输出:
4 8

查找最右置位的位置:

给定整数N ,任务是找到最右置位的位置。
例子:

方法:

  • 取给定数字的二进制补码,因为除第一个“ 1”从右到左外,所有位都被还原(0111)
  • 按位进行,并使用原始编号,这将仅用所需的编号返回编号(0100)
  • 取no的log2,您将得到(位置– 1)(2)

下面是上述方法的实现:

C++

// C++ implementation to find the
// rightmost set bit
 
#include 
 
using namespace std;
 
// Function to find the rightmost
// bit set of the integer N
unsigned int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Driver Code
int main()
{
    int N = 8;
 
    cout << getFirstSetBitPos(N);
    return 0;
}

C

// C implementation to find the
// rightmost set bit
#include 
#include 
 
// Function to find the rightmost
// bit set of the integer N
unsigned int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Driver Code
int main()
{
    int N = 8;
 
    printf("%d", getFirstSetBitPos(N));
    return 0;
}
 
// This code is contributed by vikas_g

Java

// Java implementation to find the
// rightmost set bit
class GFG{
 
// Function to find the rightmost
// bit set of the integer N
static int getFirstSetBitPos(int n)
{
    return (int)(Math.log(n & -n) /
                 Math.log(2)) + 1;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8;
 
    System.out.println(getFirstSetBitPos(N));
}
}
 
// This code is contributed by vikas_g

Python3

# Python3 implementation to find the
# rightmost set bit
import math
 
# Function to find the rightmost
# bit set of the integer N
def getFirstSetBitPos(n):
 
    return math.log2(n & -n) + 1;
 
# Driver Code
if __name__=="__main__":
     
    N = 8
 
    print(int(getFirstSetBitPos(N)))
 
# This code is contributed by dipesh99kumar

C#

// C# implementation to find the
// rightmost set bit
using System;
 
class GFG{
 
// Function to find the rightmost
// bit set of the integer N
static int getFirstSetBitPos(int n)
{
    return (int)(Math.Log(n & -n) /
                 Math.Log(2)) + 1;
}
 
// Driver Code
public static void Main()
{
    int N = 8;
 
    Console.Write(getFirstSetBitPos(N));
}
}
 
// This code is contributed by vikas_g
输出:
4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。