尼科马库斯定理(第k组奇数正数之和)

正奇数的升序为1,3,5,7,9,11,13,15,17,19…。分为(1)、(3、5)、(7、9、11)、(13、15、17、19)组…。等等 因此,第一组是(1),第二组是(3,5),第三组是(7,9,11),等等 th 组包含序列中接下来的k个元素。 鉴于 K ,求k的和 th 例如:

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

方法1:O(n) 我们的想法是找到kth群的第一个元素。 例如:

  • 第一组的第一个元素是1,它是第一个奇数。
  • 第二组的第一个元素是3,这是2st奇数。
  • 第三组的第一个元素是7,这是第四个奇数。
  • 第4组的第一个元素是13,这是第7个奇数。
  • 等等

一般来说,第k个群的第一个元素是第n个奇数,其中 n=(1+2+3+…+(k–1))+1。 我们知道,第n个奇数是2n–1。这给了我们kth群的第一个元素。我们也知道群中有k个元素,所以我们可以通过简单地从2n–1向上计数,乘以2,k次,然后将它们全部相加来求和。这给了我们一个线性时间解。

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>


输出:

27

方法2:O(1) 一个棘手的解决办法是找到k 3. 可以很容易地观察到,第k个群的和是k 3. . 以下是该方法的实施情况:

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>


输出:

27

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享