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

Discussion Overview

The discussion revolves around analyzing the efficiency of a function that counts the number of times a specific command, key++, is executed within nested loops. Participants explore the mathematical reasoning behind the execution count, including the limits of summation and the implications of rounding in calculations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant calculates the number of executions of key++ based on the outer and nested loop iterations, arriving at a specific formula.
  • Another participant tests the formula with a specific value of n=1, leading to a discrepancy in the expected execution count.
  • Participants discuss the concept of markers along a road as an analogy to understand the limits of summation and the counting of iterations.
  • There is a proposal that the number of iterations can be expressed using the floor function, leading to further exploration of its implications.
  • Participants question the correctness of their reasoning regarding the limits of the sum and the application of the floor function in their calculations.
  • There is a discussion about whether the principle of counting markers is a known concept and its relation to the problem at hand.
  • Further exploration of the floor function leads to a discussion about its properties and how it affects the calculations of iterations in the loops.

Areas of Agreement / Disagreement

Participants express uncertainty and disagreement regarding the execution count derived from their calculations, particularly when substituting specific values. The discussion remains unresolved as participants explore different interpretations and mathematical principles.

Contextual Notes

There are limitations in the assumptions made about the limits of summation and the application of the floor function, which remain unresolved throughout the discussion.

  • #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
4K
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 21 ·
Replies
21
Views
1K
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K