给定一系列数字1、2、4、3、5、7、9、6、8、10、11、13……的任务是找到直到第N个数字的所有数字之和。
例子:
Input: N = 4
Output: 10
1 + 2 + 4 + 3 = 10
Input: N = 10
Output: 55
方法:该系列基本上是2 0个奇数,2 1个偶数,2 2个偶数……。前N个奇数的总和为N * N ,前N个偶数的总和为(N *(N + 1)) 。计算总和的2个我奇数或偶数,让他们增加的总和。
对2的每一个幂进行迭代,直到迭代次数超过N,并根据奇偶校验不断增加奇数或偶数的总和。对于每个段,该段的总和为(X个奇/偶数的当前总和-Y个奇/偶数的先前总和) ,其中X是直到该段的奇/偶数的总和,Y是总和直到奇数/偶数出现之前的奇数/偶数。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the
// sum of first N odd numbers
int sumodd(int n)
{
return (n * n);
}
// Function to find the
// sum of first N even numbers
int sumeven(int n)
{
return (n * (n + 1));
}
// Function to overall
// find the sum of series
int findSum(int num)
{
// Initiall odd numbers
int sumo = 0;
// Initial even numbers
int sume = 0;
// First power of 2
int x = 1;
// Check for parity
// for odd/even
int cur = 0;
// Counts the sum
int ans = 0;
while (num > 0) {
// Get the minimum
// out of remaining num
// or power of 2
int inc = min(x, num);
// Decrease that much numbers
// from num
num -= inc;
// If the segment has odd numbers
if (cur == 0) {
// Summate the odd numbers
// By exclusion
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
// Increase number of odd numbers
sumo += inc;
}
// If the segment has evn numbers
else {
// Summate the even numbers
// By exclusion
ans = ans + sumeven(sume + inc) - sumeven(sume);
// Increase number of even numbers
sume += inc;
}
// Next set of numbers
x *= 2;
// Change parity for odd/even
cur ^= 1;
}
return ans;
}
// Driver code
int main()
{
int n = 4;
cout << findSum(n);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG
{
// Function to find the
// sum of first N odd numbers
static int sumodd(int n)
{
return (n * n);
}
// Function to find the
// sum of first N even numbers
static int sumeven(int n)
{
return (n * (n + 1));
}
// Function to overall
// find the sum of series
static int findSum(int num)
{
// Initiall odd numbers
int sumo = 0;
// Initial even numbers
int sume = 0;
// First power of 2
int x = 1;
// Check for parity
// for odd/even
int cur = 0;
// Counts the sum
int ans = 0;
while (num > 0)
{
// Get the minimum
// out of remaining num
// or power of 2
int inc = Math.min(x, num);
// Decrease that much numbers
// from num
num -= inc;
// If the segment has odd numbers
if (cur == 0)
{
// Summate the odd numbers
// By exclusion
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
// Increase number of odd numbers
sumo += inc;
}
// If the segment has evn numbers
else
{
// Summate the even numbers
// By exclusion
ans = ans + sumeven(sume + inc) - sumeven(sume);
// Increase number of even numbers
sume += inc;
}
// Next set of numbers
x *= 2;
// Change parity for odd/even
cur ^= 1;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 4;
System.out.println(findSum(n));
}
}
// This code contributed by Rajput-Ji
Python
# Python3 program to implement
# the above approach
# Function to find the
# sum of first N odd numbers
def sumodd(n):
return (n * n)
# Function to find the
# sum of first N even numbers
def sumeven(n):
return (n * (n + 1))
# Function to overall
# find the sum of series
def findSum(num):
# Initiall odd numbers
sumo = 0
# Initial even numbers
sume = 0
# First power of 2
x = 1
# Check for parity
# for odd/even
cur = 0
# Counts the sum
ans = 0
while (num > 0):
# Get the minimum
# out of remaining num
# or power of 2
inc = min(x, num)
# Decrease that much numbers
# from num
num -= inc
# If the segment has odd numbers
if (cur == 0):
# Summate the odd numbers
# By exclusion
ans = ans + sumodd(sumo + inc) - sumodd(sumo)
# Increase number of odd numbers
sumo += inc
# If the segment has evn numbers
else:
# Summate the even numbers
# By exclusion
ans = ans + sumeven(sume + inc) - sumeven(sume)
# Increase number of even numbers
sume += inc
# Next set of numbers
x *= 2
# Change parity for odd/even
cur ^= 1
return ans
# Driver code
n = 4
print(findSum(n))
# This code is contributed by mohit kumar
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the
// sum of first N odd numbers
static int sumodd(int n)
{
return (n * n);
}
// Function to find the
// sum of first N even numbers
static int sumeven(int n)
{
return (n * (n + 1));
}
// Function to overall
// find the sum of series
static int findSum(int num)
{
// Initiall odd numbers
int sumo = 0;
// Initial even numbers
int sume = 0;
// First power of 2
int x = 1;
// Check for parity
// for odd/even
int cur = 0;
// Counts the sum
int ans = 0;
while (num > 0)
{
// Get the minimum
// out of remaining num
// or power of 2
int inc = Math.Min(x, num);
// Decrease that much numbers
// from num
num -= inc;
// If the segment has odd numbers
if (cur == 0)
{
// Summate the odd numbers
// By exclusion
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
// Increase number of odd numbers
sumo += inc;
}
// If the segment has evn numbers
else
{
// Summate the even numbers
// By exclusion
ans = ans + sumeven(sume + inc) - sumeven(sume);
// Increase number of even numbers
sume += inc;
}
// Next set of numbers
x *= 2;
// Change parity for odd/even
cur ^= 1;
}
return ans;
}
// Driver code
public static void Main(String[] args)
{
int n = 4;
Console.WriteLine(findSum(n));
}
}
// This code has been contributed by 29AjayKumar
PHP
0)
{
// Get the minimum
// out of remaining num
// or power of 2
$inc = min($x, $num);
// Decrease that much numbers
// from num
$num -= $inc;
// If the segment has odd numbers
if ($cur == 0)
{
// Summate the odd numbers
// By exclusion
$ans = $ans + sumodd($sumo + $inc) -
sumodd($sumo);
// Increase number of odd numbers
$sumo += $inc;
}
// If the segment has evn numbers
else
{
// Summate the even numbers
// By exclusion
$ans = $ans + sumeven($sume + $inc) -
sumeven($sume);
// Increase number of even numbers
$sume += $inc;
}
// Next set of numbers
$x *= 2;
// Change parity for odd/even
$cur ^= 1;
}
return $ans;
}
// Driver code
$n = 4;
echo findSum($n);
// This code contributed by princiraj1992
?>
Javascript
输出:
10