Being completely self (and StackOverflow) taught, I'm new to Big-O notation and need to understand it better for some upcoming interviews.
My question is how do you annotate in Big-O when the complexity is less than N
? Example is a prime calculator that checks for remainder of every integer up to N/2
, since if we find no divisor less than half, it's certain there will be none in the upper half.
So is that O(N/2)
or does N
become N = N/2
for the purposes of the notation?
def primecheck(num):
i = 2
while i <= ( num // 2 ):
if not (num % i):
return False
i += 1
return True
Big-O notation is designed to ignore constant factors. As long as k
is a constant (meaning, something unrelated to N
, such as 1/2, 1/3, 1, 2, 3, etc), then O(kN)
means exactly the same thing as O(N)
.
Big-O
notation is designed to express variable-only complexity, all constants are ignored in this case so n/2
is expressed same as 4n
. However prime numbers checking is a problem that needs to check only up to sqrt(n)
so the problem is just O(sqrt(n))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.