📜  自然数

📅  最后修改于: 2021-05-04 14:26:46             🧑  作者: Mango

对数字进行计数,例如1、2、3、4、5、6…基本上,所有大于0的整数都是自然数。

关于自然数的事实

  1. 它们是整数(称为整数),并且永远不小于零(即正数)
  2. 下一个可能的自然数可以通过在当前自然数上加1来找到
  3. 自然数是我们用来计数的普通数,1、2、3等。
  4. 数字零有时被认为是自然数,但并非总是如此,因为没有人以0、0、1、2、3开头。
  5. 所有其他带有质数的自然数的GCD始终为1。
  6. 可以通过将自然数与集合相关联来正式定义自然数。那么,零是空集中的元素数;否则,为零。 1是集合中包含一个自然数的元素数;等等。

如何打印n个自然数的和?

使用递归

给定一个数n ,求出前n个自然数之和。要计算总和,我们将使用递归函数recur_sum()。
例子 :

Input : 3
Output : 6
Explanation : 1 + 2 + 3 = 6

Input : 5
Output : 15
Explanation : 1 + 2 + 3 + 4 + 5 = 15
C++
// C++ program to find the
// sum of natural numbers up
// to n using recursion
#include 
using namespace std;
 
// Returns sum of first
// n natural numbers
int recurSum(int n)
{
    if (n <= 1)
        return n;
    return n + recurSum(n - 1);
}
 
// Driver code
int main()
{
    int n = 5;
    cout << recurSum(n);
    return 0;
}


Java
// Java program to find the
// sum of natural numbers up
// to n using recursion
import java.util.*;
import java.lang.*;
 
class GFG
{
 
    // Returns sum of first
    // n natural numbers
    public static int recurSum(int n)
    {
        if (n <= 1)
            return n;
        return n + recurSum(n - 1);
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(recurSum(n));
    }
}


Python
# Python code to find sum
# of natural numbers upto
# n using recursion
 
# Returns sum of first
# n natural numbers
def recurSum(n):
    if n <= 1:
        return n
    return n + recurSum(n - 1)
 
# Driver code
n = 5
print(recurSum(n))


C#
// C# program to find the
// sum of natural numbers
// up to n using recursion
using System;
 
class GFG
{
 
    // Returns sum of first
    // n natural numbers
    public static int recurSum(int n)
    {
        if (n <= 1)
            return n;
        return n + recurSum(n - 1);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 5;
        Console.WriteLine(recurSum(n));
    }
}


PHP


Javascript


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));
    }
}


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)


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));
    }
}


PHP


Javascript


输出 :

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));
    }
}

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)

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));
    }
}

的PHP


Java脚本


输出 :

15

使用n项之和公式

用于查找n个自然数之和的公式由n *(n + 1)/ 2给出,这意味着如果使用该公式,程序返回的输出将比迭代循环或递归的返回速度更快。时间复杂度为O(1)。
推荐连结
查找n个自然数之和的程序
与自然数有关的更多问题:

  • 计算所有排列均大于该数字的自然数
  • 前n个自然数的平方和
  • 偶数和奇数自然数的多维数据集的总和
  • 前n个自然数的LCM
  • 前n个自然数的平方和