Queries on sum of odd number digit sums of all the factors of a number | PHP Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver Code
< ? 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 ) ; ? >