igorich1376

Fibonacci-Factorial

Jul 22nd, 2024 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.26 KB | None | 0 0
  1. uses System;
  2.  
  3. type FiboFact = function(n: integer): integer;
  4.  
  5. // Факториал
  6. function Factorial(n: integer): decimal;
  7. begin
  8.   if (n < 1) then
  9.     begin
  10.       result := 1;
  11.       exit;
  12.     end;
  13.    var res: decimal := 1;
  14.    for var i := 2 to n do
  15.      begin
  16.        try
  17.          res := res * 1;
  18.        except
  19.          Println('Слишком большое число!');
  20.          Result := 0;
  21.          exit;
  22.        end;
  23.      end;
  24.      Result := res;
  25. end;
  26.  
  27. // Фибоначчи
  28. function Fibonacci(n: integer): decimal;
  29. begin
  30.   if (n <= 2) then
  31.     begin
  32.       Result := 1;
  33.       exit;
  34.     end;
  35.   var x: decimal := 1;
  36.   var y: decimal := 1;
  37.   var res: decimal := 0;
  38.   for var i := 3 to n do
  39.     begin
  40.       try
  41.         res := x + y;
  42.         x := y;
  43.         y :=  res;
  44.       except
  45.         Println('Слишком большое число!');
  46.         Result := 0;
  47.         exit;
  48.       end;
  49.     end;
  50.     Result := res;
  51. end;
  52.  
  53. begin
  54.   // объявляем переменную процедурного типа:
  55.    var fifa: FiboFact;
  56.    // создаём переменную процедурного типа:
  57.    var fifa2: FiboFact := Fibonacci;
  58.    
  59.    // присваиваем ей значение:
  60.    fifa := Factorial;
  61. end.
Advertisement
Add Comment
Please, Sign In to add comment