Sum of all N | Javascript program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the final ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the final Sum ; Driver Code
let MOD = 1000000007 ; function power ( a , b ) { if ( b == 0 ) return 1 ; let temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } function palindromicSum ( N ) { let sum = 0 , res , ways ; if ( N == 1 ) { document . write ( " " + " " ) ; return ; } if ( N == 2 ) { document . write ( " " + " " ) ; return ; } ways = Math . floor ( N / 2 ) ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } document . write ( sum + " " ) ; } let N = 3 ; palindromicSum ( N ) ;