Count unimodal and non | JavaScript program for the above approach ; Function to calculate the factorials up to a number ; Calculate the factorial ; Function to find power ( a , b ) ; Iterate until b exists ; If b is divisible by 2 ; Decrease the value of b ; Return the answer ; Function that counts the unimodal and non - unimodal permutations of a given integer N ; Function Call for finding factorials up to N ; Function to count unimodal permutations ; Non - unimodal permutation is N ! - unimodal permutations ; Driver Code Given Number N ; Function Call
var mod = parseInt ( 1e9 + 7 ) ; var mx = 1000000 ; var fact = new Array ( mx + 1 ) . fill ( 0 ) ; function Calculate_factorial ( ) { fact [ 0 ] = 1 ; for ( var i = 1 ; i <= mx ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; fact [ i ] %= mod ; } } function UniModal_per ( a , b ) { var res = 1 ; while ( b > 0 ) { if ( b % 2 !== 0 ) res = res * a ; res %= mod ; a = a * a ; a %= mod ; b = parseInt ( b / 2 ) ; } return res ; } function countPermutations ( n ) { Calculate_factorial ( ) ; var uni_modal = UniModal_per ( 2 , n - 1 ) ; var nonuni_modal = fact [ n ] - uni_modal ; document . write ( uni_modal + " " + nonuni_modal ) ; return ; } var N = 4 ; countPermutations ( N ) ;