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.
Approach:
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++
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
void sumOddDigit( int digitSum[])
{
for ( int i = 1; i < N; i++) {
digitSum[i] = digitSum[i / 10] + (i & 1) * (i % 10);
}
}
void sumFactor( int digitSum[], int factorDigitSum[])
{
for ( int i = 1; i < N; i++) {
for ( int j = i; j < N; j += i) {
factorDigitSum[j] += digitSum[i];
}
}
}
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]] << " " ;
}
int main()
{
int q = 2;
int n[] = { 10, 36 };
wrapper(q, n);
return 0;
}
|
Java
class GFG
{
static int N = 1000005 ;
static void sumOddDigit( int digitSum[])
{
for ( int i = 1 ; i < N; i++)
{
digitSum[i] = digitSum[i / 10 ] +
(i & 1 ) * (i % 10 );
}
}
static void sumFactor( int digitSum[],
int factorDigitSum[])
{
for ( int i = 1 ; i < N; i++)
{
for ( int j = i; j < N; j += i)
{
factorDigitSum[j] += digitSum[i];
}
}
}
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]]
+ " " );
}
public static void main(String args[])
{
int q = 2 ;
int n[] = new int []{ 10 , 36 };
wrapper(q, n);
}
}
|
Python3
N = 100
digitSum = [ 0 ] * N
factorDigitSum = [ 0 ] * N
def sumOddDigit() :
global N,digitSum,factorDigitSum
for i in range ( 1 , N) :
digitSum[i] = (digitSum[ int (i / 10 )]
+ int (i & 1 ) * (i % 10 ))
def sumFactor() :
global N,digitSum,factorDigitSum
j = 0
for i in range ( 1 , N) :
j = i
while (j < N) :
factorDigitSum[j] = (factorDigitSum[j]
+ digitSum[i])
j = j + i
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 = "")
q = 2
n = [ 10 , 36 ]
wrapper(q, n)
|
C#
using System;
class GFG {
static int N = 1000005;
static void sumOddDigit( int []digitSum)
{
for ( int i = 1; i < N; i++) {
digitSum[i] = digitSum[i / 10]
+ (i & 1) * (i % 10);
}
}
static void sumFactor( int []digitSum,
int []factorDigitSum)
{
for ( int i = 1; i < N; i++) {
for ( int j = i; j < N; j += i)
{
factorDigitSum[j] += digitSum[i];
}
}
}
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]]
+ " " );
}
public static void Main()
{
int q = 2;
int []n = new int []{ 10, 36 };
wrapper(q, n);
}
}
|
PHP
<?php
$N = 1000005;
function sumOddDigit(& $digitSum )
{
global $N ;
for ( $i = 1; $i < $N ; $i ++)
{
$digitSum [ $i ] = $digitSum [ intval ( $i / 10)] +
intval ( $i & 1) *
( $i % 10);
}
}
function sumFactor( $digitSum ,
& $factorDigitSum )
{
global $N ;
for ( $i = 1; $i < $N ; $i ++)
{
for ( $j = $i ; $j < $N ; $j += $i )
{
$factorDigitSum [ $j ] += $digitSum [ $i ];
}
}
}
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 ]]. " " );
}
$q = 2;
$n = array ( 10, 36 );
wrapper( $q , $n );
?>
|
Javascript
<script>
var N = 1000005;
function sumOddDigit(digitSum)
{
for ( var i = 1; i < N; i++) {
digitSum[i] = digitSum[parseInt(i / 10)] + (i & 1) * (i % 10);
}
}
function sumFactor(digitSum, factorDigitSum)
{
for ( var i = 1; i < N; i++) {
for ( var j = i; j < N; j += i) {
factorDigitSum[j] += digitSum[i];
}
}
}
function wrapper(q, n)
{
var digitSum = Array(N).fill(0);
var factorDigitSum = Array(N).fill(0);
sumOddDigit(digitSum);
sumFactor(digitSum, factorDigitSum);
for ( var i = 0; i < q; i++)
document.write( factorDigitSum[n[i]] + " " );
}
var q = 2;
var n = [ 10, 36 ];
wrapper(q, n);
</script>
|
Time complexity: O(q+N2)
Auxiliary space: O(N)
Last Updated :
16 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...