Given Q queries. Each query contain a positive integer n. The task is to output the sum of sum of odd number digit contained in all the divisors of n.
Input : Q = 2, n1 = 10, n2 = 36
Output : 7 18
Divisors of 10 are 1, 2, 5, 10.
Sum of odd digits in 1 is 1, in 2 is 0, in 5 is 5, in 10 is 1.
So, sum became 7.
For Query 2,
Divisors of 36 are 1, 2, 3, 4, 6, 9, 12, 18, 36.
Sum of odd digits in 1 is 1, in 2 is 0, in 3 is 3, in 4 is 0,
in 6 is 0, in 9 is 9, in 12 is 1, in 18 is 1, in 36 is 3.
So, sum became 18.
The idea is to precompute the sum of odd number digit of all the numbers. Also, we can you use the sum of odd number digit of the previous number to compute the sum of odd number digit of the current number.
For example, to compute the sum of odd number digit of “123”, we can use the sum of odd number digit of “12” and “3”. Therefore, the sum of odd digit of “123” = sum of odd digit of “12” + add the last digit if it is odd (i.e 3).
Now, to find the sum of the sum of odd number digit of the factors, we can you use the jump phenomenon of Sieve of Eratosthenes. So, for all possible factors, add their contribution to its multiples.
For example, for 1 as the factor, add 1 (because 1 have only 1 odd digit) to all of its multiple.
for 2 as the factor, add 0 to all the multiples of 2 i.e 2, 4, 8, …
for 3 as the factor, add 1 to all the multiples of 3 i.e 3, 6, 9, …..
Below is the implementation of this approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Queries to find whether a number has exactly four distinct factors or not
- Queries for the smallest and the largest prime number of given digit
- Maximum number of prime factors a number can have with exactly x factors
- Number of factors of very large number N modulo M where M is any prime number
- 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
- Number which has the maximum number of distinct prime factors in the range M to N
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Count of Numbers in Range where first digit is equal to last digit of the number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find the remainder when First digit of a number is divided by its Last digit
- Largest number less than N whose each digit is prime number
- Count the number of occurrences of a particular digit in a number
- Largest number less than N with digit sum greater than the digit sum of N
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.