Sum of all armstrong numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all armstrong number ; Function that return number num if num is armstrong else return 0 ; Function to precompute the sum of all armstrong numbers upto 100000 ; checkarmstrong ( ) return the number i if i is armstrong else return 0 ; Function to print the sum for each query ; Function to prsum of all armstrong numbers between [ L , R ] ; Function that pre computes the sum of all armstrong numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all armstrong number in Range [ L , R ]
let pref = new Array ( 100001 ) ; pref . fill ( 0 ) ; function checkArmstrong ( x ) { let n = ( x . toString ( ) ) . length ; let sum1 = 0 ; let temp = x ; while ( temp > 0 ) { let digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp = parseInt ( temp / 10 , 10 ) ; } if ( sum1 == x ) return x ; return 0 ; } function preCompute ( ) { for ( let i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) ; } } function printSum ( L , R ) { document . write ( ( pref [ R ] - pref [ L - 1 ] ) + " " ) ; } function printSumarmstrong ( arr , Q ) { preCompute ( ) ; for ( let i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } let Q = 2 ; let arr = [ [ 1 , 13 ] , [ 121 , 211 ] ] ; printSumarmstrong ( arr , Q ) ;