Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the complexity of the log function?

What is the complexity of the log base 10 function?

like image 288
Paul Manta Avatar asked Sep 06 '11 09:09

Paul Manta


People also ask

How do you find the complexity of a log?

Logarithmic time complexity log(n): Represented in Big O notation as O(log n), when an algorithm has O(log n) running time, it means that as the input size grows, the number of operations grows very slowly.

What is the complexity of Logn?

O(logn) O(logn) is known as logarithmic complexity. The logarithm in O(logn) has a base of 2. The best way to wrap your head around this is to remember the concept of halving: every time n increases by an amount k, the time or space increases by k/2.

Why time complexity is log n?

Logarithmic Time Complexity O(Log n): The time Complexity of a loop is considered as O(Logn) if the loop variables are divided/multiplied by a constant amount. And also for recursive calls in the recursive function, the Time Complexity is considered as O(Logn).

What is the complexity of function?

In computer science, the complexity function of a word or string (a finite or infinite sequence of symbols from some alphabet) is the function that counts the number of distinct factors (substrings of consecutive symbols) of that string.


2 Answers

This really depends on the domain of what values you want to compute a logarithm of.

For IEEE doubles, many processors can take logarithms in a single assembly instruction; x86 has the FYL2X and FYL2XP1 instructions, for example. Although typically instructions like these will only take the logarithm in some fixed base, they can be used to take logarithms in arbitrary bases by using the fact that

loga b = logc b / logc a

by simply taking two logarithms and finding their quotient.

For general integers (of arbitrary precision), you can use repeated squaring combined with a binary search to take logarithms using only O(log log n) arithmetic operations (each time you square a number you double the exponent, which means you can only square the number log log n times before you've exceeded its value and can do a binary search). Using some cute tricks with Fibonacci numbers, you can do this in only O(log n) space. If you're computing the binary logarithm, there are some cute tricks you can use with bit shifts to compute the value in less time (though the asymptotic complexity is the same).

For arbitrary real numbers the logic is harder. You can use Newton's method or the Taylor series to compute logarithms to within a certain precision, though I confess that I'm not familiar with the methods for doing this. However, you rarely actually need to do this because most real numbers are IEEE doubles and there are better algorithms (or even hardware instructions) in that case.

Hope this helps!

like image 110
templatetypedef Avatar answered Oct 05 '22 18:10

templatetypedef


To do log(n) in O(1) ( where n is an integer )

int log(long long x) {     return 64 - __builtin_clzl(x) - 1; } 

for __builtin_clzl(x) refer here

like image 33
iamakshatjain Avatar answered Oct 05 '22 18:10

iamakshatjain