Sum of greatest odd divisor of numbers in given range | C ++ program to find 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 ] ; if ( n % 2 == 1 ) { Odd n ; else { Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int square ( int n ) { return n * n ; } int sum ( int n ) { if ( n == 0 ) return 0 ; return square ( ( n + 1 ) / 2 ) + sum ( n / 2 ) ; } return square ( n / 2 ) + sum ( n / 2 ) ; } } int oddDivSum ( int a , int b ) { return sum ( b ) - sum ( a - 1 ) ; } int main ( ) { int a = 3 , b = 9 ; cout << oddDivSum ( a , b ) << endl ; return 0 ; }