This function is O(log(n)). Why? Isn’t it looping up to n?
function fxn($n) {
for ($i = 1; $i <= $n; $i *= 2)
echo $i;
}
I’m pretty new to O(n) analysis by the way. This function sure looks O(n) though since it loops up to n.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This loop would be O(n):
Because
$itakes on the values:But this loop is only O(log(n)):
Because
$itakes on the values:And a sequence that grows in that manner is called “logarithmic”.