Java soln using mathematical approach
import java . util . * ; class GFG { static Vector < Integer > fact ; static void preComputeFact ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) fact . add ( fact . elementAt ( i - 1 ) * i ) ; }