Example 1 — Watch the n^2 term dominate
f(n) = 3n^2 + 5n + 2 divided by n^2.
#include <stdio.h>
int main(void) {
long long sizes[] = {10, 100, 1000};
for (int i = 0; i < 3; i++) {
long long n = sizes[i];
long long f = 3 * n * n + 5 * n + 2;
printf("n=%lld: f=%lld, f/n^2=%.2f\n", n, f, (double)f / (double)(n * n));
}
return 0;
} How It Works
- The ratio settles near the constant 3 as n grows.
- Lower terms 5n and 2 become irrelevant.
- So f is O(n^2): constants and small terms are dropped.
