# Solution 3

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

3Ea - Solutions

Solution to Fibonacci Sequence Problem

1: (* Author:    Tao Yue
2:    Date:      19 July 1997
3:    Description:
4:       Find the first 10 Fibonacci numbers
5:    Version:
6:       1.0 - original version
7: *)
8:
9: program Fibonacci;
10:
11: var
12:   Fibonacci1, Fibonacci2 : integer;
13:   temp : integer;
14:   count : integer;
15:
16: begin    (* Main *)
17:   writeln ('First ten Fibonacci numbers are:');
18:   count := 0;
19:   Fibonacci1 := 0;
20:   Fibonacci2 := 1;
21:   repeat
22:     write (Fibonacci2:7);
23:     temp := Fibonacci2;
24:     Fibonacci2 := Fibonacci1 + Fibonacci2;
25:     Fibonacci1 := Temp;
26:     count := count + 1
27:   until count = 10;
28:   writeln;
29:
30:   (* Of course, you could use a FOR loop or a WHILE loop
31:      to solve this problem. *)
32:
33: end.     (* Main *)

Solution to Powers of Two Problem

1: (* Author:    Tao Yue
2:    Date:      13 July 2000
3:    Description:
4:       Display all powers of two up to 20000, five per line
5:    Version:
6:       1.0 - original version
7: *)
8:
9: program PowersofTwo;
10:
11: const
12:   numperline = 5;
13:   maxnum = 20000;
14:   base = 2;
15:
16: var
17:   number : longint;
18:   linecount : integer;
19:
20: begin    (* Main *)
21:   writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
22:   (* Set up for loop *)
23:   number := 1;
24:   linecount := 0;
25:   (* Loop *)
26:   while number <= maxnum do
27:   begin
28:     linecount := linecount + 1;
29:     (* Print a comma and space unless this is the first
30:        number on the line *)
31:     if linecount > 1 then
32:       write (', ');
33:     (* Display the number *)
34:     write (number);
35:     (* Print a comma and go to the next line if this is
36:        the last number on the line UNLESS it is the
37:        last number of the series *)
38:     if (linecount = numperline) and not (number * 2 > maxnum) then
39:     begin
40:       writeln (',');
41:       linecount := 0
42:     end;
43:     (* Increment number *)
44:     number := number * base;
45:   end;  (* while *)
46:   writeln;
47:
48:   (* This program can also be written using a
49:      REPEAT..UNTIL loop. *)
50:
51: end.     (* Main *)

Note that I used three constants: the base, the number of powers to display on each line, and the maximum number. This ensures that the program can be easily adaptable in the future.

Using constants rather than literals is a good programming habit to form. When you write really long programs, you may refer to certain numbers thousands of times. If you hardcoded them into your code, you'd have to search them out. Also, you might use the same value in a different context, so you can't simply do a global Search-and-Replace. Using a constant makes it simpler to expand the program.

Also note that I used the longint type for the number variable. This is because to fail the test number <= 20000, number would have to reach 32768, the next power of two after 16384. This exceeds the range of the integer type: -32768 to 32767. (try it without longint and see what happens)