Given a two integers N and K, the task is to find the first M and last M digits of the number NK .
Examples:
Input: N = 2345, K = 3, M = 3
Output: 128 625
Explanation:
2345 3 = 12895213625
Therefore, the first M(= 3) digits are 128 and the last three digits are 625.Input: N = 12, K = 12, M = 4
Output: 8916 8256
Explanation:
12 12 = 8916100448256
Naive Approach:
The simplest approach to solve the problem is to calculate the value of NK and iterate to first M digits and find the last M digits by NK mod 10M.
Time Complexity: O(K)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be optimized by the following observation:
Let us consider a number x which can be written as 10y Where y is a decimal number.
Let x = NK
NK = 10 y
Taking log10 on both sides of the above expression, we get:
K * log10(N) = log10(10 y)
=> K * log10(N) = y * (log1010)
=> y = K * log10(N)
Now y will be a decimal number of form abc—.xyz—
Therefore,
NK = 10abc—.xyz—
=> NK = 10abc— + 0.xyz—
=> NK = 10abc— * 100.xyz—
In the above equation, 10abc— only moves the decimal point forward.
By calculating 100.xyz—, the first M digits can be figured out by moving the decimal point forward.
Follow the steps below to solve the problem:
- Find the first M digits of NK by calculating (NK)mod(10M).
- Calculate K * log10(N).
- Calculate 10K * log10(N).
- Find the first M digits by calculating 10K * log10(N) * 10M – 1 .
- Print the first and last M digits obtained.
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> #define ll long long int using namespace std; // Function to find a^b modulo M ll modPower(ll a, ll b, ll M) { ll res = 1; while (b) { if (b & 1) res = res * a % M; a = a * a % M; b >>= 1; } return res; } // Function to find the first and last // M digits from N^K void findFirstAndLastM(ll N, ll K, ll M) { // Calculate Last M digits ll lastM = modPower(N, K, (1LL) * pow(10, M)); // Calculate First M digits ll firstM; double y = (double)K * log10(N * 1.0); // Extract the number after decimal y = y - (ll)y; // Find 10 ^ y double temp = pow(10.0, y); // Move the Decimal Point M - 1 digits forward firstM = temp * (1LL) * pow(10, M - 1); // Print the result cout << firstM << " " << lastM << endl; } // Driver Code int main() { ll N = 12, K = 12, M = 4; findFirstAndLastM(N, K, M); return 0; } |
Java
// Java program to implement // the above approach class GFG{ // Function to find a^b modulo M static long modPower(long a, long b, long M) { long res = 1; while (b > 0) { if (b % 2 == 1) res = res * a % M; a = a * a % M; b >>= 1; } return res; } // Function to find the first and last // M digits from N^K static void findFirstAndLastM(long N, long K, long M) { // Calculate Last M digits long lastM = modPower(N, K, (1L) * (long)Math.pow(10, M)); // Calculate First M digits long firstM; double y = (double)K * Math.log10(N * 1.0); // Extract the number after decimal y = y - (long)y; // Find 10 ^ y double temp = Math.pow(10.0, y); // Move the Decimal Point M - 1 digits forward firstM = (long)(temp * (1L) * Math.pow(10, (M - 1))); // Print the result System.out.print(firstM + " " + lastM + "\n"); } // Driver Code public static void main(String[] args) { long N = 12, K = 12, M = 4; findFirstAndLastM(N, K, M); } } // This code is contributed by Rajput-Ji |
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG{ // Function to find a^b modulo M static long modPower(long a, long b, long M) { long res = 1; while (b > 0) { if (b % 2 == 1) res = res * a % M; a = a * a % M; b >>= 1; } return res; } // Function to find the first and last // M digits from N^K static void findFirstAndLastM(long N, long K, long M) { // Calculate Last M digits long lastM = modPower(N, K, (1L) * (long)Math.Pow(10, M)); // Calculate First M digits long firstM; double y = (double)K * Math.Log10(N * 1.0); // Extract the number after decimal y = y - (long)y; // Find 10 ^ y double temp = Math.Pow(10.0, y); // Move the Decimal Point M - 1 digits forward firstM = (long)(temp * (1L) * Math.Pow(10, (M - 1))); // Print the result Console.Write(firstM + " " + lastM + "\n"); } // Driver Code public static void Main(String[] args) { long N = 12, K = 12, M = 4; findFirstAndLastM(N, K, M); } } // This code is contributed by gauravrajput1 |
8916 8256
Time Complexity: O(log K)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find last five digits of a given five digit number raised to power five
- Find the sum of power of bit count raised to the power B
- Sum of digits of a given number to a given power
- Number of digits in N factorial to the power N
- Print last k digits of a^b (a raised to power b)
- Number of digits in 2 raised to power n
- Find power of power under mod of a prime
- Find the average of k digits from the beginning and l digits from the end of the given number
- Program to find whether a no is power of two
- Find value of y mod (2 raised to power x)
- Find whether a given integer is a power of 3 or not
- Find whether a given number is a power of 4 or not
- Find the super power of a given Number
- Given two numbers as strings, find if one is a power of other
- Find smallest number with given number of digits and sum of digits under given constraints
- Find unit digit of x raised to power y
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find the minimum number to be added to N to make it a power of K
- Find two numbers whose difference of fourth power is equal to N
- Find maximum power of a number that divides a factorial
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

