给定数字n,请求出第一个自然数的总和。
例子 :
Input : n = 3
Output : 6
Explanation :
Note that 1 + 2 + 3 = 6
Input : 5
Output : 15
Explanation :
Note that 1 + 2 + 3 + 4 + 5 = 15
一个简单的解决方案是执行以下操作。
1) Initialize : sum = 0
2) Run a loop from x = 1 to n and
do following in loop.
sum = sum + x
C++
// CPP program to find sum of first
// n natural numbers.
#include
using namespace std;
// Returns sum of first n natural
// numbers
int findSum(int n)
{
int sum = 0;
for (int x=1; x<=n; x++)
sum = sum + x;
return sum;
}
// Driver code
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
Java
// JAVA program to find sum of first
// n natural numbers.
import java.io.*;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
// Driver code
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
// This code is contributed by Nikita Tiwari.
Python
# PYTHON program to find sum of first
# n natural numbers.
# Returns sum of first n natural
# numbers
def findSum(n) :
sum = 0
x = 1
while x <=n :
sum = sum + x
x = x + 1
return sum
# Driver code
n = 5
print findSum(n)
# This code is contributed by Nikita Tiwari.
C#
// C# program to find sum of first
// n natural numbers.
using System;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// Efficient CPP program to find sum of first
// n natural numbers.
#include
using namespace std;
// Returns sum of first n natural
// numbers
int findSum(int n)
{
return n * (n + 1) / 2;
}
// Driver code
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
Java
// Efficient JAVA program to find sum
// of first n natural numbers.
import java.io.*;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
return n * (n + 1) / 2;
}
// Driver code
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
// This code is contributed by Nikita Tiwari.
Python
# Efficient CPP program to find sum
# of first n natural numbers.
# Returns sum of first n natural
# numbers
def findSum(n) :
return n * (n + 1) / 2
# Driver code
n = 5
print findSum(n)
# This code is contributed by Nikita Tiwari.
C#
// Efficient C# program to find sum
// of first n natural numbers.
using System;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
return n * (n + 1) / 2;
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
// This code is contributed by vt_m.
php
Javascript
C++
// Efficient CPP program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
#include
using namespace std;
// Returns sum of first n natural
// numbers
int findSum(int n)
{
if (n % 2 == 0)
return (n/2) * (n+1);
// If n is odd, (n+1) must be even
else
return ((n + 1) / 2) * n;
}
// Driver code
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
Java
// Efficient JAVA program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
import java.io.*;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
if (n % 2 == 0)
return (n / 2) * (n + 1);
// If n is odd, (n+1) must be even
else
return ((n + 1) / 2) * n;
}
// Driver code
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
//This code is contributed by Nikita Tiwari.
Python
# Efficient Python program to find the sum
# of first n natural numbers that avoid
# overflow if the result is going to be
# within limits.
# Returns sum of first n natural
# numbers
def findSum(n) :
if (n % 2 == 0) :
return (n / 2) * (n + 1)
# If n is odd, (n+1) must be even
else :
return ((n + 1) / 2) * n
# Driver code
n = 5
print findSum(n)
# This code is contributed by Nikita Tiwari.
C#
// Efficient C# program to find the sum of first
// n natural numbers that avoid overflow if
// result is going to be within limits.
using System;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
if (n % 2 == 0)
return (n / 2) * (n + 1);
// If n is odd, (n+1) must be even
else
return ((n + 1) / 2) * n;
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
15
时间复杂度: O(n)
辅助空间: O(1)
一个有效的解决方案是使用以下公式。
这是如何运作的?
We can prove this formula using induction.
It is true for n = 1 and n = 2
For n = 1, sum = 1 * (1 + 1)/2 = 1
For n = 2, sum = 2 * (2 + 1)/2 = 3
Let it be true for k = n-1.
Sum of k numbers = (k * (k+1))/2
Putting k = n-1, we get
Sum of k numbers = ((n-1) * (n-1+1))/2
= (n - 1) * n / 2
If we add n, we get,
Sum of n numbers = n + (n - 1) * n / 2
= (2n + n2 - n)/2
= n * (n + 1)/2
C++
// Efficient CPP program to find sum of first
// n natural numbers.
#include
using namespace std;
// Returns sum of first n natural
// numbers
int findSum(int n)
{
return n * (n + 1) / 2;
}
// Driver code
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
Java
// Efficient JAVA program to find sum
// of first n natural numbers.
import java.io.*;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
return n * (n + 1) / 2;
}
// Driver code
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
// This code is contributed by Nikita Tiwari.
Python
# Efficient CPP program to find sum
# of first n natural numbers.
# Returns sum of first n natural
# numbers
def findSum(n) :
return n * (n + 1) / 2
# Driver code
n = 5
print findSum(n)
# This code is contributed by Nikita Tiwari.
C#
// Efficient C# program to find sum
// of first n natural numbers.
using System;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
return n * (n + 1) / 2;
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
// This code is contributed by vt_m.
的PHP
Java脚本
输出:
15
时间复杂度: O(1)
辅助空间: O(1)
即使结果未超出整数限制,上述程序也会导致溢出。通过先进行除法,可以在某种程度上避免溢出。
C++
// Efficient CPP program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
#include
using namespace std;
// Returns sum of first n natural
// numbers
int findSum(int n)
{
if (n % 2 == 0)
return (n/2) * (n+1);
// If n is odd, (n+1) must be even
else
return ((n + 1) / 2) * n;
}
// Driver code
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
Java
// Efficient JAVA program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
import java.io.*;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
if (n % 2 == 0)
return (n / 2) * (n + 1);
// If n is odd, (n+1) must be even
else
return ((n + 1) / 2) * n;
}
// Driver code
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
//This code is contributed by Nikita Tiwari.
Python
# Efficient Python program to find the sum
# of first n natural numbers that avoid
# overflow if the result is going to be
# within limits.
# Returns sum of first n natural
# numbers
def findSum(n) :
if (n % 2 == 0) :
return (n / 2) * (n + 1)
# If n is odd, (n+1) must be even
else :
return ((n + 1) / 2) * n
# Driver code
n = 5
print findSum(n)
# This code is contributed by Nikita Tiwari.
C#
// Efficient C# program to find the sum of first
// n natural numbers that avoid overflow if
// result is going to be within limits.
using System;
class GFG{
// Returns sum of first n natural
// numbers
static int findSum(int n)
{
if (n % 2 == 0)
return (n / 2) * (n + 1);
// If n is odd, (n+1) must be even
else
return ((n + 1) / 2) * n;
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
// This code is contributed by vt_m.
的PHP
Java脚本
输出:
15
时间复杂度: O(1)