Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #31 Recursive factorial (simple)

Create the recursive function f which returns the factorial of the non-negative integer i, calculated from f(i-1)

def f(i) { i == 0 ? 1 : i * f(i - 1) }
function F (I : Natural) return Natural is (if I < 2 then 1 else I * F (I - 1));
unsigned int f(unsigned int i)
{
	return i?i*f(i-1):1;
}
(defn f [i]
  (loop [cnt i, acc 1N]
    (if (zero? cnt) acc
      (recur (dec cnt) (* cnt acc)))))
unsigned int f( unsigned int i ) {
	if ( i == 0 ) return 1;
	
	return i * f( i - 1 );
}
int f(int i)
{
    if (i == 0) return 1;
    return i * f(i - 1);
}
int f(int i) => i == 0 ? 1 : i * f(i - 1)
uint f(in uint i) pure nothrow @nogc
in {
    assert(i <= 12);
} body {
    if (i == 0)
        return 1;
    else
        return i * f(i - 1);
}
f(i) => (i == 0) ? 1 : i * f(i - 1);
defmodule Factorial do
  def f(0), do: 1
  def f(i) when i > 0 do
    n * f(i-1)
  end
end
f(0) -> 1;
f(I) -> I * f(I - 1).
module x
  implicit none
contains
  recursive function f (i) result (res)
    integer, intent(in) :: i
    integer :: res
    if (i <= 0) then
       res = 1
    else
       res = f(i-1) * i
    end if
  end function f
end module x
func f(i int) int {
  if i == 0 {
    return 1
  }
  return i * f(i-1)
}
f i = if i > 1 then f (i-1) * i else 1
function f(i) {
   return i<2 ? 1 : i * f(i-1);
}
const f = i => i === 0 ? 1 : i * f(i-1)
int f(int i) {
    if (i == 0)
        return 1;
    else
        return i * f(i - 1);
}
fun f(i: Int): Int = when (i) {
    0 -> 1
    else -> i * f(i - 1)
}
fun f(i: Int) = if (i == 0) 1 else i * f(i - 1)
(defun f (i)
  (if (< i 2)
    1
    (* i (f (- i 1)))))
function f(n)
   local function f_(factorial,n)
      if n < 2 then return 1
      return f_(factorial*n, n-1)
   end
   return f_(1,n)
end
unsigned f(unsigned i) {
  return i?i*f(i-1):1;
}
function f($i) {
	if($i == 0) {
		return 1;
	}

	return ($i * f($i-1));
}
function f(int $i): int
{
    if ($i == 0) {
        return 1;
    }

    return $i * f($i - 1);
}
type
  TPositiveInt = 0..MaxInt;

function _f(_i: TPositiveInt): Integer;
begin
  if (_i < 2) then
    Result := 1
  else
    Result := _f(_i - 1);
end; 
sub f {
   my $i = shift;
   return $i<2 ? 1 : $i * f($i-1);
}
def f(i):
   if i == 0:
       return 1
   else:
       return i * f(i-1)
fac = Hash.new {|h, i| h[i] = i * h[i-1] }.tap {|h| h[0] = 1 }
f = Hash.new { |hash, i| hash[i] = i * hash[i -1] }
f[0] = 1
fn factorial(num: u64) -> u64 {
    match num {
        0 | 1 => 1,
        _ => factorial(num - 1) * num,
    }
}
fn f(n: u32) -> u32 {
    if n < 2 {
        1
    } else {
        n * f(n - 1)
    }
}
def f(i: Int): Int =
  if (i > 1){
    i * f(i-1) 
  } else{
    1
  }
(define (f i)
  (if (> i 1)
      (* (f (- i 1)) i)
      1))
f := [:i | i = 0 ifTrue: [1] ifFalse: [i * (f value: i - 1)]].
Function f(i As Integer) As Integer
    Return If(i = 0, 1, i * f(i - 1))
End Function

New implementation...
< >
programming-idioms.org