Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; Odd n ; Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
function square ( n ) { return n * n ; } function sum ( n ) { if ( n == 0 ) return 0 ; if ( n % 2 == 1 ) { return square ( ( n + 1 ) / 2 ) + sum ( n / 2 ) ; } else { return square ( n / 2 ) + sum ( n / 2 ) ; } } function oddDivSum ( a , b ) { return sum ( b ) - sum ( a - 1 ) ; } var a = 3 , b = 9 ; document . write ( parseInt ( oddDivSum ( a , b ) ) ) ;