📌  相关文章
📜  包含2位数字的最小幂

📅  最后修改于: 2021-04-25 00:16:16             🧑  作者: Mango

给定整数N ,任务是找到2的最小幂,该幂由N个数字组成。

例子:

天真的方法:一个简单的解决方案是遍历2的所有幂,从2 0开始,并检查2的每个幂,是否包含N位数字。打印包含N位数字的2的第一个幂。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to return smallest
// power of 2 with N digits
int smallestNum(int n)
{
    int res = 1;
 
    // Iterate through all
    // powers of 2
    for (int i = 2;; i *= 2) {
        int length = log10(i) + 1;
        if (length == n)
            return log(i) / log(2);
    }
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << smallestNum(n);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to return smallest
// power of 2 with N digits
static int smallestNum(int n)
{
    int res = 1;
 
    // Iterate through all
    // powers of 2
    for(int i = 2;; i *= 2)
    {
        int length = (int)(Math.log10(i)) + 1;
         
        if (length == n)
            return (int)(Math.log(i) /
                         Math.log(2));
    }
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
     
    System.out.print(smallestNum(n));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
from math import log10, log
 
# Function to return smallest
# power of 2 with N digits
def smallestNum(n):
 
    res = 1
 
    # Iterate through all
    # powers of 2
    i = 2
    while (True):
        length = int(log10(i) + 1)
         
        if (length == n):
            return int(log(i) // log(2))
             
        i *= 2
 
# Driver Code
n = 4
 
print(smallestNum(n))
 
# This code is contributed by SHIVAMSINGH67


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to return smallest
// power of 2 with N digits
static int smallestNum(int n)
{
    //int res = 1;
 
    // Iterate through all
    // powers of 2
    for(int i = 2;; i *= 2)
    {
        int length = (int)(Math.Log10(i)) + 1;
         
        if (length == n)
            return (int)(Math.Log(i) /
                         Math.Log(2));
    }
}
 
// Driver Code
public static void Main ()
{
    int n = 4;
     
    Console.Write(smallestNum(n));
}
}
 
// This code is contributed by code_hunt


Javascript


C++
// C++ Program of the
// above approach
 
#include 
using namespace std;
 
// Function to return smallest
// power of 2 consisitng of N digits
int smallestNum(int n)
{
    float power = log2(10);
  cout<


Java
// Java Program of the
// above approach
class GFG{
 
  // Function to return smallest
  // power of 2 consisitng of N digits
  static int smallestNum(int n)
  {
    double power = log2(10);
    return (int) Math.ceil((n - 1) * power);
  }
 
  static double log2(int N)
  {
    // calculate log2 N indirectly
    // using log() method
    return (Math.log(N) / Math.log(2));
}
 
// Driver Code
public static void main(String[] args)
{
  int n = 4;
  System.out.print(smallestNum(n));
 
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program of the
# above approach
from math import log2, ceil
 
# Function to return smallest
# power of 2 with N digits
def smallestNum(n):
 
    power = log2(10)
    print(power);
    return ceil((n - 1) * power)
 
# Driver Code
n = 4
 
print(smallestNum(n))
 
# This code is contributed by SHIVAMSINGH67


C#
// C# program of the
// above approach
using System;
class GFG{
 
// Function to return smallest
// power of 2 consisitng of N digits
static int smallestNum(int n)
{
  double power = log2(10);
  return (int) Math.Ceiling((n - 1) * power);
}
 
static double log2(int N)
{
  // calculate log2 N indirectly
  // using log() method
  return (Math.Log(N) / Math.Log(2));
}
 
// Driver Code
public static void Main()
{
  int n = 4;
  Console.Write(smallestNum(n));
}
}
 
// This code is contributed by Chitranayal


Javascript


输出:
10

高效方法:优化上述方法的主要观察结果是,可以通过以下公式获得N位数为2的最小幂:

\lceil (N-1)*\log_2 10 \rceil

下面是上述方法的实现:

C++

// C++ Program of the
// above approach
 
#include 
using namespace std;
 
// Function to return smallest
// power of 2 consisitng of N digits
int smallestNum(int n)
{
    float power = log2(10);
  cout<

Java

// Java Program of the
// above approach
class GFG{
 
  // Function to return smallest
  // power of 2 consisitng of N digits
  static int smallestNum(int n)
  {
    double power = log2(10);
    return (int) Math.ceil((n - 1) * power);
  }
 
  static double log2(int N)
  {
    // calculate log2 N indirectly
    // using log() method
    return (Math.log(N) / Math.log(2));
}
 
// Driver Code
public static void main(String[] args)
{
  int n = 4;
  System.out.print(smallestNum(n));
 
}
}
 
// This code is contributed by Princi Singh

Python3

# Python3 program of the
# above approach
from math import log2, ceil
 
# Function to return smallest
# power of 2 with N digits
def smallestNum(n):
 
    power = log2(10)
    print(power);
    return ceil((n - 1) * power)
 
# Driver Code
n = 4
 
print(smallestNum(n))
 
# This code is contributed by SHIVAMSINGH67

C#

// C# program of the
// above approach
using System;
class GFG{
 
// Function to return smallest
// power of 2 consisitng of N digits
static int smallestNum(int n)
{
  double power = log2(10);
  return (int) Math.Ceiling((n - 1) * power);
}
 
static double log2(int N)
{
  // calculate log2 N indirectly
  // using log() method
  return (Math.Log(N) / Math.Log(2));
}
 
// Driver Code
public static void Main()
{
  int n = 4;
  Console.Write(smallestNum(n));
}
}
 
// This code is contributed by Chitranayal

Java脚本


输出:
10

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