
Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
Great explanation of O(log n)! I especially liked the analogy with guessing a number.
Can you expand on how O(log n) compares to other time complexities like O(n) or O(n^2) in terms of efficiency, and which is best for which types of problems? I think this can help a lot of beginner developers compare all options and gain a bit more insight.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

I had a hard time understanding O(log n) .
I'm a high school dropout.
I don't know shit about logarithms.
So I ended up writing this for my own understanding.
Let's say you're trying to guess a number between 1 and 1024, and with each guess, I tell you if the correct number is higher or lower. If each guess you make is always in the middle of the remaining range, the number of guesses you will make will be:
Guess 1: Range is 1-1024
Guess 2: Range is either 1-512 or 513-1024
Guess 3: The range is halved again...
This is logarithmic time. Each operation reduces the problem size by in large.
The logarithmic time complexity, O (log n), means that as the input size (n) grows, the number of operations doesn't grow linearly but grows in a logarithmic scale.
This is why algorithms with logarithmic time complexity are efficient. They're handy when dealing with large data sets.
Binary search is the most classic example. Let's say you have a sorted array of numbers and you want to check if a specific number exists in that array.
function binarySearch(array, target) {
let left = 0;
let right = array.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (array[mid] === target) {
return mid;
}
if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
Every iteration of the loop, we halve the problem size.
It's actually simpler than you'd expect.
All you need is an analogy.