A program (or function), called H, that solves the Halting Problem takes as input any program P and any input X for P. H outputs TRUE if P would halt when given input X, and outputs FALSE otherwise (e.g., if P would go into an infinite loop).For example,
Loop.cpp
:
#includedoes not halt for input X = -1, but does halt for input X = 5. So running#include int main(int argc, char *argv[]) { int Index; Index = atoi(argv[1]); while (Index != 0) Index--; return(0); }
H("Loop.cpp",-1)
should output FALSE,
and H("Loop.cpp",5)
should output TRUE.
#includewill obvious never halt for any input. Similarly,int main(int argc, char *argv[]) { while (TRUE) ; return(0); }
#includewill always halt for any input. Of course, most programs are a lot more complicated so solving the halting problem in general is harder than this.int main(int argc, char *argv[]) { return(0); }
H(P,X) = True if P halts when given input X = False if P does not halt when given input X
I(P) { if (H(P,P)) while (TRUE) ; else return(TRUE); }
I(P) = True if P does not halt when given input P Loops if P halts when given input P
A program, called T, that solves the Totality Problem takes as input a program P. T outputs TRUE if P would halt when given any possible input, and outputs FALSE otherwise.If the Totality Problem could be solved, then so could the Halting Problem. Therefore the Totality Problem is noncomputable.
A program, called E, that solves the Equivalence Problem takes as input two programs P and Q. E outputs TRUE if, given any input, P and Q produce the same output, and E outputs FALSE otherwise.If the Equivalence Problem could be solved, then so could the Totality Problem. Therefore the Equivalence Problem is noncomputable.
for (Index = 0; Index < N; Index++) TheArray[Index] = TheArray[Index] + 7;How long will it take on a computer?
Index = 0
- once
Index < N
- N times
TheArray[Index] =
- N times
+ 7
- N times
Index++
- N times
<, +, ++
for (Index = 0; Index < N; Index++) TheArray[Index] = TheArray[Index] * 2 + 7;How long will it take on a computer?
* 2
- N times. The time is taken
is still proportional to N.
O(1)
| | O(logb(N))
| | O(N)
| | O(N * logb(N))
| | O(Nm)
| | O(kN)
| |
3 | 3 | 3 | 3 | 3 | 3 | Constant |
---|---|---|---|---|---|---|
log10N | 0 | 1 | 2 | 3 | 4 | Logarithmic Base 10 |
3 * log10N | 0 | 3 | 6 | 9 | 12 | |
log2N | 0 | 3.32 | 6.64 | 9.96 | 13.29 | Logarithmic Base 2 |
10 * log2N | 0 | 33.2 | 66.4 | 99.6 | 132.9 | |
N | 1 | 10 | 100 | 1000 | 10000 | Linear |
2 * N | 2 | 20 | 200 | 2000 | 20000 | |
200 * N | 200 | 2000 | 20000 | 200000 | 2000000 | |
N * log10N | 0 | 10 | 200 | 3000 | 40000 | N log N |
N2 | 1 | 100 | 10000 | 1000000 | 100000000 | Polynomial Order 2 |
100 * N2 | 100 | 10000 | 1000000 | 100000000 | 10000000000 | |
N5 | 1 | 100000 | 10000000000 | 1015 | 1020 | Polynomial Order 5 |
2N | 2 | 1024 | 1.26765 * 1030 | eeek | mega eeek | Exponential |
for (k=1; k <= n/2; k++) { for (j=1; j <=n*n; j++) { Do Something } }
for (k=1; k <= n/2; k++) { Do Something } for (j=1; j <=n*n; j++) { Do Something }
k = n; while (k > 1) { Do Something k = k/2; }
Data | |||
---|---|---|---|
State | A | B | ' ' |
S | S Move right | S1 Write A | H |
S1 | S Move right |
State | # | ||||
---|---|---|---|---|---|
S | A | B | B | A |   |
State | # | ||||
---|---|---|---|---|---|
S | A | B | B | A |   |
State | # | ||||
---|---|---|---|---|---|
S1 | A | A | B | A |   |
State | # | ||||
---|---|---|---|---|---|
S | A | A | B | A |   |
State | # | ||||
---|---|---|---|---|---|
S1 | A | A | A | A |   |
State | # | ||||
---|---|---|---|---|---|
S | A | A | A | A |   |
State | # | ||||
---|---|---|---|---|---|
S | A | A | A | A |   |
State | # | ||||
---|---|---|---|---|---|
H | A | A | A | A |   |
Index = N; Total = 0; while (Index > 1) { Total += Index; Index =/ 2; } for (Index = N; Index > -N; Index--) Total += Index;