Checking the Efficiency of Function(int n) - Let's Count Key++ Executions!

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Count Efficiency
Click For Summary
SUMMARY

The forum discussion centers on analyzing the efficiency of a nested loop function, specifically the number of times the command key++ is executed. The outer loop iterates approximately (5n^3 - 1) / 5 times, while the inner loop executes around n^(2/3) - 1/3 times. The total execution count for key++ is calculated as ((5n^3 - 1) / 5) * (n^(2/3) - 1/3). Participants discuss the implications of rounding and the correct interpretation of loop limits, ultimately confirming the calculations with examples.

PREREQUISITES
  • Understanding of algorithm complexity analysis
  • Familiarity with nested loops in programming
  • Knowledge of mathematical summation and floor functions
  • Basic proficiency in programming languages that support loops (e.g., C++, Python)
NEXT STEPS
  • Study the implications of loop limits in nested loops
  • Learn about the floor function and its applications in algorithm analysis
  • Explore the concept of Big O notation for algorithm efficiency
  • Investigate common pitfalls in calculating iterations of nested loops
USEFUL FOR

Students, software developers, and computer scientists interested in algorithm optimization and performance analysis of nested loops.

  • #31
evinda said:
A ok.. (Nod) And is the inner loop executed $\displaystyle{ \lfloor \frac{3n^{\frac{2}{3}}-j}{3} \rfloor+1 }$ times, each time ? (Thinking) (Wasntme)

Yes. (Happy)

And before we go on, how many times is the outer loop executed? (Wondering)
 
Technology news on Phys.org
  • #32
I like Serena said:
Yes. (Happy)

And before we go on, how many times is the outer loop executed? (Wondering)

Isn't it executed $\lfloor n^3-1 \rfloor+1$ times? (Thinking) (Thinking)
 
  • #33
evinda said:
Isn't it executed $\lfloor n^3-1 \rfloor+1$ times? (Thinking) (Thinking)

Yes... but you can simplify it further... (Wasntme)
 
  • #34
I like Serena said:
Yes... but you can simplify it further... (Wasntme)

Is it :
$$ \lfloor n^3-\frac{2}{5}\rfloor = \lfloor n^3-1\rfloor$$

or

$$\lfloor n^3-\frac{2}{5}\rfloor= n^3-1$$

? (Thinking)
 
  • #35
evinda said:
Is it :
$$ \lfloor n^3-\frac{2}{5}\rfloor = \lfloor n^3-1\rfloor$$

or

$$\lfloor n^3-\frac{2}{5}\rfloor= n^3-1$$

? (Thinking)

Those are the same! (Wasntme)

If you take the floor of an integer, it's just that integer.
 
  • #36
I like Serena said:
Those are the same! (Wasntme)

If you take the floor of an integer, it's just that integer.

A ok! (Nod) So, the outer loop is executed $n^3-1$ times, right?

The inner loop is executed $\displaystyle{ \lfloor n^{\frac{2}{3}}- \frac{j}{3} \rfloor+1 }$ times..But.. how can we simplify it, now that $j$ is not a constant? (Thinking)
 
  • #37
evinda said:
A ok! (Nod) So, the outer loop is executed $n^3-1$ times, right?

Nope.
Can you check the formula you had again? (Angel)
The outer loop is executed $\displaystyle{ \lfloor n^{\frac{2}{3}}- \frac{j}{3} \rfloor+1 }$ times..But.. how can we simplify it, now that $j$ is not a constant? (Thinking)

I guess you mean the inner loop. :rolleyes:

Let's start with the first time the loop is executed.
That means $j=2$.
How many times does that make? (Wondering)

And how many times during the second time when $j=7$?
 
  • #38
I like Serena said:
Nope.
Can you check the formula you had again? (Angel)

Oh, sorry! It is $n^3$, right? (Blush)

I like Serena said:
I guess you mean the inner loop. :rolleyes:

(Nod) (Blush)

I like Serena said:
Let's start with the first time the loop is executed.
That means $j=2$.
How many times does that make? (Wondering)

And how many times during the second time when $j=7$?

The first time, it is executed $\displaystyle{ \lfloor n^{\frac{2}{3}}- \frac{2}{3} \rfloor+1 }=n^{\frac{2}{3}}-1+1=n^{\frac{2}{3}}$ times, when $j=7$, it is executed $\displaystyle{ \lfloor n^{\frac{2}{3}}- \frac{7}{3} \rfloor+1 = n^{\frac{2}{3}}-3+1=n^{\frac{2}{3}}-2}$ times..

Or am I wrong? (Thinking)
 
  • #39
evinda said:
Oh, sorry! It is $n^3$, right? (Blush)

Yep. (Nod)
The first time, it is executed $\displaystyle{ \lfloor n^{\frac{2}{3}}- \frac{2}{3} \rfloor+1 }=n^{\frac{2}{3}}-1+1=n^{\frac{2}{3}}$ times, when $j=7$, it is executed $\displaystyle{ \lfloor n^{\frac{2}{3}}- \frac{7}{3} \rfloor+1 = n^{\frac{2}{3}}-3+1=n^{\frac{2}{3}}-2}$ times..

Or am I wrong? (Thinking)

Previously, we used that $n^3$ was an integer, so we could simplify the floor function.
However, this time round we have $n^{\frac{2}{3}}$, which does not have to be an integer. :eek:
 
  • #40
I like Serena said:
Previously, we used that $n^3$ was an integer, so we could simplify the floor function.
However, this time round we have $n^{\frac{2}{3}}$, which does not have to be an integer. :eek:

So, can we not simplify the floor function now? (Thinking)
 
  • #41
evinda said:
So, can we not simplify the floor function now? (Thinking)

Nope. We can't. (Doh)
 
  • #42
I like Serena said:
Nope. We can't. (Doh)

(Worried)

So, is the inner loop executed $\sum_{j=2}^{3n^{\frac{2}{3}}} \left ( \lfloor n^{\frac{2}{3}}- \frac{j}{3} \rfloor+1 \right )$ times ? (Thinking)
 
  • #43
evinda said:
(Worried)

So, is the inner loop executed $\sum_{j=2}^{3n^{\frac{2}{3}}} \left ( \lfloor n^{\frac{2}{3}}- \frac{j}{3} \rfloor+1 \right )$ times ? (Thinking)

Something like that... except that $j$ goes up with steps of $5$.

To simplify it, you might substitute $j=5i+2$... (Thinking)

What you will have, is more or less a arithmetic sequence.
For a complexity analysis, we would typically round it up to a worst case scenario. (Thinking)
 
  • #44
I like Serena said:
Something like that... except that $j$ goes up with steps of $5$.

To simplify it, you might substitute $j=5i+2$... (Thinking)

What you will have, is more or less a arithmetic sequence.
For a complexity analysis, we would typically round it up to a worst case scenario. (Thinking)

For $j=2$, we have these values for $k$: $2,5,8, \dots, 3n^{\frac{2}{3}}$ and for $j=7$, we have these values for $k$: $7,10,13, \dots, 3n^{\frac{2}{3}}$, right? (Thinking)

So, is the inner loop maybe executed $\displaystyle{ \frac{1}{2} \cdot \left ( \lfloor \frac{ 3n^{\frac{2}{3}}-2}{3} \rfloor+1\right ) \cdot (3n^{\frac{2}{3}}-2) }$ times? (Thinking)
 
  • #45
evinda said:
For $j=2$, we have these values for $k$: $2,5,8, \dots, 3n^{\frac{2}{3}}$ and for $j=7$, we have these values for $k$: $7,10,13, \dots, 3n^{\frac{2}{3}}$, right? (Thinking)

So, is the inner loop maybe executed $\displaystyle{ \frac{1}{2} \cdot \left ( \lfloor \frac{ 3n^{\frac{2}{3}}-2}{3} \rfloor+1\right ) \cdot (3n^{\frac{2}{3}}-2) }$ times? (Thinking)

Let's see...

The number of times the statement [m]key++;[/m] is executed is something like:
$$
N(2,5,8, \dots, 3n^{\frac{2}{3}})
+N(7,10,13, \dots, 3n^{\frac{2}{3}})
+N(12,15, \dots, 3n^{\frac{2}{3}})
+ \dots
+N(3n^{\frac{2}{3}})
$$
where $N(S)$ is the number of elements in the set $S$.
To be fair, the upper limit is a bit more complex, and slightly lower.
So if we calculate this, we'll get a number that is an upper estimate for the actual number of times.We can write this approximately (erring on the upper side, which corresponds to the "worst" case) as:
$$
\left(\frac{3n^{\frac{2}{3}} - 2}{3}+1\right)
+\left(\frac{3n^{\frac{2}{3}} - 7}{3}+1\right)
+\left(\frac{3n^{\frac{2}{3}} - 12}{3}+1\right)
+ \dots
+1
$$

How many terms are that? (Wondering)
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
31
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 21 ·
Replies
21
Views
1K
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K