logo

Dat un număr n, găsiți primele k cifre ale lui n^n

Dat un număr n găsiți primele k cifre ale lui nnunde k este o valoare mai mică decât numărul de cifre din nn 
Exemple:
 

   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 


 


Problema poate fi rezolvată în mai multe moduri, dintre care două sunt:
Metoda 1 (simplu): O metodă naivă care presupune calcularea valorii reale și apoi împărțirea la 10 până când obținem răspunsul necesar. Cu toate acestea, această metodă nu poate prelua o intrare mai mare de n = 15, deoarece ar provoca depășire. 
 



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
 // PHP program to find the // first k digits of n^n // function that manually  // calculates n^n and then // removes digits until k // digits remain function firstkdigits($n $k) { $product = 1; for ($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 = (int) $product / 10; return floor($product); } // Driver Code $n = 15; $k = 4; echo firstkdigits($n $k); // This code is contributed by aj_36 ?> 
JavaScript
<script> // Javascript program to find the first k digits of n^n    // function that manually calculates n^n and then  // removes digits until k digits remain  function firstkdigits(nk)  {  let product = 1;  for (let i = 0 ; i < n ; i++)  product *= n;    // loop will terminate when there are only  // k digits left  while (Math.floor(product / Math.pow(10 k)) != 0)  product = Math.floor(product / 10);  return product;  }    let n = 15;  let k = 4;  document.write(firstkdigits(n k));    // This code is contributed by avanitrachhadiya2155 </script> 

Ieșire: 
 

4378


Metoda 2: Următoarea metodă implică utilizarea logaritmilor pentru a calcula primele k cifre. Metoda și pașii sunt explicați mai jos:
 

  1. Fie produs = nn. Luați baza logaritmului 10 de ambele părți ale ecuației. Primim jurnalul10(produs) = log10(nn) pe care îl putem scrie și ca n*log10(n)
  2. În acest exemplu obținem jurnalul10(produs) = 3871,137516. Putem împărți RHS ca 3871 + 0,137516, astfel încât ecuația noastră poate fi acum scrisă ca log10(produs) = 3871 + 0,137516
  3. Ridicați ambele părți cu baza 10 și folosind exemplul de mai sus obținem produsul = 103871x 100,137516. 103871nu va face o diferență pentru primele noastre k cifre, deoarece deplasează doar punctele zecimale. Suntem interesați de următoarea parte 100,137516deoarece aceasta va determina primele câteva cifre. 
    În acest caz, valoarea 100,137516este 1,37251.
  4. Prin urmare, primele 5 cifre necesare ar fi 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 nint 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(nk));  } } /* 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(nk));  } } // This code is contributed by nitin mittal 
PHP
 // PHP program to generate  // k digits of n ^ n // function to calculate  // first k digits of n^n function firstkdigits($n $k) { // take log10 of n^n.  // log10(n^n) = n*log10(n) $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 $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 $digits = pow(10 $k - 1); $i = 0; return floor($decimal_part * $digits); } // Driver Code $n = 1450; $k = 6; echo firstkdigits($n $k); // This code is contributed by m_kit ?> 
JavaScript
<script> // Javascript program to find the first k digits of n^n    /* function that manually calculates   n^n and then removes digits until   k digits remain */  function firstkdigits(nk)  {  //take log10 of n^n.   // log10(n^n) = n*log10(n)  let 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 */  let 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*/  let digits = Math.pow(10 k - 1) i = 0;    return (Math.floor(decimal_part * digits));  }    // Driver code  let n = 1450;  let k = 6;  document.write(firstkdigits(n k));    // This code is contributed by rag2127 </script> 

Ieșire:  
 

962948


Acest cod rulează în timp constant și poate gestiona valori mari de intrare de n
 

Creați un test