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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find sum of greatest
// odd divisor of numbers in given range
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
 }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find sum of greatest
// odd divisor of numbers in given range
  
// Function to return sum of
// first n odd numbers
function square($n
    return $n * $n
}
  
// Recursive function to return sum
// of greatest odd divisor of numbers 
// in range [1, n]
function 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]
function oddDivSum($a, $b)
{
    return sum($b) - sum($a - 1);
}
  
// Driver code
$a = 3;
$b = 9;
echo oddDivSum($a, $b);
  
// This code is contributed by mits
?>

chevron_right


Output:

29

Time Complexity : O(log(N))



My Personal Notes arrow_drop_up


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.