Find the sum of numbers from 1 to n excluding those which are powers of K | Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code
function sumPowersK ( n , k ) { var sum = 0 , num = 1 ; while ( num <= n ) { sum += num ; num *= k ; } return sum ; } function getSum ( n , k ) { var pwrK = sumPowersK ( n , k ) ; var sumAll = ( n * ( n + 1 ) ) / 2 ; return ( sumAll - pwrK ) ; } var n = 10 , k = 3 ; document . write ( getSum ( n , k ) ) ;