Does the variable take these values?

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

The discussion centers on the behavior of a for loop defined as for (i=8^{n-1}; i<8^n; i+=1), where n is an integer. Participants confirm that i takes the values from 8^{n-1} to 8^n - 1 when n ≥ 1. They clarify that the loop will always terminate due to the condition i < 8^n. Additionally, the execution count of a nested loop structure is analyzed, concluding that the command key++ is executed (6n+1) * 7 * 8^{n-1} * 52 times, with special cases for n = 0 and n < 0.

PREREQUISITES
  • Understanding of for loop syntax in programming languages
  • Knowledge of exponential notation and integer properties
  • Familiarity with nested loops and their execution counts
  • Basic programming concepts in C-like languages
NEXT STEPS
  • Study the behavior of for loops in C, C++, and Java
  • Learn about exponential growth and its implications in algorithm complexity
  • Explore nested loop execution analysis and performance considerations
  • Investigate edge cases in loop termination conditions
USEFUL FOR

Programmers, computer science students, and algorithm analysts who are interested in loop behavior, execution counts, and performance optimization in coding practices.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

If we have the following for loop:

Code:
 for (i=8^{n-1}; i<8^n; i+=1)

(where $n$ is an integer)

does $i$ take the values $8^{n-1}, 8^{n-1}+1, \dots, 8^n-1$, or am I wrong? (Thinking)
 
Technology news on Phys.org
You are correct. Note, however, that this precise syntax would not work in C, C++, Java and other C-like languages.
 
Evgeny.Makarov said:
You are correct. Note, however, that this precise syntax would not work in C, C++, Java and other C-like languages.

Nice, thank you! (Smile)

It should be also $n \geq 1$, right? (Thinking)

- - - Updated - - -

Or do we not have to take restrictions for $n$ ? (Thinking)
 
evinda said:
It should be also $n \geq 1$, right? (Thinking)

Or do we not have to take restrictions for $n$ ? (Thinking)
The loop will not terminate unless $n$ is a positive integer.

Edit: As I remark later, the loop will always terminate.
 
Last edited:
Evgeny.Makarov said:
The loop will not terminate unless $n$ is a positive integer.

So, can I just say that $i$ takes the values $8^{n-1}, 8^{n-1}+1, \dots, 8^n-1$ times, so the for loop will be executed $7 \cdot 8^{n-1}$, or do I have to say that $i$ takes these values, only if $n \geq 1$ ? (Thinking)
 
First, my remark about termination was wrong. The loop will terminate in any case because the loop guard is $i<8^n$ and not $i=8^n$.

evinda said:
So, can I just say that $i$ takes the values $8^{n-1}, 8^{n-1}+1, \dots, 8^n-1$ times, so the for loop will be executed $7 \cdot 8^{n-1}$, or do I have to say that $i$ takes these values, only if $n \geq 1$ ? (Thinking)
What you said is correct when $n\ge 1$ and $n$ is an integer. Determining the precise number of iterations when $8^{n-1}$ or $8^n$ is not an integer requires some thought (though it's not hard). Do you really need this?
 
Evgeny.Makarov said:
First, my remark about termination was wrong. The loop will terminate in any case because the loop guard is $i<8^n$ and not $i=8^n$.

What you said is correct when $n\ge 1$ and $n$ is an integer. Determining the precise number of iterations when $8^{n-1}$ or $8^n$ is not an integer requires some thought (though it's not hard). Do you really need this?

I am given this algorithm:

Code:
Function(int n){
  int key=1, j,t,x;
  for (j=2*n; j<=8*n; j++){ 
        for (t=8^(n-1); t<8^n; t++){
             for (x=3; x<55; x++){
                  key++;
             }
        }
  }
}

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

The first for is executed $6n+1$ times, the secod for is executed $7 \cdot 8^{n-1}$ times and the third one, $52$ times. So, the command is executed $(6n+1) \cdot 7 \cdot 8^{n-1} \cdot 52$ times.

Do I have to say that it is executed only when $n \geq 1$ ? (Thinking)
 
evinda said:
The first for is executed $6n+1$ times, the secod for is executed $7 \cdot 8^{n-1}$ times and the third one, $52$ times. So, the command is executed $(6n+1) \cdot 7 \cdot 8^{n-1} \cdot 52$ times.

Do I have to say that it is executed only when $n \geq 1$ ?
Yes, your analysis is correct when $n\ge 1$ (and the argument type says that $n$ is integer). If $n=0$, then the first and second loops execute 1 time each, so [m]key++[/m] is executed 52 times. If $n<0$, then the first loop is never executed, so [m]key++[/m] is executed 0 times.
 
Evgeny.Makarov said:
Yes, your analysis is correct when $n\ge 1$ (and the argument type says that $n$ is integer). If $n=0$, then the first and second loops execute 1 time each, so [m]key++[/m] is executed 52 times. If $n<0$, then the first loop is never executed, so [m]key++[/m] is executed 0 times.

So, if $n=0$, does $t$ take only the value $\frac{1}{8}$ and then the for loop terminates? (Thinking)
 
  • #10
Yes, because $8^{-1}+1=\frac{1}{8}+1>1=8^0$.
 
  • #11
Evgeny.Makarov said:
Yes, because $8^{-1}+1=\frac{1}{8}+1>1=8^0$.

I understand! (Nod)

So, do I have to distinguish the cases:

  • $n=0$
  • $n \geq 1$

and find a formula for each case? (Thinking)
 
  • #12
evinda said:
So, do I have to distinguish the cases:

  • $n=0$
  • $n \geq 1$
Yes, and $n<0$.

evinda said:
and find a formula for each case?
In each case, formulas have already been written explicitly in this thread.

Edit: My guess is that the problem authors assumed that $n\ge 1$, but I can't be sure.
 
  • #13
Evgeny.Makarov said:
Yes, and $n<0$.

In each case, formulas have already been written explicitly in this thread.

Edit: My guess is that the problem authors assumed that $n\ge 1$, but I can't be sure.

Nice! Thank you very much! (Smile)
 

Similar threads

Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
1K
Replies
235
Views
14K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K