-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfactorial.f90
41 lines (31 loc) · 1.36 KB
/
factorial.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
!> Factorial Module
!! This module implements factorial calculation functions.
!! https://en.wikipedia.org/wiki/Factorial
!! The module contains two functions: one for calculating factorial iteratively
!! and another one recursively.
module factorial_module
implicit none
contains
!! This function calculates the factorial of a given number using a loop.
function factorial(number) result(factorial_number)
integer, intent(in) :: number !! Number to calculate factorial of
integer :: factorial_number !! Resulting factorial
integer :: counter
counter = number
factorial_number = 1
do while (counter > 1)
factorial_number = factorial_number*counter
counter = counter - 1
end do
end function factorial
!! This function calculates the factorial of a given number using a recursive function.
recursive function recursive_factorial(number) result(factorial_number)
integer, intent(in) :: number !! Number to calculate factorial of
integer :: factorial_number !! Resulting factorial
if (number .lt. 1) then
factorial_number = 1
else
factorial_number = number*recursive_factorial(number - 1)
end if
end function recursive_factorial
end module factorial_module