Count of elements having Euler 's Totient value one less than itself | JavaScript program for the above approach ; Seiev of Erotosthenes method to compute all primes ; If current number is marked prime then mark its multiple as non - prime ; Function to count the number of element satisfying the condition ; Compute the number of primes in count prime array ; Print the number of elements satisfying the condition ; Given array ; Size of the array ; Function Call
let prime = new Uint8Array ( 1000001 ) ; function seiveOfEratosthenes ( ) { for ( let i = 2 ; i < 1000001 ; i ++ ) { prime [ i ] = 1 ; } for ( let i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 1 ) { for ( let j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 0 ; } } } } function CountElements ( arr , n , L , R ) { seiveOfEratosthenes ( ) ; countPrime = new Uint8Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ; } document . write ( ( countPrime [ R ] - countPrime [ L - 1 ] ) + " " ) ; return ; } let arr = [ 2 , 4 , 5 , 8 ] ; let N = arr . length ; let L = 1 , R = 3 ; CountElements ( arr , N , L , R ) ;