Open in App
Not now

# Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)

• Last Updated : 06 Mar, 2023

The positive odd numbers in ascending order as 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, …. and grouped as (1), (3, 5), (7, 9, 11), (13, 15, 17, 19), …. and so on.
Thus, the first group is (1), the second group is (3, 5) and the third group is (7, 9, 11), etc. in general, the kth group contains the next k elements of the sequence.
Given k, find the sum of the kth group.
Examples :

```Input : k = 3
Output : 27
3rd group is (7, 9, 11) and the sum is 27.

Input : k = 1
Output : 1```

Method 1: O(n)
The idea is to find the first element of the kth group.
For example:

• The 1st group’s first element is 1, which is the 1st odd number.

• The 2nd group’s first element is 3, which is the 2nd odd number.

• The 3rd group’s first element is 7, which is the 4st odd number.

• The 4st group’s first element is 13, which is the 7st odd number.

• and so on.

In general, the kth group’s first element is the nth odd number, where
n = (1 + 2 + 3 +……+ (k – 1)) + 1.
Now, as we know, the nth odd number is 2n – 1. This gives us the first element of the kth group. We also know that there are k elements in the group, so we can find their sum by simply counting upwards from 2n – 1, by two, k times and adding them all. This gives us a linear time solution.

## C++

 `// CPP program to find sum of k-th group of` `// positive odd integers.` `#include ` `using` `namespace` `std;`   `// Return the sum of k-th group of positive` `// odd integers.` `int` `kthgroupsum(``int` `k)` `{` `    ``// Finding first element of kth group.` `    ``int` `cur = (k * (k - 1)) + 1;` `    ``int` `sum = 0;`   `    ``// Finding the sum.` `    ``while` `(k--) {` `        ``sum += cur;` `        ``cur += 2;` `    ``}`   `    ``return` `sum;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `k = 3;` `    ``cout << kthgroupsum(k) << endl;` `    ``return` `0;` `}`

## Java

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

## Python3

 `# Python3 code to find sum of k-th group ` `# of positive odd integers.`   `# Return the sum of k-th group of ` `# positive odd integers.` `def` `kthgroupsum( k ):` `    `  `    ``# Finding first element of kth group.` `    ``cur ``=` `int``((k ``*` `(k ``-` `1``)) ``+` `1``)` `    ``sum` `=` `0` `    `  `    ``# Finding the sum.` `    ``while` `k:` `        ``sum` `+``=` `cur` `        ``cur ``+``=` `2` `        ``k``=``k``-``1` `    `  `    ``return` `sum` `    `  `# Driver code` `k ``=` `3` `print``(kthgroupsum(k))`   `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// C# code to find sum of k-th group` `// of positive odd integers.` `using` `System;`   `class` `GFG {` `    `  `    ``// Return the sum of k-th group` `    ``// of positive odd integers.` `    ``public` `static` `int` `kthgroupsum(``int` `k)` `    ``{` `        `  `        ``// Finding first element of kth group.` `        ``int` `cur = (k * (k - 1)) + 1;` `        ``int` `sum = 0;`   `        ``// Finding the sum.` `        ``while` `(k-- > 0) {` `            ``sum += cur;` `            ``cur += 2;` `        ``}`   `        ``return` `sum;` `    ``}`   `    ``// Driver program to test above methods` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `k = 3;` `        `  `        ``Console.WriteLine(kthgroupsum(k));` `    ``}` `}`   `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output :

`27`

Time complexity: O(k)
Auxiliary space: O(1)

Method 2 : O(1)
An tricky solution is to find k3. One can easily observe, the sum of kth group will be k3.
Below is the implementation of this approach:

## C++

 `// Efficient CPP program to find sum of k-th ` `// group of positive odd integers.` `#include ` `using` `namespace` `std;`   `// Return the sum of kth group of positive` `// odd integer.` `int` `kthgroupsum(``int` `k)` `{` `    ``return` `k * k * k;` `}`   `// Driven Program` `int` `main()` `{` `    ``int` `k = 3;` `    ``cout << kthgroupsum(k) << endl;` `    ``return` `0;` `}`

## Java

 `// Efficient Java code to find sum of k-th` `// group of positive odd integers.` `import` `java.util.Arrays;` `import` `java.util.Collections;`   `class` `GFG` `{ ` `    ``// Return the sum of kth group of` `    ``// positive odd integer.` `    ``public` `static` `int` `kthgroupsum(``int` `k)` `    ``{` `        ``return` `k * k * k;` `    ``}`   `    ``// Driver program to test above methods ` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `k = ``3``;` `        ``System.out.print( kthgroupsum(k));` `    ``}` `}`   `// This code is contributed by Chhavi`

## Python3

 `# Efficient Python code to find sum of  ` `# k-th group of positive odd integers.`   `# Return the sum of kth group of positive` `# odd integer.` `def` `kthgroupsum( k ):` `    ``return` `k ``*` `k ``*` `k`   `# Driver code` `k ``=` `3` `print``(kthgroupsum(k))`   `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// Efficient C# code to find sum of k-th` `// group of positive odd integers.` `using` `System;`   `class` `GFG {` `    `  `    ``// Return the sum of kth group of` `    ``// positive odd integer.` `    ``public` `static` `int` `kthgroupsum(``int` `k)` `    ``{` `        ``return` `k * k * k;` `    ``}`   `    ``// Driver program to test above methods` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `k = 3;` `        `  `        ``Console.WriteLine(kthgroupsum(k));` `    ``}` `}`   `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output :

`27`

Time Complexity: O(1)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles