Factorial using trampolines I've translated this Java code into Delphi code:

unit Trampoline;

interface

type
  ITrampoline&lt;T&gt; = interface
    function Get: T;
    function Run: ITrampoline&lt;T&gt;;
    function Execute: T;
  end;

  TTrampoline&lt;T&gt; = class(TInterfacedObject, ITrampoline&lt;T&gt;)
  public
    function Get: T; virtual;
    function Run: ITrampoline&lt;T&gt;; virtual;
    function Execute: T; virtual; final;
  end;

  TFactorial = class(TTrampoline&lt;integer&gt;)
  private
    fsum, fn: integer;
  public
    constructor Create(n, sum: integer);
    class function Factorial(n, sum: integer): ITrampoline&lt;Integer&gt;; static;
    function run: ITrampoline&lt;Integer&gt;; override;
  end;

implementation

uses System.Generics.Defaults;

function TTrampoline&lt;T&gt;.Run: ITrampoline&lt;T&gt;;
begin
  Result:= nil;
end;

function TTrampoline&lt;T&gt;.Get: T;
begin
  Result:= default (T);
end;

function TTrampoline&lt;T&gt;.Execute: T;
var
  Comparer: IEqualityComparer&lt;T&gt;;
  Trampoline: ITrampoline&lt;T&gt;;
begin
  Trampoline:= Self;
  Comparer:= TEqualityComparer&lt;T&gt;.Default;
  while Comparer.Equals(Trampoline.get, default (T)) do begin
    Trampoline:= Trampoline.run;
  end;
  Result:= Trampoline.get;
end;

type
  TDoneFactorial = class(TFactorial)
    function get: integer; override;
  end;

constructor TFactorial.Create(n, sum: integer);
begin
  fn:= n;
  fsum:= sum;
end;

class function TFactorial.Factorial(n, sum: integer): ITrampoline&lt;Integer&gt;;
begin
  if (n &lt;= 1) then begin
    Result:= TDoneFactorial.Create(n, sum);
  end else begin
    Result:= TFactorial.Create(n, sum);
  end;
end;

function TDoneFactorial.get: integer;
begin
  Result:= fsum;
end;

function TFactorial.run: ITrampoline&lt;Integer&gt;;
begin
  Result:= Factorial(fn - 1, fsum * fn);
end;

end.


Usage:

procedure TForm5.Button1Click(Sender: TObject);
var
  Outcome: Integer;
begin
  Outcome:= TFactorial.Factorial(StrToInt(Edit1.Text), 1).Execute;
  Label2.Caption:= IntToStr(OutCome);
end;


It runs nicely, in all the iterations of the execute method the stack pointer does not move and hence it will not generate a stack overflow no matter how many deep the recursion goes.  

It is rather inefficient though because of:


The implicit try - finally guarding the interface variable  
The constant creation of new structures on the heap.  


Is there a more efficient way to do this?

Never mind the fact that factorials are a stupid example for recursion.  I know recursion is only really useful with trees.
Recursion can make for elegant code, but it can also make for very slow code. Calculating Fibonacci numbers and Factorials are both classic examples where the recursive algorithm is simple to implement, but slow beyond the first few (and very small) input values. 

I don't know Delphi, so I quickly implemented this in VB6, but you should be able to consider it psuedo-code.

Function Factorial(x As Integer) As Long
    Dim result As Long
    Dim startIndex As Integer

    result = 1
    For startIndex = x To 1 Step -1
        result = result * startIndex
    Next

    Factorial = result
End Function