📜  计算Log n的程序

📅  最后修改于: 2021-05-26 03:13:21             🧑  作者: Mango

编写一个单行C函数来计算并返回\log_2 n  。例如,如果n = 64,则您的函数应返回6;如果n = 128,则您的函数应返回7。

使用递归

C
// program to find log(n) using Recursion
#include 
 
unsigned int Log2n(unsigned int n)
{
    return (n > 1) ? 1 + Log2n(n / 2) : 0;
}
 
int main()
{
    unsigned int n = 32;
    printf("%u", Log2n(n));
    getchar();
    return 0;
}


Java
// Java program to find log(n)
// using Recursion
class Gfg1
{
 
    static int Log2n(int n)
    {
        return (n > 1) ? 1 + Log2n(n / 2) : 0;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 32;
        System.out.println(Log2n(n));
    }
}
 
// This code is contributed by Niraj_Pandey


Python3
# Python 3 program to
# find log(n) using Recursion
 
def Log2n(n):
 
    return 1 + Log2n(n / 2) if (n > 1) else 0
 
# Driver code
n = 32
print(Log2n(n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to find log(n)
// using Recursion
using System;
 
class GFG {
 
    static int Log2n(int n)
    {
        return (n > 1) ? 1 +
            Log2n(n / 2) : 0;
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 32;
         
        Console.Write(Log2n(n));
    }
}
 
// This code is contributed by
// nitin mittal.


Javascript


C
// C program to find log(n) using Inbuilt
// function of  library
#include 
#include 
int main()
{
    unsigned int n = 32;
    printf("%d", (int)log2(n));
    return 0;
}


Java
// Java program to find log(n) using Inbuilt
// function of java.util.Math library
import java.util.*;
 
class Gfg2
{
    public static void main(String args[])
    {
        int n = 32;
        System.out.println((int)(Math.log(n) / Math.log(2)));
    }
}
 
// This code is contributed by Niraj_Pandey


Javascript


C
// C program to find log(n) on arbitrary base using Recursion
#include 
 
unsigned int Logn(unsigned int n, unsigned int r)
{
    return (n > r - 1) ? 1 + Logn(n / r, r) : 0;
}
 
int main()
{
    unsigned int n = 256;
    unsigned int r = 3;
    printf("%u", Logn(n, r));
    return 0;
}


Java
// Java program to find log(n) on
// arbitrary base using Recursion
class Gfg3
{
    static int Logn(int n, int r)
    {
        return (n > r - 1) ? 1 + Logn(n / r, r) : 0;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 256;
        int r = 3;
        System.out.println(Logn(n, r));
    }
}
 
// This code is contributed by Niraj_Pandey


Javascript


C
// C program to find log(n) on arbitrary base
// using log() function of maths library
#include 
#include 
 
unsigned int Logn(unsigned int n, unsigned int r)
{
    return log(n) / log(r);
}
 
int main()
{
    unsigned int n = 256;
    unsigned int r = 3;
    printf("%u", Logn(n, r));
 
    return 0;
}


Java
// Java program to find log(n) on arbitrary base
// using log() function of java.util.Math library
import java.util.*;
 
class Gfg4 {
 
    public static void main(String args[])
    {
        int n = 256;
        int r = 3;
        System.out.println((int)(Math.log(n) / Math.log(r)));
    }
}
 
// This code is contributed by Niraj_Pandey


Javascript


输出 :

5

时间复杂度: O(log n)
辅助空间:如果在递归过程中考虑堆栈大小,则为O(log n),否则为O(1)

使用内置日志函数

我们可以使用标准库中的内置函数。

C

// C program to find log(n) using Inbuilt
// function of  library
#include 
#include 
int main()
{
    unsigned int n = 32;
    printf("%d", (int)log2(n));
    return 0;
}

Java

// Java program to find log(n) using Inbuilt
// function of java.util.Math library
import java.util.*;
 
class Gfg2
{
    public static void main(String args[])
    {
        int n = 32;
        System.out.println((int)(Math.log(n) / Math.log(2)));
    }
}
 
// This code is contributed by Niraj_Pandey

Java脚本


输出 :

5

时间复杂度: O(1)
辅助空间: O(1)

让我们尝试问题的扩展版本。

编写一个单行函数Logn(n,r)返回\lfloor\log_r n\rfloor

使用递归

C

// C program to find log(n) on arbitrary base using Recursion
#include 
 
unsigned int Logn(unsigned int n, unsigned int r)
{
    return (n > r - 1) ? 1 + Logn(n / r, r) : 0;
}
 
int main()
{
    unsigned int n = 256;
    unsigned int r = 3;
    printf("%u", Logn(n, r));
    return 0;
}

Java

// Java program to find log(n) on
// arbitrary base using Recursion
class Gfg3
{
    static int Logn(int n, int r)
    {
        return (n > r - 1) ? 1 + Logn(n / r, r) : 0;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 256;
        int r = 3;
        System.out.println(Logn(n, r));
    }
}
 
// This code is contributed by Niraj_Pandey

Java脚本


输出 :

5

时间复杂度: O(log n)
辅助空间:如果在递归过程中考虑堆栈大小,则为O(log n),否则为O(1)

使用内置日志函数

我们只需要使用logarithm属性在任意基r上找到log(n)的。 IE, \log_r n = \dfrac{log_k (n)}{\log_k (r)}   其中k可以是任何值,对于标准对数函数,其值为e10

C

// C program to find log(n) on arbitrary base
// using log() function of maths library
#include 
#include 
 
unsigned int Logn(unsigned int n, unsigned int r)
{
    return log(n) / log(r);
}
 
int main()
{
    unsigned int n = 256;
    unsigned int r = 3;
    printf("%u", Logn(n, r));
 
    return 0;
}

Java

// Java program to find log(n) on arbitrary base
// using log() function of java.util.Math library
import java.util.*;
 
class Gfg4 {
 
    public static void main(String args[])
    {
        int n = 256;
        int r = 3;
        System.out.println((int)(Math.log(n) / Math.log(r)));
    }
}
 
// This code is contributed by Niraj_Pandey

Java脚本


输出 :

5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”