Nicomachusâ €™ s Theorem ( Sum of k | Java code to find sum of k - th group of positive odd integers . ; Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver program to test above methods
import java . util . Arrays ; import java . util . Collections ; class GFG { public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . print ( kthgroupsum ( k ) ) ; } }