Check if factorial of N is divisible by the sum of squares of first N natural numbers | Function to count number of times prime P divide factorial N ; Lengendre Formula ; Function to find count number of times all prime P divide summation ; Formula for summation of square after removing n and constant 6 ; Loop to traverse over all prime P which divide summation ; If Number itself is a Prime Number ; Driver Code
function checkfact ( N , countprime , prime ) { var countfact = 0 ; if ( prime == 2 prime == 3 ) countfact ++ ; var divide = prime ; while ( N / divide != 0 ) { countfact += N / divide ; divide = divide * divide ; } if ( countfact >= countprime ) return true ; else return false ; } function check ( N ) { var sumsquares = ( N + 1 ) * ( 2 * N + 1 ) ; var countprime = 0 ; for ( i = 2 ; i <= Math . sqrt ( sumsquares ) ; i ++ ) { var flag = 0 ; while ( sumsquares % i == 0 ) { flag = 1 ; countprime ++ ; sumsquares /= i ; } if ( flag == 1 ) { if ( ! checkfact ( N - 1 , countprime , i ) ) return false ; countprime = 0 ; } } if ( sumsquares != 1 ) if ( ! checkfact ( N - 1 , 1 , sumsquares ) ) return false ; return true ; } var N = 5 ; if ( check ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;