# Sum of greatest odd divisor of numbers in given range

Given an interval of integers [A, B]. For each number in this interval compute its greatest odd divisor. Output the sum of these divisors.

Examples:

```Input : A = 1, B = 3
Output : 5
1 + 1 + 3 = 5

Input : A = 3, B = 9
Output : 29
3 + 1 + 5 + 3 + 7 + 1 + 9 = 29
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach :
A simple approach is to iterate through all numbers in the range find their greatest odd divisor but this algorithm has time complexity of O(n).

Efficient Approach :
We need to find the answer in range [ A, B ] if we can find answer in range [ 1, B ] and subtract it from [ 1, A -1 ] then we will get our required answer.
Here we can see that –

• The answer for an odd number X is X itself.
• The answer for an even number X is equal to the answer for X/2. This is true because X and X/2 have the same odd divisors.( if X = 4 then 4 and 2 both have 1 as greatest odd divisor).

If we want to find answer in range [1, N], then first we need to determine the sum of all the odd numbers
( 1, 3, 5, …) using a simple formula: the sum of the first K odd numbers is equal to K2. Then we need to add the answers for the even numbers (2, 4, 6, …). But these are actually equal to the answers for 1, 2, 3, …, floor(N/2), so we can call our function recursively for floor(N/2).

The complexity of this algorithm is O(log( N)).

Below is the implementation of the above idea :

## C++

 `// C++ program to find sum of greatest ` `// odd divisor of numbers in given range ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return sum of ` `// first n odd numbers ` `int` `square(``int` `n) { ``return` `n * n; } ` ` `  `// Recursive function to return sum of greatest ` `// odd divisor of numbers in range [1, n] ` `int` `sum(``int` `n) ` `{ ` `    ``if` `(n == 0) ` `        ``return` `0; ` `    ``if` `(n % 2 == 1) {  ``// Odd n ` `        ``return` `square((n + 1) / 2) + sum(n / 2);        ` `    ``} ` `    ``else` `{ ``// Even n ` `        ``return` `square(n / 2) + sum(n / 2); ` `    ``} ` `} ` ` `  `// Function to return sum of greatest ` `// odd divisor of numbers in range [a, b] ` `int` `oddDivSum(``int` `a, ``int` `b) ` `{ ` `    ``return` `sum(b) - sum(a - 1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 3, b = 9; ` `    ``cout << oddDivSum(a, b) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find sum of greatest ` `// odd divisor of numbers in given range ` ` `  `// Function to return sum of ` `// first n odd numbers ` `class` `gfg  ` `{ ` `static` `int` `square(``int` `n) ` `{ ` `    ``return` `n * n; ` `} ` ` `  `// Recursive function to return sum of greatest ` `// odd divisor of numbers in range [1, n] ` `static` `int` `sum(``int` `n) ` `{ ` `    ``if` `(n == ``0``) ` `        ``return` `0``; ` `    ``if` `(n % ``2` `== ``1``) ` `    ``{ ` `        ``// Odd n ` `        ``return` `square((n + ``1``) / ``2``) + sum(n / ``2``);      ` `    ``} ` `    ``else` `    ``{  ` `        ``// Even n ` `        ``return` `square(n / ``2``) + sum(n / ``2``); ` `    ``} ` `} ` ` `  `// Function to return sum of greatest ` `// odd divisor of numbers in range [a, b] ` `static` `int` `oddDivSum(``int` `a, ``int` `b) ` `{ ` `    ``return` `sum(b) - sum(a - ``1``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a = ``3``, b = ``9``; ` `    ``System.out.println(oddDivSum(a, b)); ` `} ` `} ` `// This code is contributed by mits `

## Python3

 `# Python3 program to find sum of greatest ` `# odd divisor of numbers in given range ` ` `  `# Function to return sum of first ` `# n odd numbers ` `def` `square(n): ` `    ``return` `n ``*` `n;  ` ` `  `# Recursive function to return sum ` `# of greatest odd divisor of numbers  ` `# in range [1, n] ` `def` `sum``(n): ` ` `  `    ``if` `(n ``=``=` `0``): ` `        ``return` `0``; ` `    ``if` `(n ``%` `2` `=``=` `1``): ` `         `  `        ``# Odd n ` `        ``return` `(square(``int``((n ``+` `1``) ``/` `2``)) ``+`  `                   ``sum``(``int``(n ``/` `2``)));  ` `    ``else``: ` `         `  `        ``# Even n ` `        ``return` `(square(``int``(n ``/` `2``)) ``+` `                   ``sum``(``int``(n ``/` `2``))); ` ` `  `# Function to return sum of greatest ` `# odd divisor of numbers in range [a, b] ` `def` `oddDivSum(a, b): ` ` `  `    ``return` `sum``(b) ``-` `sum``(a ``-` `1``); ` ` `  `# Driver code ` `a, b ``=` `3``, ``9``; ` `print``(oddDivSum(a, b)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# program to find sum of greatest ` `// odd divisor of numbers in given range ` `using` `System; ` ` `  `// Function to return sum of ` `// first n odd numbers ` `class` `gfg  ` `{ ` ` ``public` `int` `square(``int` `n) ` ` ``{ ` `     ``return` `n * n; ` ` ``} ` ` `  `// Recursive function to return sum of greatest ` `// odd divisor of numbers in range [1, n] ` ` ``public` `int` `sum(``int` `n) ` ` ``{ ` `    ``if` `(n == 0) ` `        ``return` `0; ` `    ``if` `(n % 2 == 1) ` `    ``{ ` `        ``// Odd n ` `        ``return` `square((n + 1) / 2) + sum(n / 2);      ` `    ``} ` `    ``else`  `    ``{  ` `        ``// Even n ` `        ``return` `square(n / 2) + sum(n / 2); ` `    ``} ` `} ` ` `  `// Function to return sum of greatest ` `// odd divisor of numbers in range [a, b] ` ` ``public` `int` `oddDivSum(``int` `a, ``int` `b) ` ` ``{ ` `    ``return` `sum(b) - sum(a - 1); ` ` ``} ` `} ` ` `  `// Driver code ` `class` `geek ` `{ ` ` ``public` `static` `int` `Main() ` ` ``{ ` `     ``gfg g = ``new` `gfg(); ` `    ``int` `a = 3, b = 9; ` `    ``Console.WriteLine(g.oddDivSum(a, b)); ` `    ``return` `0; ` ` ``} ` `} `

## PHP

 ` `

Output:

```29
```

Time Complexity : O(log(N))

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.