Nicomachusâ €™ s Theorem ( Sum of k | Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver code
< ? php function kthgroupsum ( $ k ) { $ cur = ( $ k * ( $ k - 1 ) ) + 1 ; $ sum = 0 ; while ( $ k -- ) { $ sum += $ cur ; $ cur += 2 ; } return $ sum ; } $ k = 3 ; echo kthgroupsum ( $ k ) ; ? >