From b167eb332713573a3a557e5a42953c9be81cab5f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 17 Sep 2021 14:27:53 -0400 Subject: [PATCH] Add Fib sequence and Powers of 2 programs --- pascal/Fib.pas | 30 ++++++++++++++++++++++++++++++ pascal/Pow2.pas | 29 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 pascal/Fib.pas create mode 100644 pascal/Pow2.pas diff --git a/pascal/Fib.pas b/pascal/Fib.pas new file mode 100644 index 0000000..a02c146 --- /dev/null +++ b/pascal/Fib.pas @@ -0,0 +1,30 @@ +{ Prints Fibonacci sequence up to 10th number } +program Fib; + +const + ZERO = 1; + ONE = 1; + +var + i, prev, min2, this: integer; + +begin + // 0 and 1 are pre-defined, so start at 2 + i := 2; + write('1 1'); + prev := ONE; + min2 := ZERO; + + while i < 11 do + begin + this := prev + min2; + min2 := prev; + prev := this; + + write(' ', this); + + i := i + 1; + end; + + writeln; +end. diff --git a/pascal/Pow2.pas b/pascal/Pow2.pas new file mode 100644 index 0000000..abbfe2a --- /dev/null +++ b/pascal/Pow2.pas @@ -0,0 +1,29 @@ +{ Prints powers of 2 less than 20,000 } +program Pow2; + +const + ZERO = 1; + +var + prev, this, i: integer; + +begin + prev := ZERO; + + for i := 1 to 15 do + begin + // Each loop is 2^i-1 + if i = 1 then + this := prev + else + this := prev * 2; + + prev := this; + + case i of + 15: writeln(this:6); + 5, 10: writeln(this:6, ','); + otherwise write(this:6, ', ') + end; + end; +end. \ No newline at end of file