PDA

View Full Version : number of digits in (x pow y)



zak
04-12-2010, 16:22
i found this tip: suppose you want to know the number of digits in the result of 73 ^ 94 how do you know that without calculating realy this astronomical number?
number of digits = floor(num2*log(num1)) + 1 = floor(94*log(73)) + 1
= 94 * 1.8633228 + 1
= 175.15234885 + 1
so the number of digits is 176, check this with the gmp calculator here:
http://www.thinbasic.com/community/showthread.php?8577-GMP-library-free-library-for-arbitrary-precision-arithmetic/page2
i don't know the difference between floor and int, i think they are the same. unless someone explain more.

you will find more questions here:
http://mathforum.org/dr.math/

Petr Schreiber
04-12-2010, 17:25
Hi Zak,

thanks for your tip!

As Wikipedia (http://en.wikipedia.org/wiki/Floor_and_ceiling_functions) says, floor is named int in BASIC languages. I attach sample script for you, reproducing the Wiki table results.



Uses "Console"

Dim TestValue(4) As Number = 12/5, 2.7, -2.7, -2
Dim i As Long

PrintL "Value"+$TAB+"Floor"+$TAB+"Ceiling"+$TAB+"Fractional part"
For i = 1 To UBound(TestValue)

PrintL TestValue(i)+$TAB+Int(TestValue(i))+$TAB+Ceil(testValue(i))+$TAB+FractionalPart(testValue(i))

Next

WaitKey

Function FractionalPart(x As Number) As Number
Return x - Int(x)
End Function


The fractional part is quite confusing stuff, I would expect it to work differentely (like ThinBASIC frac).


Petr

kryton9
04-12-2010, 23:34
Math is amazing, that is cool Zak.

danbaron
05-12-2010, 08:45
For Zak's formula to work, you have to use the logarithm for the same base as the numbers. For instance, 73 and 94 are base 10 (I assume). So, in the formula you have to use the base 10 logarithm. (There are logarithms for every base greater than 1, i.e., 2,3,4,..)

The reason Zak's formula works is because, for any base, if

z = x^y

then

log(z) = y * log(x)

(Of course, log(z) and log(x) must be for the same base.)

That is one of the properties of logarithms which makes them so useful.

zak
05-12-2010, 10:08
thanks Dan for the proof, even it is very simple but alluded me and all the 6 respondents in 2 forums,
most of the time the simplest thing are the most hidden one.

danbaron
05-12-2010, 10:41
No problem, Zak.

Now tired.

Dan