Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of digits for N**K

Tags:

c++

math

For given integers N and K (1 <= N, K <= 2000000000) you have to find the number of digits of N^K.

Is there any formula or something ? Because I tried solving it by simply powering N**K but it's not working for large values and the program simply freezes because of the calculations. I am looking for some fast way maybe some math formula like I said before.

like image 881
dada Avatar asked Apr 15 '10 12:04

dada


4 Answers

The 10 base log of N should give you the number of digits in it. This must be enough as a hint :-)

like image 135
Péter Török Avatar answered Oct 12 '22 02:10

Péter Török


Hint: Logarithm.

like image 39
kennytm Avatar answered Oct 12 '22 04:10

kennytm


Hints: Log (X ^ Y) = Y * Log (X)

The following numbers have 4 digits; the integer part of the decimal logarithm is 4 - 1 = 3. Log 1000 = 3, Log (9999) = 3,9999565683801924896154439559762

like image 27
Thingol Avatar answered Oct 12 '22 04:10

Thingol


Try to think of a math operation that tells you the number of digits of a number. Apply that to N**K and see if you can't simplify the formula.

like image 44
clahey Avatar answered Oct 12 '22 04:10

clahey