Factorial in Erlang

Published on 28 April 2022 (Updated: 18 September 2022)

Factorial in Erlang

Welcome to the Factorial in Erlang page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

-module(factorial).
-export([start/1]).

-spec start(Number :: integer()) -> integer().
start(N) 
    when N<0;
         not is_integer(N) ->
    io:format("Usage: please input a non-negative integer~n");
start(0) ->
    factorial(1,1);
start(N) ->
    factorial(N,N).

%%--------------------------------------------------------------------
%% Recursively multiply N times N-1 until N-1=1. Output Accumulator
%%--------------------------------------------------------------------
factorial(1,Acc) ->
    io:format("~w~n", [Acc]);
factorial(N,Acc) ->
    factorial(N-1, (N-1)*Acc).

Factorial in Erlang was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.