How to find the first 10 prime Fibonacci numbers in Pascal

1 Answer

0 votes
program PrimeFibonacci;

{$mode objfpc}

function IsPrime(n: QWord): Boolean;
var
  i: QWord;
begin
  if n < 2 then Exit(False);
  if (n = 2) or (n = 3) then Exit(True);
  if n mod 2 = 0 then Exit(False);

  i := 3;
  while i * i <= n do
  begin
    if n mod i = 0 then Exit(False);
    Inc(i, 2);
  end;

  Result := True;
end;

function NextFib(var a, b: QWord): QWord;
var
  f, nextVal: QWord;
begin
  f := a;
  nextVal := a + b;
  a := b;
  b := nextVal;
  
  NextFib := f;
end;

var
  a, b, f: QWord;
  count: Integer;
begin
  a := 1;
  b := 1;
  count := 0;

  while count < 10 do
  begin
    f := NextFib(a, b);
    if IsPrime(f) then
    begin
      Write(f, ' ');
      Inc(count);
    end;
  end;
end.



(*
run:

2 3 5 13 89 233 1597 28657 514229 433494437 

*)

 



answered 9 hours ago by avibootz
edited 9 hours ago by avibootz
...