Big-Oh Basics 2 - Single Loops

What is the runtime of the following function?

1
2
3
4
5
function f(n)
    k = 0
    for i in 1 to n:
         k := k * i
    return k

O ( n 3 ) O(n^3) O ( n ) O(n) O ( n 2 ) O(n^2) O ( 1 ) O(1)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Spencer Whitehead
Feb 26, 2016

Notice that we have an inner for loop that runs n n times. So if we double n n , we double the amount of time spent in that for loop. In general, the time the function takes to run is proportional to n n , so we say that f ( n ) O ( n ) f(n) \in O(n)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...