Sub Factoriel() Dim a As Integer, b As Integer, i As Integer a = InputBox("Entrez le nombre a tester") b = 1 For i = 2 To a b = i * b Next MsgBox "Le factoriel de " & a & " est " & b End Sub
alias Fact { var %i = 2, %b = 1 while %i <= $1 { var %b = $calc(%i * %b) | inc %i } return %b }
function Factorielle(byval pNb as integer) as integer If pNb=1 Then Return 1 Else Return pNb*Factorielle(pNb-1) End If End Function
// Remarque: Ce code est assez élégant (1 ligne) mais n'est pas le // plus performant (récursion oblige). Pour les petits nombres cependant, // il est plus rapide qu'une boucle while (d'après mes testes, jusqu'à une // valeur de 1000 environ) private long Factoriel(int nb) { return nb > 1 ? nb * this.Factoriel(nb - 1) : nb; }
public long Factor(int nb) { long Res = 1; while(nb > 1)Res *= nb--; return Res; }
Function GetFactoriel(lVal As Long) As Long GetFactoriel = 1 Dim i& For i = 2 To lVal GetFactoriel = i * GetFactoriel Next i End Function
def Fact(nb): res=1 for i in range(1, nb+1): res *=i return res
function fact(n) { return (n==1)?1:n*fact(n-1); }
function FFact(const N : integer { max = 1754}) : extended; var i : integer; begin result := 1; for i := 1 to N do result := result * i; end;
function factoriel(num:Number){ return num > 1 ? num*factoriel(num-1) : num; }
(* Récursive terminale. Exemple d'utilisation : factorielle 5 donne 120 *) let factorielle = let rec loop result = function | 0 -> result | n -> loop (n * result) (n - 1) in loop 1
template <class T, int N> class Math{ public : static T factorielle(){ T a=N; return Math<T, N-1>::factorielle()*a; } }; template <class T> class Math<T, 0>{ public : static T factorielle(){ T a=1; return a; } };
public static long Fact(int a) { // méthode propre long b=1; for(int i=1;i<=a;i++) { b = b*i; } return b; } public static long Fact2(int nb) { // méthode moins propre return nb > 1 ? nb * Fact2(nb - 1) : nb; } public static void main(String[ ] args) { System.out.println(Fact(20)); }
#include <iostream> using namespace std; #include <gmpxx.h> typedef mpz_class Integer Integer factoriel (Integer n) { Integer i,b=1; for (i=1;i<=n;i++){ b=b*i; } return b; }