Hi guys, so yeah, I got some IT homework AGAIN on programs and I repeatedly did trace tables and they all worked, but somehow, when I enter it in turbo pascal, after the 10th number, the digits start to get negative and after the 6th number, its not following the sequence... Anyways, the question states: Write a program to generate the first 20 terms of the following sequence: 1,3,3,9,27,243,6561.........here's the program that I did:

program sequence;

uses wincrt;

var X,Y,Result,i:integer;

Begin

 X:=1;
 Y:=3;
 Result:=X*Y;
 Writeln(X);
 Writeln(Y);
 For i:=1 to 18 do
 Begin
      Y:=X;
      X:=Result;
      Result:=X*Y;
      Writeln(Result);
 End;
End.

Dani AI

Generated

and were on the right track: the unexpected negatives come from built‑in integer overflow (wrap‑around). A neat extra insight is that the sequence has a closed form: a_n = 3^(Fib(n-1)) (with Fib(0)=0, Fib(1)=1). That gives exact exponents — e.g. a9 = 3^21 = 10,460,353,203 and a10 = 3^34 = 16,677,181,699,666,569 — and explains the ridiculous growth: a20 = 3^4181 (about 1,995 decimal digits). Built‑in 16/32/64‑bit types exhaust their ranges long before term 20, so the observed corruption is expected.

Practical options:

  • Short fix: switch to a wider integer type (LongInt/Int64) — this only delays overflow (Int64 reaches maybe term 10).
  • If exact decimal terms are required, use arbitrary‑precision arithmetic (a big‑integer library) or implement big integers (array of decimal digits).
  • If only size is needed, compute Fib(n-1) and report a_n as “3^exp” or compute digit count with log10: digits(a_n) = floor(Fib(n-1)*log10(3)) + 1.

Below is a compact Pascal approach that stores each big integer as reversed decimal digits in an array and multiplies with grade‑school arithmetic. MAXD must be >= 1995 to hold term 20; adjust if longer output is needed.

const MAXD = 2100;
type TBig = array[1..MAXD] of byte;

procedure InitFromString(s: string; var B: TBig; var Len: integer);
var i: integer;
begin
  Len := Length(s);
  for i := 1 to Len do B[i] := Ord(s[Len - i + 1]) - Ord('0'); {reversed digits}
  for i := Len + 1 to MAXD do B[i] := 0;
end;

procedure PrintBig(const B: TBig; Len: integer);
var i: integer;
begin
  for i := Len downto 1 do Write(Chr(B[i] + Ord('0')));
  Writeln;
end;

procedure MulBig(const A: TBig; LenA: integer; const B: TBig; LenB: integer; var R: TBig; var LenR: integer);
var i,j,carry,tmp: integer;
begin
  for i := 1 to LenA + LenB do R[i] := 0;
  for i := 1 to LenA do
  begin
    carry := 0;
    for j := 1 to LenB do
    begin
      tmp := R[i + j - 1] + A[i] * B[j] + carry;
      R[i + j - 1] := tmp mod 10;
      carry := tmp div 10;
    end;
    R[i + LenB] := R[i + LenB] + carry;
  end;
  LenR := LenA + LenB;
  while (LenR > 1) and (R[LenR] = 0) do Dec(LenR);
end;

{ usage: start with '1' and '3', print them, then loop i=3..20: next := B*A; shift A:=B, B:=next }

This prints exact decimal terms. If the teacher only cares about pattern or growth, stating a_n = 3^(Fib(n-1)) or giving digit counts is a concise, mathematically exact answer.

Recommended Answers

All 3 Replies

The digits get negative because the integer type cannot hold such large values.

In a way it makes sense!
An integer cannot hold numbers as big as 10^300 and higher, hence the negative values

ooooooooooooo Thanks guys... So i'll just tell the teacher that then!!! You guys deserve some reward lol :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.