With Flow.apex, recursion is not difficult to achieve.
Flow f = new Flow() .inputAs('n').returnInteger() .doIf( 'n == 0', Flow.block() .doReturn(0) ) .var('ret = 2 + this(n - 1)') .doReturn('ret');