Given a range [l, r], the task is to find the sum of all the odd factors of the numbers from the given range.
Input: l = 6, r = 8
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
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:
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.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of all even factors of numbers in the range [l, r]
- Queries on sum of odd number digit sums of all the factors of a number
- Number of elements with odd factors in given range
- Sum of all odd length palindromic numbers within the range [L, R]
- Sum of all odd natural numbers in range L and R
- Find number of factors of N when location of its two factors whose product is N is given
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Even numbers at even index and odd numbers at odd index
- Find sum of odd factors of a number
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- K-Primes (Numbers with k prime factors) in a range
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count of integers in a range which have even number of odd digits and odd number of even digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.