Find maximum operations to reduce N to 1 | Javascript program to find maximum number moves possible ; To store number of prime factors of each number ; Function to find number of prime factors of each number ; if i is a prime number ; increase value by one from it 's preveious multiple ; make prefix sum this will be helpful for multiple test cases ; Generate primeFactors array ; required answer
let N = 1000005 ; let primeFactors = new Array ( N ) ; primeFactors . fill ( 0 ) ; function findPrimeFactors ( ) { for ( let i = 2 ; i < N ; i ++ ) if ( primeFactors [ i ] == 0 ) for ( let j = i ; j < N ; j += i ) primeFactors [ j ] = primeFactors [ parseInt ( j / i , 10 ) ] + 1 ; for ( let i = 1 ; i < N ; i ++ ) primeFactors [ i ] += primeFactors [ i - 1 ] ; } findPrimeFactors ( ) ; let a = 6 , b = 3 ; document . write ( primeFactors [ a ] - primeFactors [ b ] ) ;