However, there are other programs that also print hello, world; yet the fact that they do so is far from obvious. Figure 8.2 shows another program that might print hello, world. It takes an input n, and looks for positive integer solutions to the equation xn + yn = zn. If it finds one, it prints hello, world. If it never finds integers x, y, and z to satisfy the equation, then it continues searching forever, and never prints hello, world.To understand what this program does, first observe that exp is an auxiliary function to compute exponentials. The main program needs to search through triples (x, y, z) in an order such that we are sure we get to every triple of positive integers eventually. To organize the search properly, we use a fourth variable, total, that starts at 3 and, in the while-loop, is increased one unit at a time, eventually reaching any finite integer. Inside the while-loop, we divide total into three positive integers x, y, and z, by first allowing x to range from 1 to total-2, and within that for-loop allowing y to range from 1 up to one less than what x has not already taken from total. What remains, which must be between 1 and total-2, is given to 2.In the innermost loop, the triple (x, y, z) is tested to see if xn + yn = zn. If so, the program prints hello, world, and if not, it prints nothing.
đang được dịch, vui lòng đợi..