Does the variable take these values?

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Variable
In summary, the for loop will terminate unless $n$ is a positive integer. For cases where $n\ge 1$, the first and second loops will execute 1 time each.
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
You are correct. Note, however, that this precise syntax would not work in C, C++, Java and other C-like languages.
 
  • #3
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)
 
  • #4
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:
  • #5
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)
 
  • #6
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?
 
  • #7
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)
 
  • #8
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.
 
  • #9
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)
 

Related to Does the variable take these values?

What are the possible values for this variable?

The possible values for a variable depend on the type of variable. If it is a categorical or qualitative variable, the possible values are specific categories or labels. If it is a numerical or quantitative variable, the possible values can be any number within a range or set of values.

How do the values of this variable affect the outcome of the experiment?

The values of a variable can affect the outcome of an experiment by influencing the results or causing a change in the dependent variable. It is important to carefully choose and control the values of variables in order to accurately interpret the results of an experiment.

Are there any restrictions on the values that this variable can take?

Some variables may have restrictions on the values they can take, such as being positive or negative, or within a certain range. These restrictions may be based on the nature of the variable or the specific experiment being conducted.

Can the values of this variable be manipulated or controlled?

In most cases, the values of a variable can be manipulated or controlled by the researcher. This allows for the testing of different values and their impact on the outcome of an experiment.

How do the values of this variable compare to other variables in the experiment?

The values of a variable can be compared to other variables in an experiment in order to determine relationships or patterns. This can help identify the role and significance of the variable in the overall experiment.

Similar threads

  • Programming and Computer Science
Replies
25
Views
490
  • Programming and Computer Science
Replies
1
Views
953
  • Programming and Computer Science
Replies
2
Views
881
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
27
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top