Sum of the digits of a number N written in all bases from 2 to N / 2 | Function to calculate the sum of the digits of n in the given base ; Sum of digits ; Digit of n in the given base ; Add the digit ; Function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all bases ; function call for multiple bases ; Driver Code
< ? php function solve ( $ n , $ base ) { $ sum = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ sum += $ remainder ; $ n = $ n / $ base ; } return $ sum ; } function SumsOfDigits ( $ n ) { $ sum = 0 ; for ( $ base = 2 ; $ base <= $ n / 2 ; ++ $ base ) $ sum += solve ( $ n , $ base ) ; echo $ sum ; } $ n = 8 ; SumsOfDigits ( $ n ) ; ? >