📜  给定数字n,找到n ^ n的前k个数字

📅  最后修改于: 2021-04-23 20:01:45             🧑  作者: Mango

给定数字n,请找到n n的前k个数字,其中k是小于n n中数字位数的值
例子 :

Input :  n = 10
          k = 2
 Output : 10
The first 2 digits in 1010 are 10.

 Input :  n = 144
          k = 6
 Output : 637087 

 Input:  n = 1250
         k = 5
 Output:  13725 

该问题可以通过多种方式解决,其中两种是:

方法1(简单):一种朴素的方法,包括计算实际值,然后除以10,直到获得所需的答案。但是,此方法不能接受大于n = 15的输入,因为这会导致溢出。

C++
// C++ program to find the first k digits of n^n
#include 
using namespace std;
  
// function that manually calculates n^n and then
// removes digits until k digits remain
unsigned long long firstkdigits(int n, int k)
{
   unsigned long long product = 1;
  
   for (int i = 0 ; i < n ; i++)
      product *= n;
  
   // loop will terminate when there are only
   // k digits left
   while ((int)(product / pow(10, k)) != 0)
      product = product / 10;
  
   return product;
}
  
//driver function
int main()
{
   int n = 15;
   int k = 4;
   cout << firstkdigits(n, k);
   return 0;
}


Java
// Java program to find the first k digits of n^n
public class Digits
{
    // function that manually calculates n^n and then
    // removes digits until k digits remain
    static long firstkdigits(int n, int k)
    {
        long product = 1;
        for (int i = 0 ; i < n ; i++)
           product *= n;
      
       // loop will terminate when there are only
        // k digits left
       while ((int)(product / Math.pow(10, k)) != 0)
            product = product / 10;
        return product;
    }
      
    public static void main(String[] args)
    {
      int n = 15;
      int k = 4;
      System.out.println(firstkdigits(n, k));
    }
}
  
//This code is contributed by Saket Kumar


Python 3
# Python 3 program to find the 
# first k digits of n^n
  
# function that manually calculates 
# n^n and then removes digits until
# k digits remain
def firstkdigits(n, k):
  
    product = 1
      
    for i in range(n ):
        product *= n
      
    # loop will terminate when there 
    # are only k digits left
    while ((product // pow(10, k)) != 0):
        product = product // 10
      
    return product
  
# Driver Code
n = 15
k = 4
print(firstkdigits(n, k))
  
# This code is contributed 
# by ChitraNayal


C#
// C# program to find the
// first k digits of n^n
using System;
  
class Digits
{
    // function that manually calculates
    // n^n and then removes digits until
    // k digits remain
    static long firstkdigits(int n, int k)
    {
        long product = 1;
        for (int i = 0 ; i < n ; i++)
        product *= n;
      
    // loop will terminate when there 
    // are only k digits left
    while ((int)(product / Math.Pow(10, k)) != 0)
            product = product / 10;
              
        return product;
    }
      
    // Driver code
    public static void Main()
    {
      int n = 15;
      int k = 4;
      Console.Write(firstkdigits(n, k));
    }
}
  
// This code is contributed by nitin mittal.


PHP


C
//C++ program to generate first k digits of
// n ^ n
#include 
using namespace std;
  
// function to calculate first k digits
// of n^n
long long firstkdigits(int n,int k)
{
  
   //take log10 of n^n. log10(n^n) = n*log10(n)
   long double product = n * log10(n);
  
   // We now try to separate the decimal and
   // integral part of the /product. The floor
   // function returns the smallest integer
   // less than or equal to the argument. So in
   // this case, product - floor(product) will
   // give us the decimal part of product
   long double decimal_part = product - floor(product);
  
   // we now exponentiate this back by raising 10
   // to the power of decimal part
   decimal_part = pow(10, decimal_part);
  
   // We now try to find the power of 10 by which
   // we will have to multiply the decimal part to
   // obtain our final answer
   long long digits = pow(10, k - 1), i = 0;
  
   return decimal_part * digits;
}
  
// driver function
int main()
{
   int n = 1450;
   int k = 6;
   cout << firstkdigits(n, k);
   return 0;
}


Java
// Java  program to find the first k digits of n^n
  
import java.util.*;
import java.lang.*;
import java.io.*;
  
class KDigitSquare
{
      /* function that manually calculates 
         n^n and then removes digits until 
         k digits remain */
    public static long  firstkdigits(int n, int k)
    {
        //take log10 of n^n. 
        // log10(n^n) = n*log10(n)
        double product = n * Math.log10(n);
       
       /* We will now try to separate the decimal 
          and integral part of the /product. The 
          floor function returns the smallest integer
          less than or equal to the argument. So in
          this case, product - floor(product) will
          give us the decimal part of product */
        double decimal_part = product - Math.floor(product);
       
        // we will now exponentiate this back by 
        // raising 10 to the power of decimal part
        decimal_part = Math.pow(10, decimal_part);
       
        /* We now try to find the power of 10 by 
           which we will have to multiply the decimal 
           part to obtain our final answer*/
        double digits = Math.pow(10, k - 1), i = 0;
          
        return ((long)(decimal_part * digits));
    }
  
    // driver function
    public static void main (String[] args)
    {
        int n = 1450;
        int k = 6;
        System.out.println(firstkdigits(n,k));
    }
}
  
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python3 program to generate k digits of n ^ n 
import math
  
# function to calculate first k digits of n^n 
def firstkdigits(n, k):
      
    # take log10 of n^n.
    # log10(n^n) = n*log10(n)
    product = n * math.log(n, 10);
      
    # We now try to separate the decimal 
    # and integral part of the /product.
    # The floor function returns the smallest 
    # integer less than or equal to the argument. 
    # So in this case, product - floor(product) 
    # will give us the decimal part of product
    decimal_part = product - math.floor(product);
      
    # we now exponentiate this back
    # by raising 10 to the power of
    # decimal part
    decimal_part = pow(10, decimal_part);
      
    # We now try to find the power of 10 by 
    # which we will have to multiply the 
    # decimal part to obtain our final answer
    digits = pow(10, k - 1);
      
    return math.floor(decimal_part * digits); 
  
# Driver Code 
n = 1450; 
k = 6; 
print(firstkdigits(n, k)); 
  
# This code is contributed by mits


C#
// C# program to find the first k digits of n^n
using System;
  
class GFG {
      
    /* function that manually calculates 
        n^n and then removes digits until 
        k digits remain */
    public static long firstkdigits(int n, int k)
    {
          
        // take log10 of n^n. 
        // log10(n^n) = n*log10(n)
        double product = n * Math.Log10(n);
      
    /* We will now try to separate the decimal 
        and integral part of the /product. The 
        floor function returns the smallest integer
        less than or equal to the argument. So in
        this case, product - floor(product) will
        give us the decimal part of product */
        double decimal_part = product -
                              Math.Floor(product);
      
        // we will now exponentiate this back by 
        // raising 10 to the power of decimal part
        decimal_part = Math.Pow(10, decimal_part);
      
        /* We now try to find the power of 10 by 
        which we will have to multiply the decimal 
        part to obtain our final answer*/
        double digits = Math.Pow(10, k - 1);
          
        return ((long)(decimal_part * digits));
    }
  
    // driver function
    public static void Main ()
    {
        int n = 1450;
        int k = 6;
        Console.Write(firstkdigits(n,k));
    }
}
  
// This code is contributed by nitin mittal


PHP


输出 :

4378

方法2:下一个方法涉及使用对数来计算前k个数字。方法和步骤说明如下:

  1. 令积= n n 。取等式两边的对数为10。我们得到log 10 (乘积)= log 10 (n n ),也可以写成n * log 10 (n)
  2. 在此示例中,我们得到log 10 (乘积)= 3871.137516。我们可以将RHS拆分为3871 + 0.137516,因此我们的等式现在可以写为log 10 (乘积)= 3871 + 0.137516
  3. 用底数10升高两侧,并使用上面的示例,我们得到product = 10 3871 x 10 0.137516 。 10 3871不会改变我们的前k位数字,因为它仅移动小数点。我们对下一部分10 0.137516感兴趣,因为这将确定前几位数。
    在这种情况下,值10 0.137516为1.37251。
  4. 因此,我们要求的前5位数字为13725。

C

//C++ program to generate first k digits of
// n ^ n
#include 
using namespace std;
  
// function to calculate first k digits
// of n^n
long long firstkdigits(int n,int k)
{
  
   //take log10 of n^n. log10(n^n) = n*log10(n)
   long double product = n * log10(n);
  
   // We now try to separate the decimal and
   // integral part of the /product. The floor
   // function returns the smallest integer
   // less than or equal to the argument. So in
   // this case, product - floor(product) will
   // give us the decimal part of product
   long double decimal_part = product - floor(product);
  
   // we now exponentiate this back by raising 10
   // to the power of decimal part
   decimal_part = pow(10, decimal_part);
  
   // We now try to find the power of 10 by which
   // we will have to multiply the decimal part to
   // obtain our final answer
   long long digits = pow(10, k - 1), i = 0;
  
   return decimal_part * digits;
}
  
// driver function
int main()
{
   int n = 1450;
   int k = 6;
   cout << firstkdigits(n, k);
   return 0;
}

Java

// Java  program to find the first k digits of n^n
  
import java.util.*;
import java.lang.*;
import java.io.*;
  
class KDigitSquare
{
      /* function that manually calculates 
         n^n and then removes digits until 
         k digits remain */
    public static long  firstkdigits(int n, int k)
    {
        //take log10 of n^n. 
        // log10(n^n) = n*log10(n)
        double product = n * Math.log10(n);
       
       /* We will now try to separate the decimal 
          and integral part of the /product. The 
          floor function returns the smallest integer
          less than or equal to the argument. So in
          this case, product - floor(product) will
          give us the decimal part of product */
        double decimal_part = product - Math.floor(product);
       
        // we will now exponentiate this back by 
        // raising 10 to the power of decimal part
        decimal_part = Math.pow(10, decimal_part);
       
        /* We now try to find the power of 10 by 
           which we will have to multiply the decimal 
           part to obtain our final answer*/
        double digits = Math.pow(10, k - 1), i = 0;
          
        return ((long)(decimal_part * digits));
    }
  
    // driver function
    public static void main (String[] args)
    {
        int n = 1450;
        int k = 6;
        System.out.println(firstkdigits(n,k));
    }
}
  
/* This code is contributed by Mr. Somesh Awasthi */

Python3

# Python3 program to generate k digits of n ^ n 
import math
  
# function to calculate first k digits of n^n 
def firstkdigits(n, k):
      
    # take log10 of n^n.
    # log10(n^n) = n*log10(n)
    product = n * math.log(n, 10);
      
    # We now try to separate the decimal 
    # and integral part of the /product.
    # The floor function returns the smallest 
    # integer less than or equal to the argument. 
    # So in this case, product - floor(product) 
    # will give us the decimal part of product
    decimal_part = product - math.floor(product);
      
    # we now exponentiate this back
    # by raising 10 to the power of
    # decimal part
    decimal_part = pow(10, decimal_part);
      
    # We now try to find the power of 10 by 
    # which we will have to multiply the 
    # decimal part to obtain our final answer
    digits = pow(10, k - 1);
      
    return math.floor(decimal_part * digits); 
  
# Driver Code 
n = 1450; 
k = 6; 
print(firstkdigits(n, k)); 
  
# This code is contributed by mits

C#

// C# program to find the first k digits of n^n
using System;
  
class GFG {
      
    /* function that manually calculates 
        n^n and then removes digits until 
        k digits remain */
    public static long firstkdigits(int n, int k)
    {
          
        // take log10 of n^n. 
        // log10(n^n) = n*log10(n)
        double product = n * Math.Log10(n);
      
    /* We will now try to separate the decimal 
        and integral part of the /product. The 
        floor function returns the smallest integer
        less than or equal to the argument. So in
        this case, product - floor(product) will
        give us the decimal part of product */
        double decimal_part = product -
                              Math.Floor(product);
      
        // we will now exponentiate this back by 
        // raising 10 to the power of decimal part
        decimal_part = Math.Pow(10, decimal_part);
      
        /* We now try to find the power of 10 by 
        which we will have to multiply the decimal 
        part to obtain our final answer*/
        double digits = Math.Pow(10, k - 1);
          
        return ((long)(decimal_part * digits));
    }
  
    // driver function
    public static void Main ()
    {
        int n = 1450;
        int k = 6;
        Console.Write(firstkdigits(n,k));
    }
}
  
// This code is contributed by nitin mittal

的PHP


输出 :

962948

该代码在恒定时间内运行,并且可以处理n的较大输入值