Open In App

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

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 4th odd number. 
     
  • The 4th 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 <bits/stdc++.h>
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




<?php
// PHP program to find
// sum of k-th group of
// positive odd integers.
 
// Return the sum of
// k-th group of positive
// odd integers.
function kthgroupsum($k)
{
     
    // Finding first element
    // of kth group.
    $cur = ($k * ($k - 1)) + 1;
    $sum = 0;
 
    // Finding the sum.
    while ($k--)
    {
        $sum += $cur;
        $cur += 2;
    }
    return $sum;
}
 
// Driver code
$k = 3;
echo kthgroupsum($k) ;
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
// javascript program to find
// sum of k-th group of
// positive odd integers.
 
// Return the sum of
// k-th group of positive
// odd integers.
function kthgroupsum(k)
{
     
    // Finding first element
    // of kth group.
    let cur = (k * (k - 1)) + 1;
    let sum = 0;
 
    // Finding the sum.
    while (k--)
    {
        sum += cur;
        cur += 2;
    }
    return sum;
}
 
// Driver code
let k = 3;
document.write(kthgroupsum(k));
 
// This code is contributed by _saurabh_jaiswal.
</script>


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 <bits/stdc++.h>
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




<?php
// Efficient PHP program to
// find sum of k-th group of
// positive odd integers.
 
 
// Return the sum of kth group
// of positive odd integer.
function kthgroupsum( $k)
{
    return $k * $k *$k;
}
 
// Driven Code
$k = 3;
echo kthgroupsum($k);
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
// Efficient Javascript program to
// find sum of k-th group of
// positive odd integers.
 
 
// Return the sum of kth group
// of positive odd integer.
function kthgroupsum(k)
{
    return k * k * k;
}
 
// Driven Code
let k = 3;
document.write(kthgroupsum(k));
 
// This code is contributed by _saurabh_jaiswal.
</script>


Output : 
 

27

Time Complexity: O(1)

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Nicomachus's Theorem
Nicomachus's Theorem states that sum of cubes of first n natural numbers is equal to squares of natural number sum.[Tex]1^{3}+2^{3}+3^{3}+\cdots +n^{3}=\left(1+2+3+\cdots +n\right)^{2} [/Tex]In other words[Tex]1^{3}+2^{3}+3^{3}+\cdots +n^{3}=\left(n*(n+1)/2)^{2} [/Tex]Or we can say that the sum is equal to square of n-th triangular number.Mathemati
3 min read
Generate an array with K positive numbers such that arr[i] is either -1 or 1 and sum of the array is positive
Given two positive integers, N and K ( 1 ≤ K ≤ N ), the task is to construct an array arr[](1-based indexing) such that each array element arr[i] is either i or -i and the array contains exactly K positive values such that sum of the array elements is positive. If more than one such sequence can be generated, print any of them. Otherwise, print "-1
7 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Cook–Levin theorem or Cook's theorem
In computational complexity theory, the Cook–Levin theorem, also known as Cook's theorem, states that the Boolean satisfiability problem is NP-complete. That is, it is in NP, and any problem in NP can be reduced in polynomial time by a deterministic Turing machine to the Boolean satisfiability problem. Stephen Arthur Cook and L.A. Levin in 1973 ind
4 min read
Compute nCr % p | Set 4 (Chinese Remainder theorem with Lucas Theorem)
Given three numbers n, r and p, the task is to compute the value of nCr % p. Note: p is a square-free number and the largest prime factor of p ≤ 50. Examples: Input: n = 10, r = 2, p = 13Output: 6Explanation: 10C2 is 45 and 45 % 13= 6 Input: n = 6, r = 2, p = 13Output: 2 Approach: We have already discussed several other approaches of this problem.
10 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required. Examples: Input: arr[] = {2, 3, 4, 3, 5, 3, 2}Output: 2Explanation:In first operation, choose the subarray {2, 3, 4}
9 min read
Find K distinct positive odd integers with sum N
Given two integers N and K, the task is to find K distinct positive odd integers such that their sum is equal to the given number N.Examples: Input: N = 10, K = 2 Output: 1 9 Explanation: Two odd positive integers such that their sum is 10 can be (1, 9) or (3, 7).Input: N = 10, K = 4 Output: NO Explanation: There does not exists four odd positive i
5 min read
Only integer with positive value in positive negative value in array
Given an array of N integers. In the given, for each positive element 'x' there exist a negative value '-x', except one integer whose negative value is not present. That integer may occur multiple number of time. The task is find that integer. Examples: Input : arr[] = { 1, 8, -6, -1, 6, 8, 8 } Output : 8 All positive elements have an equal negativ
8 min read
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements
Given an array arr[] consisting of N positive integers, the task is to find the last positive array element remaining after repeated subtractions of the smallest positive array element from all array elements. Examples: Input: arr[] = {3, 5, 4, 7}Output: 2Explanation: Subtract the smallest positive element from the array, i.e. 3, the new array is a
6 min read