Given a two integers N and K, the task is to find the first M and last M digits of the number NK .
Input: N = 2345, K = 3, M = 3
Output: 128 625
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
12 12 = 8916100448256
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)
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—
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:
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.
- Find last five digits of a given five digit number raised to power five
- Print last k digits of a^b (a raised to power b)
- Find first and last digits of a number
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Count numbers with same first and last digits
- Absolute difference between the first X and last X Digits of N
- N digit numbers having difference between the first and last digits as K
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Find power of power under mod of a prime
- Find the sum of power of bit count raised to the power B
- Count pairs in Array whose product is a Kth power of any positive integer
- Calculate Root Mean Kth power of all array elements
- Minimum digits to be removed to make either all digits or alternating digits same
- Find the kth smallest number with sum of digits as m
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.