Find ways an Integer can be expressed as sum of n | Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code .
< ? php function power ( $ num , $ n ) { if ( $ n == 0 ) return 1 ; else if ( $ n % 2 == 0 ) return power ( $ num , ( int ) ( $ n / 2 ) ) * power ( $ num , ( int ) ( $ n / 2 ) ) ; else return $ num * power ( $ num , ( int ) ( $ n / 2 ) ) * power ( $ num , ( int ) ( $ n / 2 ) ) ; } function checkRecursive ( $ x , $ n , $ curr_num = 1 , $ curr_sum = 0 ) { $ results = 0 ; $ p = power ( $ curr_num , $ n ) ; while ( $ p + $ curr_sum < $ x ) { $ results += checkRecursive ( $ x , $ n , $ curr_num + 1 , $ p + $ curr_sum ) ; $ curr_num ++ ; $ p = power ( $ curr_num , $ n ) ; } if ( $ p + $ curr_sum == $ x ) $ results ++ ; return $ results ; } $ x = 10 ; $ n = 2 ; echo ( checkRecursive ( $ x , $ n ) ) ; ? >