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
function kthgroupsum ( k ) { let cur = ( k * ( k - 1 ) ) + 1 ; let sum = 0 ; while ( k -- ) { sum += cur ; cur += 2 ; } return sum ; } let k = 3 ; document . write ( kthgroupsum ( k ) ) ;