I'm learning Data Structures and Algorithms now, I have a practical question that asked to write a function with O(log3n), which means log(n)*log(n)*log(n).
public void run(int n) {
for (int i = 1; i < n; i *= 2) {
for (int j = 1; j < n; j *= 2) {
for (int k = 1; k < n; k *= 2) {
System.out.println("hi");
}
}
}
}
I have come with this solution, but it seems not correct. Please help me out.
n
withfloor(log(n))
. – Jun 15 '12 at 16:31for
s make a^3
, and multiplying with 2 approaches n logarithmically. There's just a trivial way to "construct" a desired complexityO(f(n))
by doingfor(int i=0; i<f(n); i++)
, as pointed out by usr. – Jun 15 '12 at 16:50