Does the variable take these values?

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

Discussion Overview

The discussion revolves around the behavior of a for loop defined with the variable $i$ taking values from $8^{n-1}$ to $8^n - 1$, where $n$ is an integer. Participants explore the implications of different values of $n$ on the loop's execution and termination, including considerations for when $n$ is less than or equal to zero.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants assert that $i$ takes the values $8^{n-1}, 8^{n-1}+1, \dots, 8^n-1$ when $n \geq 1.
  • Others note that the loop will terminate regardless of whether $n$ is positive, as the condition is $i < 8^n$.
  • There is discussion about whether restrictions on $n$ are necessary for the loop to function correctly, with some suggesting that $n$ must be a positive integer for meaningful execution.
  • Participants explore the execution count of a nested loop structure, with calculations provided for different values of $n$, including cases where $n=0$ and $n<0$.
  • Some participants question whether it is necessary to distinguish cases for $n=0$ and $n \geq 1$, and whether separate formulas should be derived for each case.
  • There is uncertainty regarding the assumptions made by the problem authors about the value of $n$.

Areas of Agreement / Disagreement

Participants express both agreement and disagreement on various aspects of the loop's behavior and execution based on the value of $n$. Multiple competing views remain regarding the necessity of restrictions on $n$ and the implications of different integer values.

Contextual Notes

Limitations include the dependency on the definition of $n$ and the implications of its value on loop execution. The discussion does not resolve whether $n$ must be restricted to positive integers for the loop to be meaningful.

Who May Find This Useful

This discussion may be useful for individuals interested in programming logic, algorithm analysis, and the behavior of loops in programming languages, particularly in the context of mathematical expressions and integer constraints.

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
2K
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