Add Fib sequence and Powers of 2 programs
This commit is contained in:
parent
bef5cd78c6
commit
b167eb3327
|
@ -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.
|
|
@ -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.
|
Loading…
Reference in New Issue