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

In summary: Yes.Can you simplify that expression?... (thinking)We can explain it by using the road-with-markers-principle. (Nerd)If $x$ is a multiple of $d$, there are $\frac x d + 1$ markers.If $x$ is not a multiple of $d$, we have to round $\frac x d$ down for a total of $\left\lfloor\frac x d\right\rfloor + 1$ markers.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

I have the following function:

Code:
Function(int n){
  int key,j,k;
  for (j=2; j<=5n^3; j+=5){ 
        for (k=j; k<=3n^(2/3); k+=3){
             key++;
        }
  }
}

I want to find how many times the command
Code:
key++
is executed.

That's what I thought (Thinking) :

The outer for loop is executed $ \displaystyle{ \frac{5n^3-2+1}{5}=\frac{5n^3-1}{5}}$ times, the nested for loop is executed $ \displaystyle{ \frac{1}{3} \cdot \sum_{j=2}^{3n^{\frac{2}{3}}} 1 =\frac{1}{3} \cdot \sum_{j=1}^{3n^{\frac{2}{3}}} 1 -\frac{1}{3}=\frac{1}{3} \cdot 3n^{\frac{2}{3}}-\frac{1}{3}= n^{\frac{2}{3}}-\frac{1}{3} }$ times.

Therefore, the command is executed $\displaystyle{ \frac{5n^3-1}{5} \cdot \left ( n^{\frac{2}{3}}-\frac{1}{3} \right ) }$ times.

Could you tell me if it is right or if I have done something wrong? (Sweating)
 
Technology news on Phys.org
  • #2
evinda said:
Hello! (Wave)

I have the following function:

Code:
Function(int n){
  int key,j,k;
  for (j=2; j<=5n^3; j+=5){ 
        for (k=j; k<=3n^(2/3); k+=3){
             key++;
        }
  }
}

I want to find how many times the command
Code:
key++
is executed.

That's what I thought (Thinking) :

The outer for loop is executed $ \displaystyle{ \frac{5n^3-2+1}{5}=\frac{5n^3-1}{5}}$ times, the nested for loop is executed $ \displaystyle{ \frac{1}{3} \cdot \sum_{j=2}^{3n^{\frac{2}{3}}} 1 =\frac{1}{3} \cdot \sum_{j=1}^{3n^{\frac{2}{3}}} 1 -\frac{1}{3}=\frac{1}{3} \cdot 3n^{\frac{2}{3}}-\frac{1}{3}= n^{\frac{2}{3}}-\frac{1}{3} }$ times.

Therefore, the command is executed $\displaystyle{ \frac{5n^3-1}{5} \cdot \left ( n^{\frac{2}{3}}-\frac{1}{3} \right ) }$ times.

Could you tell me if it is right or if I have done something wrong? (Sweating)

Hey! (Malthe)

Let's see... suppose we substitute $n=1$... (Thinking)
 
  • #3
I like Serena said:
Hey! (Malthe)

Let's see... suppose we substitute $n=1$... (Thinking)

Then, the command is executed once, but according to my result, it is executed $\frac{8}{15}$ times.. :eek: What have I done wrong? (Thinking)
 
  • #4
evinda said:
Then, the command is executed once, but according to my result, it is executed $\frac{8}{15}$ times.. :eek: What have I done wrong? (Thinking)

Let's start with the outer loop.

Or rather, let's start with an intermezzo. (Emo)
Suppose you are walking along a road that is 100 meters long.
And suppose every meter there is a marker.
How many markers are there? (Thinking)

And what if the road is 99.8 meters? (Thinking)
 
  • #5
I like Serena said:
Let's start with the outer loop.

Or rather, let's start with an intermezzo. (Emo)
Suppose you are walking along a road that is 100 meters long.
And suppose every meter there is a marker.
How many markers are there? (Thinking)

There are $100$ markers, right? (Wasntme)

I like Serena said:
And what if the road is 99.8 meters? (Thinking)

In this case, there are $99$ markers, right? (Thinking)
 
  • #6
evinda said:
There are $100$ markers, right? (Wasntme)

Eek. :eek:
Suppose the road is 2 meters long... how many markers? :rolleyes:
 
  • #7
I like Serena said:
Eek. :eek:
Suppose the road is 2 meters long... how many markers? :rolleyes:

Aren't there two markers? One at the first meter and one at the second one?

View attachment 3330

Or am I wrong? (Thinking) (Blush)
 

Attachments

  • road.png
    road.png
    503 bytes · Views: 49
  • #8
evinda said:
Aren't there two markers? One at the first meter and one at the second one?

https://www.physicsforums.com/attachments/3330

Or am I wrong? (Thinking) (Blush)

Doesn't your picture show 3 markers? (Wasntme)
 
  • #9
I like Serena said:
Doesn't your picture show 3 markers? (Wasntme)

So, is there also a marker at the beginning of the road, before we have one meter? (Thinking)
 
  • #10
evinda said:
So, is there also a marker at the beginning of the road, before we have one meter? (Thinking)

Yep.
One at the beginning of the road, and one every meter, including one at the end of the road. (Wasntme)
 
  • #11
I like Serena said:
Yep.
One at the beginning of the road, and one every meter, including one at the end of the road. (Wasntme)

So, have I done a mistake at the limits of the sum? (Thinking)
 
  • #12
evinda said:
So, have I done a mistake at the limits of the sum? (Thinking)

Before we go back to the limits of the sum...
Suppose the road is $x$ meters long and has markers at every $d$ meters.
How many markers? (Wondering)
 
  • #13
I like Serena said:
Before we go back to the limits of the sum...
Suppose the road is $x$ meters long and has markers at every $d$ meters.
How many markers? (Wondering)

Is it maybe $\displaystyle{ \lfloor \frac{x}{d} \rfloor}+1$ ? (Thinking)
 
  • #14
evinda said:
Is it maybe $\displaystyle{ \lfloor \frac{x}{d} \rfloor}+1$ ? (Thinking)

Yes! (Nod)

Now let's get back to the outer loop.
How many iterations? (Wasntme)
 
  • #15
I like Serena said:
Yes! (Nod)

How could we explain that it is like that? (Thinking)

I like Serena said:
Now let's get back to the outer loop.
How many iterations? (Wasntme)

Are there then $\lfloor \frac{5n^3-2}{5} \rfloor +1$ iterations? (Thinking)
 
  • #16
evinda said:
How could we explain that it is like that? (Thinking)

We can explain it by using the road-with-markers-principle. (Nerd)

If $x$ is a multiple of $d$, there are $\frac x d + 1$ markers.
If $x$ is not a multiple of $d$, we have to round $\frac x d$ down for a total of $\left\lfloor\frac x d\right\rfloor + 1$ markers.
Are there then $\lfloor \frac{5n^3-2}{5} \rfloor +1$ iterations? (Thinking)

Yes.
Can you simplify that expression? (Wondering)
 
  • #17
I like Serena said:
We can explain it by using the road-with-markers-principle. (Nerd)

If $x$ is a multiple of $d$, there are $\frac x d + 1$ markers.
If $x$ is not a multiple of $d$, we have to round $\frac x d$ down for a total of $\left\lfloor\frac x d\right\rfloor + 1$ markers.

A ok.. Is this principle known? (Wasntme)
I like Serena said:
Yes.
Can you simplify that expression? (Wondering)

Is it maybe like that?

$$\lfloor \frac{5n^3-2}{5} \rfloor +1=\lfloor n^3-\frac{2}{5}\rfloor+1, \text{ and since } \frac{2}{5}<1, \lfloor n^3-\frac{2}{5}\rfloor+1=\lfloor n^3\rfloor+1$$

Or am I wrong? (Thinking)
 
  • #18
evinda said:
A ok.. Is this principle known? (Wasntme)

The principle is known, although I'm not sure if it has a name.
Is it maybe like that?

$$\lfloor \frac{5n^3-2}{5} \rfloor +1=\lfloor n^3-\frac{2}{5}\rfloor+1, \text{ and since } \frac{2}{5}<1, \lfloor n^3-\frac{2}{5}\rfloor+1=\lfloor n^3\rfloor+1$$

Or am I wrong? (Thinking)

Suppose $n=1$...? (Wondering)
 
  • #19
evinda said:
It is equal to: $1$, so it cannot be like that.. How could I simplify it then? (Thinking)

Let's take another look at $\left\lfloor n^3-\frac{2}{5}\right\rfloor + 1$.
$n^3$ is an integer.
Since we subtract $\frac{2}{5}$, and then round down, the result will always be 1 less.
And then we add 1 again... (Thinking)
 
  • #20
I like Serena said:
Let's take another look at $\left\lfloor n^3-\frac{2}{5}\right\rfloor + 1$.
$n^3$ is an integer.
Since we subtract $\frac{2}{5}$, and then round down, the result will always be 1 less.
And then we add 1 again... (Thinking)

So, you mean that $ \displaystyle{ \left\lfloor n^3-\frac{2}{5}\right\rfloor=\left\lfloor n^3-1\right\rfloor }$ ?

Why is it like that? (Sweating)
 
  • #21
evinda said:
So, you mean that $ \displaystyle{ \left\lfloor n^3-\frac{2}{5}\right\rfloor=\left\lfloor n^3-1\right\rfloor }$ ?

Why is it like that? (Sweating)

Yes, because this is what rounding down means. (Nerd)
For instance, for $n=1$ we have:
$$\left\lfloor 1-\frac{2}{5}\right\rfloor=\left\lfloor \frac{3}{5}\right\rfloor = 0$$

More generally, $\left\lfloor n^3-\frac{2}{5}\right\rfloor$ is always greater than the integer $n^3-1$ and smaller than $n^3$.
Therefore rounding down yields $n^3-1$. (Wasntme)
 
  • #22
I like Serena said:
Yes, because this is what rounding down means. (Nerd)
For instance, for $n=1$ we have:
$$\left\lfloor 1-\frac{2}{5}\right\rfloor=\left\lfloor \frac{3}{5}\right\rfloor = 0$$

More generally, $\left\lfloor n^3-\frac{2}{5}\right\rfloor$ is always greater than the integer $n^3-1$ and smaller than $n^3$.
Therefore rounding down yields $n^3-1$. (Wasntme)

Is there an identity that we could use, to show it? (Thinking) :confused:
 
  • #23
evinda said:
Is there an identity that we could use, to show it? (Thinking) :confused:

The floor function is defined as:
$$\lfloor x \rfloor=\max\, \{m\in\mathbb{Z}\mid m\le x\}$$

Now note that $n^3 -1 \le n^3 - \frac 25$ and that $n^3 > n^3 - \frac 25$.
So $n^3 -1$ is the largest integer less or equal than the number we're rounding down. (Wasntme)
 
  • #24
I like Serena said:
The floor function is defined as:
$$\lfloor x \rfloor=\max\, \{m\in\mathbb{Z}\mid m\le x\}$$

Now note that $n^3 -1 \le n^3 - \frac 25$ and that $n^3 > n^3 - \frac 25$.
So $n^3 -1$ is the largest integer less or equal than the number we're rounding down. (Wasntme)

A ok.. I understand! (Nerd)

Could you also explain me why it is:

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

and not:

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

? (Thinking)
 
  • #25
evinda said:
Could you also explain me why it is:

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

and not:

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

? (Thinking)

Because however long the road is, there will always be one marker at the beginning of the road.
We calculate how many markers come after by taking the length of the road, dividing by the distance between markers, and rounding down. (Wasntme)
 
  • #26
I like Serena said:
Because however long the road is, there will always be one marker at the beginning of the road.
We calculate how many markers come after by taking the length of the road, dividing by the distance between markers, and rounding down. (Wasntme)

So, at this algorithm, do we know, for sure, that the first for loop is executed for $j=2$ and we want to calculate how many times it will be executed for $j=7$ till $j=5n^3$ ? (Thinking)
 
  • #27
evinda said:
So, at this algorithm, do we know, for sure, that the first for loop is executed for $j=2$ and we want to calculate how many times it will be executed for $j=7$ till $j=5n^3$ ? (Thinking)

There is an exception.
That is if $5n^3$ would be smaller than $2$ (which is not the case here).
Then the loop would not be executed at all.
If that would be the case, the formula would come out as zero or negative. (Mmm)
 
  • #28
I like Serena said:
There is an exception.
That is if $5n^3$ would be smaller than $2$ (which is not the case here).
Then the loop would not be executed at all.
If that would be the case, the formula would come out as zero or negative. (Mmm)

So, in our case, is it like that because of that what I wrote at post #26 ? (Thinking)

In which case does the formula come out negative? (Thinking)
 
  • #29
evinda said:
So, in our case, is it like that because of that what I wrote at post #26 ? (Thinking)

Yes. (Nod)

In which case does the formula come out negative? (Thinking)

If you pick for instance $n=-1$. (Rain)
 
  • #30
I like Serena said:
Yes. (Nod)
If you pick for instance $n=-1$. (Rain)

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)
 
  • #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)
 
  • #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.
 

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
12
Views
819
Replies
9
Views
1K
  • Introductory Physics Homework Help
Replies
17
Views
469
Replies
2
Views
1K
Back
Top