Download presentation
Presentation is loading. Please wait.
1
Halting Problem Dr K R Bond 2009
NEW A2 COMPUTING Halting Problem Dr K R Bond 2009 © Dr K R Bond 2009
2
NEW A2 COMPUTING The halting problem asks 'Is it possible in general to write a program that can tell, given any program and its inputs and without executing this program, whether the given program with its given inputs will halt?' It sheds light on why it is not possible to predict in advance when a computer will crash, i.e. get stuck in some loop, therefore enabling such a situation to be avoided. © Dr K R Bond 2009
3
NEW A2 COMPUTING Predicting in advance would require a method that could decide, just by inspecting another program and its inputs, whether or not the program would go into an infinite loop on the given input; an infinite loop often signifies a bug. A general method to do this for any program and its inputs does not exist and can be proven to not exist; see the PC activity. © Dr K R Bond 2009
4
NEW A2 COMPUTING Blank slide © Dr K R Bond 2009
5
NEW A2 COMPUTING PC Activity
Here is an example of an algorithm that may or may not go into an infinite loop: 1 Input x. 2 While x is not equal to 1, do the following: If x is even, divide x by 2 Otherwise, set x to 3x + 1. © Dr K R Bond 2009
6
NEW A2 COMPUTING Activity Does this algorithm terminate for:
x = 15? x = 105? Any positive x? Code the algorithm in the programming language that you are most familiar with. © Dr K R Bond 2009
7
NEW A2 COMPUTING PC activity
Go to and for proofs of the halting problem’s conclusion. © Dr K R Bond 2009
8
NEW A2 COMPUTING How long should one wait?
Often it is difficult to tell whether a program has entered an infinite loop, because it may be that the program just needs a little longer to do its calculations. © Dr K R Bond 2009
9
NEW A2 COMPUTING Here is an example:
In mathematics, a perfect number is defined as a positive integer that is the sum of its positive divisors excluding the number itself. For example, 6 is a perfect number because its divisors are 1, 2 and 3 and = 6. © Dr K R Bond 2009
10
NEW A2 COMPUTING The next two perfect numbers are 28 and 496.
We can express these perfect numbers as 1 + kx, where k and x are integers such that k, x > 0. © Dr K R Bond 2009
11
NEW A2 COMPUTING Given x, is there a perfect number n of the form 1 + k.x for some k > 0 and k < s? © Dr K R Bond 2009
12
NEW A2 COMPUTING The following program outputs 6, 28, 496, 8128 when x = 1 and s = 27, 29, 497, respectively. The program takes a long time to find the perfect number (see example program) When x = 2 the program does not halt; It searches for odd perfect numbers. Finding an odd perfect number has defeated the best mathematicians to date © Dr K R Bond 2009
13
NEW A2 COMPUTING Function FindPerfectNumber(x, s : Integer) : Integer;
Var Sum, i, n : Integer; Found : Boolean; Begin FindPerfectNumber := -1; Found := False; n := 1; While Not Found Do Sum := 0; i := 1; While i < n If (n Mod i) = 0 {Find next factor} Then Sum := Sum + i; {Add factor to Sum} i := i + 1; End; © Dr K R Bond 2009
14
NEW A2 COMPUTING If (Sum = n) And (n >= s)
{If true then found first perfect number after s} Then Begin FindPerfectNumber := n; Found := True; Break; End; If Not Found Then n := n + x; // Try next n © Dr K R Bond 2009
15
NEW A2 COMPUTING Var x, s : Integer; Begin Repeat Write('Input x: ');
Readln(x); Write('Input s: '); Readln(s); If x > 0 Then Writeln(FindPerfect(x, s)); Until x = 0; Readln; End. © Dr K R Bond 2009
16
NEW A2 COMPUTING The next program calls a procedure Test.
Procedure Test takes as input a function F and a single parameter, x and executes F on input x. How useful is a procedure that is designed to accept as input, another subprogram, e.g. a function, together with the input to be run by this subprogram? © Dr K R Bond 2009
17
NEW A2 COMPUTING Very useful because subprograms such as the function FindPerfectNumber which halt for values of x = 1 and s = 7, 29 and 497 but not for values of x = 2 could be submitted to a differently constructed procedure Test whose construction would be to determine without executing FindPerfectNumber if FindPerfectNumber halts for the given input values of x and s. © Dr K R Bond 2009
18
NEW A2 COMPUTING Procedure Test would be extremely useful because it would solve the problem of having to wait an indeterminate amount of time for the subprogram to terminate. Is it possible to construct such a procedure Test whose purpose is to accept, as input, a subprogram or program and its input, and to determine whether the subprogram will halt on this input or not? Unfortunately, the answer is no. © Dr K R Bond 2009
19
NEW A2 COMPUTING This has serious implications for algorithms in general. To discover if an algorithm will halt it needs to be run on its range of specified inputs. There is no other way, in general. However, if when running the algorithm we find ourselves waiting for a long time, then do we conclude that the algorithm is stuck in a loop or do we conclude that we have not allowed enough time for the algorithm to calculate its output? © Dr K R Bond 2009
20
NEW A2 COMPUTING End. © Dr K R Bond 2009 Program Project1;
{$APPTYPE CONSOLE} Uses SysUtils; Type TFunction = Function (y : Integer) : Integer; {Declare a type which is a function with a single integer parameter y and which returns an integer result} Function A (Value : Integer) : Integer; Begin A := 2 * Value; {Function returns 2 times Value} End; Procedure Test (F : TFunction; x : Integer); { Takes as input a function F and executes function F on input Parameter} Writeln(F(x)); Test(A,6); {Executes function F on input 6} Readln; End. © Dr K R Bond 2009
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.