Counting Times t+=8 Executed in Algorithm Fun

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Count
In summary, the algorithm executes the command t+=8 a total of $\frac 12(\lfloor \sqrt{n} \rfloor^2+3\lfloor \sqrt{n} \rfloor + 2)$ times. The highest number reached in the inner loop is $4\lfloor \sqrt{n} \rfloor$, which is lower than the highest number of the outer loop, $4n^2$. After $j$ takes the value $4 \lfloor \sqrt{n} \rfloor$, the condition for the inner loop is never true, so the command t+=8 is not executed.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Smile)

Given the following algorithm, I want to find how many times the command t+=8 is executed.

Code:
Function Fun(int m){
int j,k,t=1;
for (j=0; j<=4n^2; j+=4){
     for (k=j; k<=4*sqrt(n); k+=4){
          t+=8;
     }
}
}

The outer loop is executed $\lfloor \frac{4n^2}{4} \rfloor+1=n^2+1$ times, right?
But, after $j= 4 \sqrt{n}$, the inner loop isn't executed.
So, do I have to count the times that the outer loop is executed, till $j= 4 \sqrt{n}$ ? (Thinking)
 
Technology news on Phys.org
  • #2
I tried to find a general formula, by using different values for $n$ and I concluded that the command t+=8; is executed $\sum_{j=0}^{\lfloor \sqrt{n} \rfloor } \left ( \lfloor \sqrt{n} \rfloor-j+1 \right )$ times.

But.. how could I explain formally that the command is executed $\sum_{j=0}^{\lfloor \sqrt{n} \rfloor } \left ( \lfloor \sqrt{n} \rfloor-j+1 \right )$ times? :confused:
 
  • #3
evinda said:
I tried to find a general formula, by using different values for $n$ and I concluded that the command t+=8; is executed $\sum_{j=0}^{\lfloor \sqrt{n} \rfloor } \left ( \lfloor \sqrt{n} \rfloor-j+1 \right )$ times.

But.. how could I explain formally that the command is executed $\sum_{j=0}^{\lfloor \sqrt{n} \rfloor } \left ( \lfloor \sqrt{n} \rfloor-j+1 \right )$ times? :confused:

Here's a method to count this. (Sweating)The highest number in the inner loop that is reached, is:
$$
4\left\lfloor \frac{4\sqrt n}{4} \right\rfloor = 4\lfloor \sqrt n\rfloor
$$
This is lower than the highest number of the outer loop, which is $4n^2$ (edited).

So the number of times the inner loop is executed is:
$$
N(0,4,...,4\lfloor \sqrt n\rfloor) + N(4,...,4\lfloor \sqrt n\rfloor) + ... + N(4\lfloor \sqrt n\rfloor) =\sum_{i=0}^{\lfloor \sqrt{n} \rfloor } \left ( \lfloor \sqrt{n} \rfloor-i+1 \right )
$$
where $N(S)$ is the number of elements in set $S$.This is an arithmetic sequence with $\lfloor \sqrt{n} \rfloor+1$ terms.
Therefore the total number of iterations of the inner loop is:
$$
\frac 1 2 (\lfloor \sqrt{n} \rfloor+1)\big((\lfloor \sqrt{n} \rfloor+1) + 1\big)
=\frac 12(\lfloor \sqrt{n} \rfloor^2+3\lfloor \sqrt{n} \rfloor + 2)
$$

So the order is $O(\frac 1 2 n^2)$. (Nerd)
 
  • #4
I like Serena said:
The highest number in the inner loop that is reached, is:
$$
4\left\lfloor \frac{4\sqrt n}{4} \right\rfloor = 4\lfloor \sqrt n\rfloor
$$

Could you explain me how you found the highest number that is reached in the inner loop? :confused:

I thought that we would calculate it, using this formula:

$$\lfloor \frac{4 \sqrt{n}-j}{4} \rfloor+1$$

for $j=0$.. Am I wrong? (Thinking)
 
  • #5
evinda said:
Could you explain me how you found the highest number that is reached in the inner loop? :confused:

I thought that we would calculate it, using this formula:

$$\lfloor \frac{4 \sqrt{n}-j}{4} \rfloor+1$$

for $j=0$.. Am I wrong? (Thinking)

You are referring to the highest number of iterations. (Shake)
I was referring to the highest number that $k$ can become. (Nod)It is given that $k \le 4\sqrt n$ while $k$ is always a multiple of $4$.
What is the highest multiple of $4$ that is less or equal than $4\sqrt n$? (Wondering)
 
  • #6
I like Serena said:
You are referring to the highest number of iterations. (Shake)
I was referring to the highest number that $k$ can become. (Nod)

A ok! (Nod)
I like Serena said:
It is given that $k \le 4\sqrt n$ while $k$ is always a multiple of $4$.
What is the highest multiple of $4$ that is less or equal than $4\sqrt n$? (Wondering)

The highest multiple of $4$ that is less or equal than $4\sqrt n$ is $4 \lfloor \sqrt{n} \rfloor$, right? (Thinking)
 
  • #7
evinda said:
The highest multiple of $4$ that is less or equal than $4\sqrt n$ is $4 \lfloor \sqrt{n} \rfloor$, right? (Thinking)

Yup! (Nod)
 
  • #8
I like Serena said:
The highest number in the inner loop that is reached, is:
$$
4\left\lfloor \frac{4\sqrt n}{4} \right\rfloor = 4\lfloor \sqrt n\rfloor
$$
This is lower than the highest number of the outer loop, which is indeed $n^2+1$.

Isn't the highest number of the outer loop equal to $4n^2$ ? Or am I wrong? (Worried)
 
  • #9
evinda said:
Isn't the highest number of the outer loop equal to $4n^2$ ? Or am I wrong? (Worried)

You are right. (Doh)
 
  • #10
I like Serena said:
The highest number in the inner loop that is reached, is:
$$
4\left\lfloor \frac{4\sqrt n}{4} \right\rfloor = 4\lfloor \sqrt n\rfloor
$$
This is lower than the highest number of the outer loop, which is $4n^2$ (edited).

Does this mean that the inner loop will be executed while $j \leq 4 \lfloor \sqrt{n} \rfloor$ ? (Thinking)
 
  • #11
evinda said:
Does this mean that the inner loop will be executed while $j \leq 4 \lfloor \sqrt{n} \rfloor$ ? (Thinking)

Depends on what you mean by executed.
Taken literally, the inner loop is executed more often.
But since the starting number is higher than the ending number, its body, the statement [m]t += 8;[/m], is not executed. (Nerd)
 
  • #12
I like Serena said:
Depends on what you mean by executed.
Taken literally, the inner loop is executed more often.
But since the starting number is higher than the ending number, its body, the statement [m]t += 8;[/m], is not executed. (Nerd)

So, after $j$ has taken the value $4 \lfloor \sqrt{n} \rfloor$, we chek each time if $j$ is less or equal to $4 \sqrt{n}$, but since this condition will not be true for any $j$, the command $t+=8;$ will not be executed? (Thinking)

Or have I understood it wrong? :confused:
 
  • #13
evinda said:
So, after $j$ has taken the value $4 \lfloor \sqrt{n} \rfloor$, we chek each time if $j$ is less or equal to $4 \sqrt{n}$, but since this condition will not be true for any $j$, the command $t+=8;$ will not be executed? (Thinking)

Or have I understood it wrong? :confused:

Yep. That's it. (Nod)
 
  • #14
I like Serena said:
Yep. That's it. (Nod)

Nice! Thank you very much! (Party)
 

Related to Counting Times t+=8 Executed in Algorithm Fun

1. What is "Counting Times t+=8 Executed in Algorithm Fun"?

"Counting Times t+=8 Executed in Algorithm Fun" refers to a specific algorithm or set of instructions that involves counting by increments of 8. This could be used in various applications such as data analysis, computer programming, or mathematical calculations.

2. How does this algorithm work?

The algorithm works by starting at a given value, typically 0, and then adding 8 to it repeatedly. For example, if the starting value is 0, the first iteration would result in 0+8=8, the second iteration would result in 8+8=16, and so on. This process can be repeated as many times as needed.

3. What is the purpose of counting by increments of 8?

Counting by increments of 8 can be useful in situations where a larger step size is needed. It can also be used to break down a larger problem into smaller, more manageable parts. Additionally, counting by 8s can be helpful in identifying patterns or trends in data.

4. Can this algorithm be used for any type of data?

Yes, this algorithm can be used for any type of data that can be counted or measured. It is a general counting method and can be applied to various scenarios.

5. Are there any limitations to this algorithm?

Like any algorithm, there may be limitations depending on the specific use case. For example, if the data does not involve whole numbers or if a smaller step size is needed, this algorithm may not be the most efficient or appropriate method. It is important to consider the specific needs and requirements of the problem at hand before using this algorithm.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
2
Replies
44
Views
7K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
Back
Top