Frugal Number | Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Returns number of digits in n ; Checking whether a number is Frugal or not ; Finding number of digits in prime factorization of the number ; Exponent for current factor ; Counting number of times this prime factor divides ( Finding exponent ) ; Finding number of digits in the exponent Avoiding exponents of value 1 ; Checking condition for frugal number ; Driver Method to check for frugal number
function primes ( n ) { var prime = Array ( n + 1 ) . fill ( true ) ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( var j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } var arr = [ ] ; for ( var i = 2 ; i < n ; i ++ ) if ( prime [ i ] ) arr . push ( i ) ; return arr ; } function countDigits ( n ) { var temp = n ; var c = 0 ; while ( temp != 0 ) { temp = parseInt ( temp / 10 ) ; c ++ ; } return c ; } function frugal ( n ) { var r = primes ( n ) ; var t = n ; var s = 0 ; for ( var i = 0 ; i < r . length ; i ++ ) { if ( t % r [ i ] == 0 ) { var k = 0 ; while ( t % r [ i ] == 0 ) { t = parseInt ( t / r [ i ] ) ; k ++ ; } if ( k == 1 ) s = s + countDigits ( r [ i ] ) ; else if ( k != 1 ) s = s + countDigits ( r [ i ] ) + countDigits ( k ) ; } } return ( countDigits ( n ) > s && s != 0 ) ; } var n = 343 ; if ( frugal ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;