Sum of greatest odd divisor of numbers in given range
Last Updated :
19 Jul, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int square( int n) { return n * n; }
int sum( int 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);
}
}
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;
}
|
Java
class gfg
{
static int square( int n)
{
return n * n;
}
static int sum( int 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 );
}
}
static int oddDivSum( int a, int b)
{
return sum(b) - sum(a - 1 );
}
public static void main(String[] args)
{
int a = 3 , b = 9 ;
System.out.println(oddDivSum(a, b));
}
}
|
Python3
def square(n):
return n * n;
def sum (n):
if (n = = 0 ):
return 0 ;
if (n % 2 = = 1 ):
return (square( int ((n + 1 ) / 2 )) +
sum ( int (n / 2 )));
else :
return (square( int (n / 2 )) +
sum ( int (n / 2 )));
def oddDivSum(a, b):
return sum (b) - sum (a - 1 );
a, b = 3 , 9 ;
print (oddDivSum(a, b));
|
C#
using System;
class gfg
{
public int square( int n)
{
return n * n;
}
public int sum( int 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);
}
}
public int oddDivSum( int a, int b)
{
return sum(b) - sum(a - 1);
}
}
class geek
{
public static int Main()
{
gfg g = new gfg();
int a = 3, b = 9;
Console.WriteLine(g.oddDivSum(a, b));
return 0;
}
}
|
PHP
<?php
function square( $n )
{
return $n * $n ;
}
function sum( $n )
{
if ( $n == 0)
return 0;
if ( $n % 2 == 1)
{
return square((int)(( $n + 1) / 2)) +
sum((int)( $n / 2));
}
else
{
return square((int)( $n / 2)) +
sum((int)( $n / 2));
}
}
function oddDivSum( $a , $b )
{
return sum( $b ) - sum( $a - 1);
}
$a = 3;
$b = 9;
echo oddDivSum( $a , $b );
?>
|
Javascript
<script>
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)));
</script>
|
Time Complexity : O(log(N))
Auxiliary Space: O(log(N))
Share your thoughts in the comments
Please Login to comment...