Given a range [l, r], the task is to find the sum of all the even factors of the numbers from the given range.
Input: l = 6, r = 8
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
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of all odd factors of numbers in the range [l, r]
- K-Primes (Numbers with k prime factors) in a range
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Number of elements with even factors in the given range
- Number of elements with odd factors in given range
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Generation of n numbers with given set of factors
- Common prime factors of two numbers
- Maximum factors formed by two numbers
- Count common prime factors of two numbers
- Find sum of exponents of prime factors of numbers 1 to N
- Number of distinct prime factors of first n natural numbers
- Efficient program to print the number of factors of n numbers
- Number which has the maximum number of distinct prime factors in the range M to N
- Check if a number exists having exactly N factors and K prime factors
- Find number of factors of N when location of its two factors whose product is N is given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.