Queries on sum of odd number digit sums of all the factors of a number

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.

Examples :

Input : Q = 2, n1 = 10, n2 = 36
Output : 7 18
For Query1,
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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to answer queries on sum 
// of sum of odd number digits of all 
// the factors of a number
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
  
// finding sum of odd digit number in each integer.
void sumOddDigit(int digitSum[])
{
    // for each number
    for (int i = 1; i < N; i++) {
  
        // using previous number sum, finding
        // the current number num of odd digit
        // also, adding last digit if it is odd.
        digitSum[i] = digitSum[i / 10] + (i & 1) * (i % 10);
    }
}
  
// finding sum of sum of odd digit of all
// the factors of a number.
void sumFactor(int digitSum[], int factorDigitSum[])
{
    // for each possible factor
    for (int i = 1; i < N; i++) {
        for (int j = i; j < N; j += i) {
  
            // adding the contribution.
            factorDigitSum[j] += digitSum[i];
        }
    }
}
  
// Wrapper function
void wrapper(int q, int n[])
{
    int digitSum[N];
    int factorDigitSum[N];
  
    sumOddDigit(digitSum);
    sumFactor(digitSum, factorDigitSum);
  
    for (int i = 0; i < q; i++) 
        cout << factorDigitSum[n[i]] << " ";
}
  
// Driven Program
int main()
{
    int q = 2;
    int n[] = { 10, 36 };
  
    wrapper(q, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to answer queries 
// on sum of sum of odd number  
// digits of all the factors of 
// a number
class GFG 
{
    static int N = 1000005;
      
    // finding sum of odd digit 
    // number in each integer.
    static void sumOddDigit(int digitSum[])
    {
          
        // for each number
        for (int i = 1; i < N; i++)
        {
      
            // using previous number sum,
            // finding the current number 
            // num of odd digit also, 
            // adding last digit if it
            // is odd.
            digitSum[i] = digitSum[i / 10] + 
                         (i & 1) * (i % 10);
        }
    }
      
    // finding sum of sum of odd digit 
    // of all the factors of a number.
    static void sumFactor(int digitSum[], 
                    int factorDigitSum[])
    {
          
        // for each possible factor
        for (int i = 1; i < N; i++) 
        {
            for (int j = i; j < N; j += i)
            {
                // adding the contribution.
                factorDigitSum[j] += digitSum[i];
            }
        }
    }
      
    // Wrapper function
    static void wrapper(int q, int n[])
    {
        int digitSum[] = new int[N];
        int factorDigitSum[] = new int[N];
      
        sumOddDigit(digitSum);
        sumFactor(digitSum, factorDigitSum);
      
        for (int i = 0; i < q; i++) 
            System.out.print(factorDigitSum[n[i]]
                                          + " ");
    }
      
    // Driver Code
    public static void main(String args[]) 
    {
        int q = 2;
        int n[] = new int[]{10, 36};
      
        wrapper(q, n);
          
    }
}
  
// This code is contributed by Sam007

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to answer queries 
# on sum of sum of odd number
# digits of all the factors 
# of a number
N = 100
digitSum = [0] * N
factorDigitSum = [0] * N
  
# finding sum of odd digit 
# number in each integer.
def sumOddDigit() :
    global N,digitSum,factorDigitSum
      
    # for each number
    for i in range(1, N) : 
          
        # using previous number
        # sum, finding the current 
        # number num of odd digit
        # also, adding last digit 
        # if it is odd.
        digitSum[i] = (digitSum[int(i / 10)]
                    + int(i & 1) * (i % 10))
  
# finding sum of sum of 
# odd digit of all the
# factors of a number.
def sumFactor() :
    global N,digitSum,factorDigitSum 
    j = 0
      
    # for each possible factor
    for i in range(1, N) : 
        j = i
        while (j < N) :
              
            # adding the contribution.
            factorDigitSum[j] = (factorDigitSum[j]
                                   + digitSum[i])
            j = j + i
  
# Wrapper def
def wrapper(q, n) :
  
    global N,digitSum,factorDigitSum
      
    for i in range(0, N) :     
        digitSum[i] = 0
        factorDigitSum[i] = 0
      
    sumOddDigit()
    sumFactor()
  
    for i in range(0, q) : 
        print ("{} ".
        format(factorDigitSum[n[i]]), end = "")
  
# Driver Code
q = 2
n = [ 10, 36 ]
wrapper(q, n)
  
# This code is contributed by 
# Manish Shaw(manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to answer queries on sum 
// of sum of odd number digits of all 
// the factors of a number
using System;
  
class GFG {
      
    static int N = 1000005;
      
    // finding sum of odd digit number in
    // each integer.
    static void sumOddDigit(int []digitSum)
    {
          
        // for each number
        for (int i = 1; i < N; i++) {
      
            // using previous number sum,
            // finding the current number 
            // num of odd digit also, 
            // adding last digit if it
            // is odd.
            digitSum[i] = digitSum[i / 10] 
                     + (i & 1) * (i % 10);
        }
    }
      
    // finding sum of sum of odd digit 
    // of all the factors of a number.
    static void sumFactor(int []digitSum, 
                     int []factorDigitSum)
    {
          
        // for each possible factor
        for (int i = 1; i < N; i++) {
            for (int j = i; j < N; j += i)
            {
                // adding the contribution.
                factorDigitSum[j] += digitSum[i];
            }
        }
    }
      
    // Wrapper function
    static void wrapper(int q, int []n)
    {
        int []digitSum = new int[N];
        int []factorDigitSum = new int[N];
      
        sumOddDigit(digitSum);
        sumFactor(digitSum, factorDigitSum);
      
        for (int i = 0; i < q; i++) 
            Console.Write(factorDigitSum[n[i]]
                                       + " ");
    }
          
    // Driver code
    public static void Main()
    {
        int q = 2;
        int []n = new int[]{ 10, 36 };
      
        wrapper(q, n);
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to answer queries 
// on sum of sum of odd number
// digits of all the factors 
// of a number
$N = 1000005;
  
// finding sum of odd digit 
// number in each integer.
function sumOddDigit(&$digitSum)
{
    global $N;
    // for each number
    for ($i = 1; $i < $N; $i++) 
    {
  
        // using previous number
        // sum, finding the current 
        // number num of odd digit
        // also, adding last digit 
        // if it is odd.
        $digitSum[$i] = $digitSum[intval($i / 10)] + 
                                  intval($i & 1) *
                                        ($i % 10);
    }
}
  
// finding sum of sum of 
// odd digit of all the
// factors of a number.
function sumFactor($digitSum
                   &$factorDigitSum)
{
    global $N;
      
    // for each possible factor
    for ($i = 1; $i < $N; $i++) 
    {
        for ($j = $i; $j < $N; $j += $i)
        {
  
            // adding the contribution.
            $factorDigitSum[$j] += $digitSum[$i];
        }
    }
}
  
// Wrapper function
function wrapper($q, $n)
{
    global $N;
    $digitSum = array();
    $factorDigitSum = array();
      
    for ($i = 0; $i < $N; $i++) 
    {
        $digitSum[$i] = 0;
        $factorDigitSum[$i] = 0;
    }
    sumOddDigit($digitSum);
    sumFactor($digitSum, $factorDigitSum);
  
    for ($i = 0; $i < $q; $i++) 
        echo ($factorDigitSum[$n[$i]]. " ");
}
  
// Driver Code
$q = 2;
$n = array( 10, 36 );
wrapper($q, $n);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

chevron_right


Output :

7 18


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : Sam007, manishshaw1



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.