
-- iterative factorial function in PLpgSQL

create or replace function
   ifac(n integer) returns integer
as $$
declare
   i   integer;
   fac integer := 1;
begin
   for i in 2..n loop
      fac = fac * i;
   end loop;
   return fac;
end;
$$ language plpgsql;



-- recursive factorial function in PLpgSQL

create or replace function
   rfac(n integer) returns integer
as $$
begin
   if (n < 2) then
      return 1;
   else
      return n * rfac(n-1);
   end if;
end;
$$ language plpgsql;
