class Factorial_G { static iterative ( BigInteger n ) { if ( n < 0G ) { throw new FactorialIllegalArgumentException ( 'Parameter must be a non-negative integer.' ) } def total = 1G if ( n > 1G ) { for ( i in 2G..n ) { total *= i } } total } static iterative ( Integer n ) { iterative ( new BigInteger ( n.toString ( ) ) ) } static iterative ( Long n ) { iterative ( new BigInteger ( n.toString ( ) ) ) } static recursive ( BigInteger n ) { if ( n < 0G ) { throw new FactorialIllegalArgumentException ( 'Parameter must be a non-negative integer.' ) } n <= 1G ? 1G : n * recursive ( n - 1 ) } static recursive ( Integer n ) { recursive ( new BigInteger ( n.toString ( ) ) ) } static recursive ( Long n ) { recursive ( new BigInteger ( n.toString ( ) ) ) } static tailRecursive ( BigInteger n ) { if ( n < 0G ) { throw new FactorialIllegalArgumentException ( 'Parameter must be a non-negative integer.' ) } def iterate iterate = { BigInteger i , BigInteger result -> i < 2G ? result : iterate ( i - 1G , result * i ) } iterate ( n , 1G ) } static tailRecursive ( Integer n ) { tailRecursive ( new BigInteger ( n.toString ( ) ) ) } static tailRecursive ( Long n ) { tailRecursive ( new BigInteger ( n.toString ( ) ) ) } }