Skip to content
Related Articles

Related Articles

Sum of all even factors of numbers in the range [l, r]
  • Last Updated : 18 Mar, 2021

Given a range [l, r], the task is to find the sum of all the even factors of the numbers from the given range.
Examples: 
 

Input: l = 6, r = 8 
Output: 22 
factors(6) = 1, 2, 3, 6, evenfactors(6) = 2, 6 sumEvenFactors(6) = 2 + 6 = 8 
factors(7) = 1, 7, No even factors 
factors(8) = 1, 2, 4, 8, evenfactors(8) = 2, 4, 8 sumEvenFactors(8) = 2 + 4 + 8 = 14 
Therefore sum of all even factors = 8 + 14 = 22
Input: l = 1, r = 10 
Output: 42 
 

 

Approach: We can modify Sieve Of Eratosthenes to store the sum of all even 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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
const int MAX = 100000;
 
ll prefix[MAX];
 
// Function to calculate the prefix sum
// of all the even factors
void sieve_modified()
{
    for (int i = 2; i < MAX; i += 2) {
 
        // Add i to all the multiples of i
        for (int j = i; j < MAX; j += i)
            prefix[j] += i;
    }
 
    // Update the prefix sum
    for (int i = 1; i < MAX; i++)
        prefix[i] += prefix[i - 1];
}
 
// Function to return the sum of
// all the even factors of the
// numbers in the given range
ll sumEvenFactors(int L, int R)
{
    return (prefix[R] - prefix[L - 1]);
}
 
// Driver code
int main()
{
    sieve_modified();
    int l = 6, r = 10;
    cout << sumEvenFactors(l, r);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG {
 
    static final int MAX = 100000;
    static long prefix[] = new long[MAX];
 
    // Function to calculate the prefix sum
    // of all the even factors
    static void sieve_modified()
    {
        for (int i = 2; i < MAX; i += 2) {
 
            // Add i to all the multiples of i
            for (int j = i; j < MAX; j += i)
                prefix[j] += i;
        }
 
        // Update the prefix sum
        for (int i = 1; i < MAX; i++)
            prefix[i] += prefix[i - 1];
    }
 
    // Function to return the sum of
    // all the even factors of the
    // numbers in the given range
    static long sumEvenFactors(int L, int R)
    {
        return (prefix[R] - prefix[L - 1]);
    }
 
    // Driver code
    public static void main(String args[])
    {
        sieve_modified();
        int l = 6, r = 10;
        System.out.print(sumEvenFactors(l, r));
    }
}

Python3




# Python3 implementation of the approach.
 
# Function to calculate the prefix sum
# of all the even factors
def sieve_modified():
 
    for i in range(2, MAX, 2):
 
        # Add i to all the multiples of i
        for j in range(i, MAX, i):
            prefix[j] += i
 
    # Update the prefix sum
    for i in range(1, MAX):
        prefix[i] += prefix[i - 1]
 
# Function to return the sum of
# all the even factors of the
# numbers in the given range
def sumEvenFactors(L, R):
 
    return (prefix[R] - prefix[L - 1])
 
# Driver code
if __name__ == "__main__":
     
    MAX = 100000
    prefix = [0] * MAX
    sieve_modified()
    l, r = 6, 10
    print(sumEvenFactors(l, r))
 
# This code is contributed by Rituraj Jain

C#




using System;
// C# implementation of the approach
using System;
 
class GFG
{
 
    public const int MAX = 100000;
    public static long[] prefix = new long[MAX];
 
    // Function to calculate the prefix sum
    // of all the even factors
    public static void sieve_modified()
    {
        for (int i = 2; i < MAX; i += 2)
        {
 
            // Add i to all the multiples of i
            for (int j = i; j < MAX; j += i)
            {
                prefix[j] += i;
            }
        }
 
        // Update the prefix sum
        for (int i = 1; i < MAX; i++)
        {
            prefix[i] += prefix[i - 1];
        }
    }
 
    // Function to return the sum of
    // all the even factors of the
    // numbers in the given range
    public static long sumEvenFactors(int L, int R)
    {
        return (prefix[R] - prefix[L - 1]);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        sieve_modified();
        int l = 6, r = 10;
        Console.Write(sumEvenFactors(l, r));
    }
}
 
// This code is contributed by Shrikant13

PHP




<?php
// PHP implementation of the approach
$MAX = 10000;
 
$prefix = array_fill(0, $MAX, 0);
 
// Function to calculate the prefix sum
// of all the even factors
function sieve_modified()
{
    global $MAX, $prefix;
    for ($i = 2; $i < $MAX; $i += 2)
    {
 
        // Add i to all the multiples of i
        for ($j = $i; $j < $MAX; $j += $i)
            $prefix[$j] += $i;
    }
 
    // Update the prefix sum
    for ($i = 1; $i < $MAX; $i++)
        $prefix[$i] += $prefix[$i - 1];
}
 
// Function to return the sum of
// all the even factors of the
// numbers in the given range
function sumEvenFactors($L, $R)
{
    global $MAX, $prefix;
    return ($prefix[$R] - $prefix[$L - 1]);
}
 
// Driver code
sieve_modified();
$l = 6;
$r = 10;
echo sumEvenFactors($l, $r);
 
// This code is contributed by mits
?>

Javascript




<script>
// Javascript implementation of the approach
var MAX = 100000;
prefix = Array(MAX).fill(0);
 
// Function to calculate the prefix sum
// of all the even factors
function sieve_modified()
{
    for (var i = 2; i < MAX; i += 2) {
 
        // Add i to all the multiples of i
        for (var j = i; j < MAX; j += i)
            prefix[j] += i;
    }
 
    // Update the prefix sum
    for (var i = 1; i < MAX; i++)
        prefix[i] += prefix[i - 1];
}
 
// Function to return the sum of
// all the even factors of the
// numbers in the given range
function sumEvenFactors(L, R)
{
    return (prefix[R] - prefix[L - 1]);
}
 
// Driver code
sieve_modified();
var l = 6, r = 10;
document.write(sumEvenFactors(l, r));
 
// This code is contributed by noob2000.
</script>
Output: 
34

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :