Sum of digits written in different bases from 2 to n | function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver Code
function solve ( n , base ) { var result = 0 ; while ( n > 0 ) { var remainder = n % base ; result = result + remainder ; n = parseInt ( n / base ) ; } return result ; } function printSumsOfDigits ( n ) { for ( base = 2 ; base < n ; ++ base ) document . write ( solve ( n , base ) + " " ) ; } var n = 8 ; printSumsOfDigits ( n ) ;