Difference between revisions of "Solution 3"

From Lazarus wiki
Line 2: Line 2:
  
 
'''Solution to Fibonacci Sequence Problem'''
 
'''Solution to Fibonacci Sequence Problem'''
<font color="#000000">  1:</font> <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Author:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Tao</font><font color="#cc0000"> </font><font color="#cc0000">Yue</font>
+
<delphi>
<font color="#000000">  2:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Date:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">19</font><font color="#cc0000"> </font><font color="#cc0000">July</font><font color="#cc0000"> </font><font color="#cc0000">1997</font>
+
(* Author:   Tao Yue
<font color="#000000">  3:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Description:</font>
+
  Date:     19 July 1997
<font color="#000000">  4:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Find</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">first</font><font color="#cc0000"> </font><font color="#cc0000">10</font><font color="#cc0000"> </font><font color="#cc0000">Fibonacci</font><font color="#cc0000"> </font><font color="#cc0000">numbers</font>
+
  Description:
<font color="#990066">  5:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Version:</font>
+
      Find the first 10 Fibonacci numbers
<font color="#000000">  6:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">1.0</font><font color="#cc0000"> </font><font color="#cc0000">-</font><font color="#cc0000"> </font><font color="#cc0000">original</font><font color="#cc0000"> </font><font color="#cc0000">version</font>
+
  Version:
<font color="#000000">  7:</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
      1.0 - original version
<font color="#000000">  8:</font>
+
*)
<font color="#000000">  9:</font> <font color="#006699"><strong>program</strong></font> Fibonacci<font color="#000000"><strong>;</strong></font>
+
 
<font color="#990066">  10:</font>
+
program Fibonacci;
<font color="#000000">  11:</font> <font color="#006699"><strong>var</strong></font>
+
 
<font color="#000000">  12:</font>  Fibonacci1<font color="#000000"><strong>,</strong></font> Fibonacci2 <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
var
<font color="#000000">  13:</font>  temp <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
  Fibonacci1, Fibonacci2 : integer;
<font color="#000000">  14:</font>  count <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
  temp : integer;
<font color="#990066">  15:</font>
+
  count : integer;
<font color="#000000">  16:</font> <font color="#006699"><strong>begin</strong></font>   <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
 
<font color="#000000">  17:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">First</font><font color="#ff00cc"> </font><font color="#ff00cc">ten</font><font color="#ff00cc"> </font><font color="#ff00cc">Fibonacci</font><font color="#ff00cc"> </font><font color="#ff00cc">numbers</font><font color="#ff00cc"> </font><font color="#ff00cc">are:</font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
begin    (* Main *)
<font color="#000000">  18:</font>  count <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">0</font><font color="#000000"><strong>;</strong></font>
+
  writeln ('First ten Fibonacci numbers are:');
<font color="#000000">  19:</font>  Fibonacci1 <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">0</font><font color="#000000"><strong>;</strong></font>
+
  count := 0;
<font color="#990066">  20:</font>  Fibonacci2 <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">1</font><font color="#000000"><strong>;</strong></font>
+
  Fibonacci1 := 0;
<font color="#000000">  21:</font>  <font color="#006699"><strong>repeat</strong></font>
+
  Fibonacci2 := 1;
<font color="#000000">  22:</font>    <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font>Fibonacci2<font color="#000000"><strong>:</strong></font><font color="#ff0000">7</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
  repeat
<font color="#000000">  23:</font>    temp <font color="#000000"><strong>:=</strong></font> Fibonacci2<font color="#000000"><strong>;</strong></font>
+
      write (Fibonacci2:7);
<font color="#000000">  24:</font>    Fibonacci2 <font color="#000000"><strong>:=</strong></font> Fibonacci1 <font color="#000000"><strong>+</strong></font> Fibonacci2<font color="#000000"><strong>;</strong></font>
+
      temp := Fibonacci2;
<font color="#990066">  25:</font>    Fibonacci1 <font color="#000000"><strong>:=</strong></font> Temp<font color="#000000"><strong>;</strong></font>
+
      Fibonacci2 := Fibonacci1 + Fibonacci2;
<font color="#000000">  26:</font>    count <font color="#000000"><strong>:=</strong></font> count <font color="#000000"><strong>+</strong></font> <font color="#ff0000">1</font>
+
      Fibonacci1 := Temp;
<font color="#000000">  27:</font>  <font color="#006699"><strong>until</strong></font> count <font color="#000000"><strong>=</strong></font> <font color="#ff0000">10</font><font color="#000000"><strong>;</strong></font>
+
      count := count + 1
<font color="#000000">  28:</font>  writeln<font color="#000000"><strong>;</strong></font>
+
  until count = 10;
<font color="#000000">  29:</font>
+
  writeln;
<font color="#990066">  30:</font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Of</font><font color="#cc0000"> </font><font color="#cc0000">course,</font><font color="#cc0000"> </font><font color="#cc0000">you</font><font color="#cc0000"> </font><font color="#cc0000">could</font><font color="#cc0000"> </font><font color="#cc0000">use</font><font color="#cc0000"> </font><font color="#cc0000">a</font><font color="#cc0000"> </font><font color="#cc0000">FOR</font><font color="#cc0000"> </font><font color="#cc0000">loop</font><font color="#cc0000"> </font><font color="#cc0000">or</font><font color="#cc0000"> </font><font color="#cc0000">a</font><font color="#cc0000"> </font><font color="#cc0000">WHILE</font><font color="#cc0000"> </font><font color="#cc0000">loop</font>
+
 
<font color="#000000">  31:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">to</font><font color="#cc0000"> </font><font color="#cc0000">solve</font><font color="#cc0000"> </font><font color="#cc0000">this</font><font color="#cc0000"> </font><font color="#cc0000">problem.</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
  (* Of course, you could use a FOR loop or a WHILE loop
<font color="#000000">  32:</font>
+
      to solve this problem. *)
<font color="#000000">  33:</font> <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>     <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
+
 
 +
end.    (* Main *)  
 +
</delphi>
  
 
'''Solution to Powers of Two Problem'''
 
'''Solution to Powers of Two Problem'''
 +
<delphi>
 +
(* Author:    Tao Yue
 +
  Date:      13 July 2000
 +
  Description:
 +
      Display all powers of two up to 20000, five per line
 +
  Version:
 +
      1.0 - original version
 +
*)
 +
 +
program PowersofTwo;
 +
 +
const
 +
  numperline = 5;
 +
  maxnum = 20000;
 +
  base = 2;
 +
 +
var
 +
  number : longint;
 +
  linecount : integer;
 +
 +
begin    (* Main *)
 +
  writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
 +
  (* Set up for loop *)
 +
  number := 1;
 +
  linecount := 0;
 +
  (* Loop *)
 +
  while number <= maxnum do
 +
      begin
 +
        linecount := linecount + 1;
 +
        (* Print a comma and space unless this is the first
 +
            number on the line *)
 +
        if linecount > 1 then
 +
            write (', ');
 +
        (* Display the number *)
 +
        write (number);
 +
        (* Print a comma and go to the next line if this is
 +
            the last number on the line UNLESS it is the
 +
            last number of the series *)
 +
        if (linecount = numperline) and not (number * 2 > maxnum) then
 +
            begin
 +
              writeln (',');
 +
              linecount := 0
 +
            end;
 +
        (* Increment number *)
 +
        number := number * base;
 +
      end;  (* while *)
 +
  writeln;
 +
 +
  (* This program can also be written using a
 +
      REPEAT..UNTIL loop. *)
  
<font color="#000000">  1:</font> <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Author:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Tao</font><font color="#cc0000"> </font><font color="#cc0000">Yue</font>
+
end.    (* Main *)  
<font color="#000000">  2:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Date:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">13</font><font color="#cc0000"> </font><font color="#cc0000">July</font><font color="#cc0000"> </font><font color="#cc0000">2000</font>
+
</delphi>
<font color="#000000">  3:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Description:</font>
 
<font color="#000000">  4:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Display</font><font color="#cc0000"> </font><font color="#cc0000">all</font><font color="#cc0000"> </font><font color="#cc0000">powers</font><font color="#cc0000"> </font><font color="#cc0000">of</font><font color="#cc0000"> </font><font color="#cc0000">two</font><font color="#cc0000"> </font><font color="#cc0000">up</font><font color="#cc0000"> </font><font color="#cc0000">to</font><font color="#cc0000"> </font><font color="#cc0000">20000,</font><font color="#cc0000"> </font><font color="#cc0000">five</font><font color="#cc0000"> </font><font color="#cc0000">per</font><font color="#cc0000"> </font><font color="#cc0000">line</font>
 
<font color="#000000">  5:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">Version:</font>
 
<font color="#000000">  6:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">1.0</font><font color="#cc0000"> </font><font color="#cc0000">-</font><font color="#cc0000"> </font><font color="#cc0000">original</font><font color="#cc0000"> </font><font color="#cc0000">version</font>
 
<font color="#000000">  7:</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  8:</font>
 
<font color="#000000">  9:</font> <font color="#006699"><strong>program</strong></font> PowersofTwo<font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  10:</font>
 
<font color="#000000">  11:</font> <font color="#006699"><strong>const</strong></font>
 
<font color="#000000">  12:</font>  numperline <font color="#000000"><strong>=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  13:</font>  maxnum <font color="#000000"><strong>=</strong></font> <font color="#ff0000">20000</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  14:</font>  base <font color="#000000"><strong>=</strong></font> <font color="#ff0000">2</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  15:</font>
 
<font color="#000000">  16:</font> <font color="#006699"><strong>var</strong></font>
 
<font color="#000000">  17:</font>  number <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>longint</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  18:</font>  linecount <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  19:</font>
 
<font color="#000000">  20:</font> <font color="#006699"><strong>begin</strong></font>    <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  21:</font>  writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">Powers</font><font color="#ff00cc"> </font><font color="#ff00cc">of</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> base<font color="#000000"><strong>,</strong></font> <font color="#ff00cc">'</font><font color="#ff00cc">,</font><font color="#ff00cc"> </font><font color="#ff00cc">1</font><font color="#ff00cc"> </font><font color="#ff00cc">&lt;=</font><font color="#ff00cc"> </font><font color="#ff00cc">x</font><font color="#ff00cc"> </font><font color="#ff00cc">&lt;=</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>,</strong></font> maxnum<font color="#000000"><strong>,</strong></font> <font color="#ff00cc">'</font><font color="#ff00cc">:</font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  22:</font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Set</font><font color="#cc0000"> </font><font color="#cc0000">up</font><font color="#cc0000"> </font><font color="#cc0000">for</font><font color="#cc0000"> </font><font color="#cc0000">loop</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  23:</font>  number <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">1</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  24:</font>  linecount <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">0</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  25:</font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Loop</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  26:</font>  <font color="#006699"><strong>while</strong></font> number <font color="#000000"><strong>&lt;</strong></font><font color="#000000"><strong>=</strong></font> maxnum <font color="#006699"><strong>do</strong></font>
 
<font color="#000000">  27:</font>  <font color="#006699"><strong>begin</strong></font>
 
<font color="#000000">  28:</font>    linecount <font color="#000000"><strong>:=</strong></font> linecount <font color="#000000"><strong>+</strong></font> <font color="#ff0000">1</font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  29:</font>    <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Print</font><font color="#cc0000"> </font><font color="#cc0000">a</font><font color="#cc0000"> </font><font color="#cc0000">comma</font><font color="#cc0000"> </font><font color="#cc0000">and</font><font color="#cc0000"> </font><font color="#cc0000">space</font><font color="#cc0000"> </font><font color="#cc0000">unless</font><font color="#cc0000"> </font><font color="#cc0000">this</font><font color="#cc0000"> </font><font color="#cc0000">is</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">first</font>
 
<font color="#000000">  30:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">number</font><font color="#cc0000"> </font><font color="#cc0000">on</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">line</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  31:</font>    <font color="#006699"><strong>if</strong></font> linecount <font color="#000000"><strong>&gt;</strong></font> <font color="#ff0000">1</font> <font color="#006699"><strong>then</strong></font>
 
<font color="#000000">  32:</font>      <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">,</font><font color="#ff00cc"> </font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  33:</font>    <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Display</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">number</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  34:</font>    <font color="#009966"><strong>write</strong></font> <font color="#000000"><strong>(</strong></font>number<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  35:</font>    <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Print</font><font color="#cc0000"> </font><font color="#cc0000">a</font><font color="#cc0000"> </font><font color="#cc0000">comma</font><font color="#cc0000"> </font><font color="#cc0000">and</font><font color="#cc0000"> </font><font color="#cc0000">go</font><font color="#cc0000"> </font><font color="#cc0000">to</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">next</font><font color="#cc0000"> </font><font color="#cc0000">line</font><font color="#cc0000"> </font><font color="#cc0000">if</font><font color="#cc0000"> </font><font color="#cc0000">this</font><font color="#cc0000"> </font><font color="#cc0000">is</font>
 
<font color="#000000">  36:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">last</font><font color="#cc0000"> </font><font color="#cc0000">number</font><font color="#cc0000"> </font><font color="#cc0000">on</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">line</font><font color="#cc0000"> </font><font color="#cc0000">UNLESS</font><font color="#cc0000"> </font><font color="#cc0000">it</font><font color="#cc0000"> </font><font color="#cc0000">is</font><font color="#cc0000"> </font><font color="#cc0000">the</font>
 
<font color="#000000">  37:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">last</font><font color="#cc0000"> </font><font color="#cc0000">number</font><font color="#cc0000"> </font><font color="#cc0000">of</font><font color="#cc0000"> </font><font color="#cc0000">the</font><font color="#cc0000"> </font><font color="#cc0000">series</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  38:</font>    <font color="#006699"><strong>if</strong></font> <font color="#000000"><strong>(</strong></font>linecount <font color="#000000"><strong>=</strong></font> numperline<font color="#000000"><strong>)</strong></font> <font color="#006699"><strong>and</strong></font> <font color="#006699"><strong>not</strong></font> <font color="#000000"><strong>(</strong></font>number <font color="#000000"><strong>*</strong></font> <font color="#ff0000">2</font> <font color="#000000"><strong>&gt;</strong></font> maxnum<font color="#000000"><strong>)</strong></font> <font color="#006699"><strong>then</strong></font>
 
<font color="#000000">  39:</font>    <font color="#006699"><strong>begin</strong></font>
 
<font color="#000000">  40:</font>      writeln <font color="#000000"><strong>(</strong></font><font color="#ff00cc">'</font><font color="#ff00cc">,</font><font color="#ff00cc">'</font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  41:</font>      linecount <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">0</font>
 
<font color="#000000">  42:</font>    <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  43:</font>    <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Increment</font><font color="#cc0000"> </font><font color="#cc0000">number</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  44:</font>    number <font color="#000000"><strong>:=</strong></font> number <font color="#000000"><strong>*</strong></font> base<font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  45:</font>  <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">while</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  46:</font>  writeln<font color="#000000"><strong>;</strong></font>
 
<font color="#000000">  47:</font>
 
<font color="#000000">  48:</font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">This</font><font color="#cc0000"> </font><font color="#cc0000">program</font><font color="#cc0000"> </font><font color="#cc0000">can</font><font color="#cc0000"> </font><font color="#cc0000">also</font><font color="#cc0000"> </font><font color="#cc0000">be</font><font color="#cc0000"> </font><font color="#cc0000">written</font><font color="#cc0000"> </font><font color="#cc0000">using</font><font color="#cc0000"> </font><font color="#cc0000">a</font>
 
<font color="#000000">  49:</font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000"> </font><font color="#cc0000">REPEAT..UNTIL</font><font color="#cc0000"> </font><font color="#cc0000">loop.</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
<font color="#000000">  50:</font>
 
<font color="#000000">  51:</font> <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>     <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Main</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
  
 
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.
 
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.

Revision as of 17:14, 5 January 2010

3Ea - Solutions (author: Tao Yue, state: unchanged)

Solution to Fibonacci Sequence Problem <delphi> (* Author: Tao Yue

  Date:      19 July 1997
  Description:
     Find the first 10 Fibonacci numbers
  Version:
     1.0 - original version
  • )

program Fibonacci;

var

  Fibonacci1, Fibonacci2 : integer;
  temp : integer;
  count : integer;

begin (* Main *)

  writeln ('First ten Fibonacci numbers are:');
  count := 0;
  Fibonacci1 := 0;
  Fibonacci2 := 1;
  repeat
     write (Fibonacci2:7);
     temp := Fibonacci2;
     Fibonacci2 := Fibonacci1 + Fibonacci2;
     Fibonacci1 := Temp;
     count := count + 1
  until count = 10;
  writeln;
  (* Of course, you could use a FOR loop or a WHILE loop
     to solve this problem. *)

end. (* Main *) </delphi>

Solution to Powers of Two Problem <delphi> (* Author: Tao Yue

  Date:      13 July 2000
  Description:
     Display all powers of two up to 20000, five per line
  Version:
     1.0 - original version
  • )

program PowersofTwo;

const

  numperline = 5;
  maxnum = 20000;
  base = 2;

var

  number : longint;
  linecount : integer;

begin (* Main *)

  writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
  (* Set up for loop *)
  number := 1;
  linecount := 0;
  (* Loop *)
  while number <= maxnum do
     begin
        linecount := linecount + 1;
        (* Print a comma and space unless this is the first
           number on the line *)
        if linecount > 1 then
           write (', ');
        (* Display the number *)
        write (number);
        (* Print a comma and go to the next line if this is
           the last number on the line UNLESS it is the
           last number of the series *)
        if (linecount = numperline) and not (number * 2 > maxnum) then
           begin
              writeln (',');
              linecount := 0
           end;
        (* Increment number *)
        number := number * base;
     end;  (* while *)
  writeln;
  (* This program can also be written using a
     REPEAT..UNTIL loop. *)

end. (* Main *) </delphi>

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)

previous contents next