Ways to write N as sum of two or more positive integers | Set | Function to find the number of partitions of N ; Base case ; Driver Code
< ? php function partitions ( $ n ) { $ p = array_fill ( 0 , $ n + 1 , 0 ) ; $ p [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) { $ k = 1 ; while ( ( $ k * ( 3 * $ k - 1 ) ) / 2 <= $ i ) { $ p [ $ i ] += ( ( $ k % 2 ? 1 : -1 ) * $ p [ $ i - ( $ k * ( 3 * $ k - 1 ) ) / 2 ] ) ; if ( $ k > 0 ) $ k *= -1 ; else $ k = 1 - $ k ; } } return $ p [ $ n ] ; } $ N = 20 ; print ( partitions ( $ N ) ) ; ? >