It should be 1+4+9+...+n^2 = n(n+1)(2n+1)/6
, or simply O(n^3)
, for this case.
For each step in the for
-loop, it will run i^2
times for the while
. Given x=i;y=1/i;
, it will take i^2
(as x=y*i^2
) times for x
to reach x<=0
by decreament step x=x-y
.
For i
, it will be 1,2,...,n
, summing them up, you will get 1+4+9+...n^2 = n(n+1)(2n+1)/6
.