text: Sum of all subsequences of a number | Returns numeric value of a subsequence of s . The subsequence to be picked is decided using bit pattern of num ( We pick all thosedigits for which there is a set bit in num ) ; Initialize the result ; till n != 0 ; if i - th bit is set then add this number ; right shintift i ; function to find combined sum of all individual subsequence sum ; length of string ; stores the combined ; 2 ^ n - 1 subsequences ; loop for all subsequences ; returns the combined sum ; Driver Code
code: < ? php function findSubSequence ( $ s , $ num ) { $ res = 0 ; $ i = 0 ; while ( $ num ) { if ( $ num & 1 ) $ res += $ s [ $ i ] - '0' ; $ i ++ ; $ num = $ num >> 1 ; } return $ res ; } function combinedSum ( string $ s ) { $ n = strlen ( $ s ) ; $ c_sum = 0 ; $ range = ( 1 << $ n ) - 1 ; for ( $ i = 0 ; $ i <= $ range ; $ i ++ ) $ c_sum += findSubSequence ( $ s , $ i ) ; return $ c_sum ; } $ s = "123" ; echo combinedSum ( $ s ) ; ? >