Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver Code
< ? php function checksum ( $ n ) { if ( $ n == 0 ) { echo " - 1 ▁ 0 ▁ 1" ; return ; } $ inc ; if ( $ n > 0 ) $ inc = 1 ; else $ inc = -1 ; for ( $ i = 0 ; $ i <= $ n - 2 ; $ i += $ inc ) { if ( $ i + $ i + 1 + $ i + 2 == $ n ) { echo $ i , " ▁ " , $ i + 1 , " ▁ " , $ i + 2 ; return ; } } echo " - 1" ; } $ n = 6 ; checksum ( $ n ) ; ? >