You tackle it by keeping a tally of the number of times you go through the loop for a given n, then finding a pattern and making a math program from it. So assign n to equal 1 and figure out how many times the cout statement displays. The do it for 2. Then 3. Then 4, etc.
Then if you have a table like this:
n f(n)
1 1
2 4
3 9
4 16
f(n) equals n^2, so O(n) = n^2. It's a math problem. Get your table and calculate a function for f(n) based on the points and you should have your O(n).
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711