Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- C
- Clojure
- C++
- C#
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Groovy
- Haskell
- JS
- JS
- Java
- Kotlin
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- PHP
- Pascal
- Perl
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
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;
}
Overflows for i>20 in 64bits and for i>12 in 32bits
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);
}
uint f(in uint i) pure nothrow @nogc
in {
assert(i <= 12);
} body {
if (i == 0)
return 1;
else
return i * f(i - 1);
}
Assertion is used to prevent silent overflow.
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
iex > Factorial.f(100)
f(0) -> 1;
f(I) -> I * f(I - 1).
No "defensive programming" here.
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)
}
def f(i) { i == 0 ? 1 : 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);
}
Warnings :
- type int quickly overflows
- high number of recursive calls may cause a stack overflow
- also, f is not tail-recursive
- type int quickly overflows
- high number of recursive calls may cause a stack overflow
- also, f is not tail-recursive
(defun f (i)
(if (< i 2)
1
(* i (f (- i 1)))))
unsigned f(unsigned i) {
return i?i*f(i-1):1;
}
Precisely same as plain C
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;
The definition of type TPositiveInt allows for rangechecking on the input.
MaxInt is already defined in the language.
MaxInt is already defined in the language.
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
Note that f is not a function but plain old Hash used as a cache for performance.
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