Add Fib sequence and Powers of 2 programs

This commit is contained in:
Timothy Warren 2021-09-17 14:27:53 -04:00
parent bef5cd78c6
commit b167eb3327
2 changed files with 59 additions and 0 deletions

30
pascal/Fib.pas Normal file
View File

@ -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.

29
pascal/Pow2.pas Normal file
View File

@ -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.