How to find the next number in the series 14, 25, 47, 91 with Pascal

1 Answer

0 votes
program NextInSeries;

{
25 - 14 = 11
47 - 25 = 22
91 - 47 = 44

11 -> 22 -> 44 -> 88

91 + 88 = 179
}

{
  Compute the next number in the series using the rule:
    - Differences double each step.
    - For the given sequence:
        14, 25, 47, 91
        Differences: 11, 22, 44
        Next difference = 44 * 2 = 88
        Next value = 91 + 88 = 179
}

function NextInSeries(const arr: array of Integer): Integer;
var
  lastDiff: Integer;
  size: Integer;
begin
  size := Length(arr);

  if size < 2 then
  begin
    WriteLn('Not enough data to compute next value.');
    Halt(1);
  end;

  { Compute the last difference }
  lastDiff := arr[size - 1] - arr[size - 2];

  { Apply doubling rule }
  NextInSeries := arr[size - 1] + (lastDiff * 2);
end;

var
  series: array[0..3] of Integer = (14, 25, 47, 91);
  nextValue: Integer;

begin
  nextValue := NextInSeries(series);
  WriteLn('Next number in the series: ', nextValue);
end.



(*
run:

Next number in the series: 179

*)

 



answered 6 days ago by avibootz
...