문제

could somebody please shed some light on what this piece of code is actually doing? I think it should be fairly straightforward but I am stuck at the moment, so any help would be greatly appreciated!

 read n;
 i := 1;
 while (i * i * i) <= n do
     i := i + 1;
 output (i-1)

Also how could one calculate the complexity of this? Thank you in advance!

도움이 되었습니까?

해결책

It increments i until is greater than n. Therefore i-1 will be floor(cube_root(n)), and that's what's returned.

The running time is proportional to the cube root of n, since that's how many iterations of the loop are performed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top