Given a number n, find the first k digits of n^n
Given a number n, find the first k digits of nn, where k is a value less than the number of digits in nn
Examples :
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
The problem can be solved in multiple ways, of which two are:
Method 1 (Simple): A naive method which involves calculating the actual value and then dividing by 10 until we get the required answer. However this method cannot take input greater than n = 15 as it would cause overflow.
C++
// C++ program to find the first k digits of n^n #include <bits/stdc++.h> 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 = 15k = 4print(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 // 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 ?> |
Output :
4378
Method 2: The next method involves using logarithms to calculate the first k digits. The method and steps are explained below:
- Let product = nn. Take logarithm base 10 on both sides of the equation. We get log10(product) = log10(nn), which we can also write as n*log10(n)
- In this example, we get log10(product) = 3871.137516. We can split the RHS as 3871 + 0.137516, so our equation can now be written as log10(product) = 3871 + 0.137516
- Raise both sides with base 10, and using the above example, we get product = 103871 x 100.137516. 103871 will not make a difference to our first k digits as it only shifts decimal points. We are interested in the next part, 100.137516, as this will determine the first few digits.
In this case, the value of 100.137516 is 1.37251. - Hence our required first 5 digits would be 13725.
C
//C++ program to generate first k digits of // n ^ n #include <bits/stdc++.h> 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
<?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 ?> |
Output :
962948
This code runs in constant time and can handle large input values of n
This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find smallest possible Number from a given large Number with same count of digits
- Find the smallest number whose digits multiply to a given number n
- Find count of digits in a number that divide the number
- Find maximum number that can be formed using digits of a given number
- Find first and last digits of a number
- Find Next number having distinct digits from the given number N
- Find M-th number whose repeated sum of digits of a number is N
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find sum of digits in factorial of a number
- Find the last two digits of Factorial of a given Number

