text: Sum of divisors of factorial of a number | allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Create sieve ; Initialize result ; Find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allPrimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ Legendre ' s formula . ; Using the divisor function to calculate the sum ; Return total divisors ; Driver code
code: let allPrimes = [ ] ; function sieve ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = true ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) allPrimes . push ( p ) ; } function factorialDivisors ( n ) { sieve ( n ) ; let result = 1 ; for ( let i = 0 ; i < allPrimes . length ; i ++ ) { let p = allPrimes [ i ] ; let exp = 0 ; while ( p <= n ) { exp = exp + Math . floor ( n / p ) ; p = p * allPrimes [ i ] ; } result = Math . floor ( result * ( Math . pow ( allPrimes [ i ] , exp + 1 ) - 1 ) / ( allPrimes [ i ] - 1 ) ) ; } return result ; } document . write ( factorialDivisors ( 4 ) ) ;