Sum of all odd factors of numbers in the range [l, r]
Last Updated :
03 May, 2023
Given a range [l, r], the task is to find the sum of all the odd factors of the numbers from the given range.
Examples:
Input: l = 6, r = 8
Output: 32
factors(6) = 1, 2, 3, 6, oddfactors(6) = 1, 3 sum_Odd_Factors(6) = 1 + 3 = 4
factors(7) = 1, 7, oddfactors(6) = 1 7, sum_Odd_Factors(7) = 1 + 7 = 8
factors(8) = 1, 2, 4, 8, oddfactors(6) = 1, sum_Odd_Factors(8) = 1 = 1
Therefore sum of all odd factors = 4 + 8 + 1 = 13
Input: l = 1, r = 10
Output: 45
Approach: We can modify Sieve Of Eratosthenes to store sum of all odd factors of a number at it’s corresponding index. Then we will make a prefix array to store sum upto that index. And now each query can be answered in O(1) using prefix[r] – prefix[l – 1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const int MAX = 100001;
ll prefix[MAX];
void sieve_modified()
{
for ( int i = 1; i < MAX; i += 2) {
for ( int j = i; j < MAX; j += i)
prefix[j] += i;
}
for ( int i = 1; i < MAX; i++)
prefix[i] += prefix[i - 1];
}
ll sumOddFactors( int L, int R)
{
return (prefix[R] - prefix[L - 1]);
}
int main()
{
sieve_modified();
int l = 6, r = 10;
cout << sumOddFactors(l, r);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 100001 ;
static int prefix[] = new int [MAX];
static void sieve_modified()
{
for ( int i = 1 ; i < MAX; i += 2 )
{
for ( int j = i; j < MAX; j += i)
prefix[j] += i;
}
for ( int i = 1 ; i < MAX; i++)
prefix[i] += prefix[i - 1 ];
}
static int sumOddFactors( int L, int R)
{
return (prefix[R] - prefix[L - 1 ]);
}
public static void main (String[] args)
{
sieve_modified();
int l = 6 , r = 10 ;
System.out.println (sumOddFactors(l, r));
}
}
|
Python3
MAX = 100001 ;
prefix = [ 0 ] * MAX ;
def sieve_modified():
for i in range ( 1 , MAX , 2 ):
for j in range (i, MAX , i):
prefix[j] + = i;
for i in range ( 1 , MAX ):
prefix[i] + = prefix[i - 1 ];
def sumOddFactors(L, R):
return (prefix[R] - prefix[L - 1 ]);
sieve_modified();
l = 6 ;
r = 10 ;
print (sumOddFactors(l, r));
|
C#
using System;
class GFG
{
public static int MAX = 100001;
public static int [] prefix = new int [MAX];
public static void sieve_modified()
{
for ( int i = 1; i < MAX; i += 2)
{
for ( int j = i; j < MAX; j += i)
{
prefix[j] += i;
}
}
for ( int i = 1; i < MAX; i++)
{
prefix[i] += prefix[i - 1];
}
}
public static int sumOddFactors( int L, int R)
{
return (prefix[R] - prefix[L - 1]);
}
public static void Main( string [] args)
{
sieve_modified();
int l = 6, r = 10;
Console.WriteLine(sumOddFactors(l, r));
}
}
|
PHP
<?php
$MAX = 10001;
$prefix = array_fill (0, $MAX , 0);
function sieve_modified()
{
global $prefix , $MAX ;
for ( $i = 1; $i < $MAX ; $i += 2)
{
for ( $j = $i ; $j < $MAX ; $j += $i )
$prefix [ $j ] += $i ;
}
for ( $i = 1; $i < $MAX ; $i ++)
$prefix [ $i ] += $prefix [ $i - 1];
}
function sumOddFactors( $L , $R )
{
global $prefix ;
return ( $prefix [ $R ] -
$prefix [ $L - 1]);
}
sieve_modified();
$l = 6;
$r = 10;
echo sumOddFactors( $l , $r );
?>
|
Javascript
<script>
var MAX = 100001;
prefix = Array(MAX).fill(0)
function sieve_modified()
{
for ( var i = 1; i < MAX; i += 2) {
for ( var j = i; j < MAX; j += i)
prefix[j] += i;
}
for ( var i = 1; i < MAX; i++)
prefix[i] += prefix[i - 1];
}
function sumOddFactors(L, R)
{
return (prefix[R] - prefix[L - 1]);
}
sieve_modified();
var l = 6, r = 10;
document.write(sumOddFactors(l, r));
</script>
|
Time Complexity: O(MAX log MAX), we are using a nested loop.
Auxiliary Space: O(MAX), we are using pre[Max] extra space.
Share your thoughts in the comments
Please Login to comment...