📜  三角数

📅  最后修改于: 2021-04-29 18:45:11             🧑  作者: Mango

如果我们可以用点的三角形网格的形式表示数字,则该数字称为三角数,以使这些点形成等边三角形,并且每一行包含与行号一样多的点,即第一行有一个点,第二行有一个点行有两个点,第三行有三个点,依此类推。起始三角数为1、3(1 + 2),6(1 + 2 + 3),10(1 + 2 + 3 + 4)。

三角形的

如何检查数字是否为三角形?
这个想法基于这样一个事实,即第n个三角数可以写为n个自然数的总和,即n *(n + 1)/ 2。原因很简单,三角形网格的基线有n个点,基线上方的线有(n-1)个点,依此类推。方法1(简单)
我们从1开始,检查数字是否等于1。如果不是,则将2加到3,然后重新检查数字。我们重复此过程,直到总和小于或等于要检查的三角形数为止。
以下是检查数字是否为三角数的实现。

C++
// C++ program to check if a number is a triangular number
// using simple approach.
#include 
using namespace std;
 
// Returns true if 'num' is triangular, else false
bool isTriangular(int num)
{
    // Base case
    if (num < 0)
        return false;
 
    // A Triangular number must be sum of first n
    // natural numbers
    int sum = 0;
    for (int n=1; sum<=num; n++)
    {
        sum = sum + n;
        if (sum==num)
            return true;
    }
 
    return false;
}
 
// Driver code
int main()
{
    int n = 55;
    if (isTriangular(n))
        cout << "The number is a triangular number";
    else
        cout << "The number is NOT a triangular number";
 
    return 0;
}


Java
// Java program to check if a
// number is a triangular number
// using simple approach
class GFG
{
     
    // Returns true if 'num' is
    // triangular, else false
    static boolean isTriangular(int num)
    {
        // Base case
        if (num < 0)
            return false;
     
        // A Triangular number must be
        // sum of first n natural numbers
        int sum = 0;
         
        for (int n = 1; sum <= num; n++)
        {
            sum = sum + n;
            if (sum == num)
                return true;
        }
     
        return false;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 55;
        if (isTriangular(n))
            System.out.print("The number "
                + "is a triangular number");
        else
            System.out.print("The number"
             + " is NOT a triangular number");
    }
}
 
// This code is contributed
// by Anant Agarwal.


Python3
# Python3 program to check if a number is a
# triangular number using simple approach.
 
# Returns True if 'num' is triangular, else False
def isTriangular(num):
 
    # Base case
    if (num < 0):
        return False
 
    # A Triangular number must be
    # sum of first n natural numbers
    sum, n = 0, 1
 
    while(sum <= num):
     
        sum = sum + n
        if (sum == num):
            return True
        n += 1
 
    return False
 
# Driver code
n = 55
if (isTriangular(n)):
    print("The number is a triangular number")
else:
    print("The number is NOT a triangular number")
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# program to check if a number is a
// triangular number using simple approach
using System;
 
class GFG {
     
    // Returns true if 'num' is
    // triangular, else false
    static bool isTriangular(int num)
    {
        // Base case
        if (num < 0)
            return false;
     
        // A Triangular number must be
        // sum of first n natural numbers
        int sum = 0;
         
        for (int n = 1; sum <= num; n++)
        {
            sum = sum + n;
            if (sum == num)
                return true;
        }
     
        return false;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 55;
         
        if (isTriangular(n))
            Console.WriteLine("The number "
                + "is a triangular number");
        else
            Console.WriteLine("The number"
            + " is NOT a triangular number");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program to check if a number is a triangular number
// using quadratic equation.
#include 
using namespace std;
 
// Returns true if num is triangular
bool isTriangular(int num)
{
    if (num < 0)
        return false;
 
    // Considering the equation n*(n+1)/2 = num
    // The equation is  : a(n^2) + bn + c = 0";
    int c = (-2 * num);
    int b = 1, a = 1;
    int d = (b * b) - (4 * a * c);
 
    if (d < 0)
        return false;
 
    // Find roots of equation
    float root1 = ( -b + sqrt(d)) / (2 * a);
    float root2 = ( -b - sqrt(d)) / (2 * a);
 
    // checking if root1 is natural
    if (root1 > 0 && floor(root1) == root1)
        return true;
 
    // checking if root2 is natural
    if (root2 > 0 && floor(root2) == root2)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int num = 55;
    if (isTriangular(num))
        cout << "The number is a triangular number";
    else
        cout << "The number is NOT a triangular number";
 
    return 0;
}


Java
// Java program to check if a number is a
// triangular number using quadratic equation.
import java.io.*;
 
class GFG {
 
    // Returns true if num is triangular
    static boolean isTriangular(int num)
    {
        if (num < 0)
            return false;
     
        // Considering the equation
        // n*(n+1)/2 = num
        // The equation is :
        // a(n^2) + bn + c = 0";
        int c = (-2 * num);
        int b = 1, a = 1;
        int d = (b * b) - (4 * a * c);
     
        if (d < 0)
            return false;
     
        // Find roots of equation
        float root1 = ( -b +
           (float)Math.sqrt(d)) / (2 * a);
            
        float root2 = ( -b -
           (float)Math.sqrt(d)) / (2 * a);
     
        // checking if root1 is natural
        if (root1 > 0 && Math.floor(root1)
                                  == root1)
            return true;
     
        // checking if root2 is natural
        if (root2 > 0 && Math.floor(root2)
                                  == root2)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void main (String[] args) {
        int num = 55;
        if (isTriangular(num))
            System.out.println("The number is"
                    + " a triangular number");
        else
            System.out.println ("The number "
              + "is NOT a triangular number");
    }
}
 
//This code is contributed by vt_m.


Python3
# Python3 program to check if a number is a
# triangular number using quadratic equation.
import math
 
# Returns True if num is triangular
def isTriangular(num):
 
    if (num < 0):
        return False
 
    # Considering the equation n*(n+1)/2 = num
    # The equation is : a(n^2) + bn + c = 0
    c = (-2 * num)
    b, a = 1, 1
    d = (b * b) - (4 * a * c)
 
    if (d < 0):
        return False
 
    # Find roots of equation
    root1 = ( -b + math.sqrt(d)) / (2 * a)
    root2 = ( -b - math.sqrt(d)) / (2 * a)
 
    # checking if root1 is natural
    if (root1 > 0 and math.floor(root1) == root1):
        return True
 
    # checking if root2 is natural
    if (root2 > 0 and math.floor(root2) == root2):
        return True
 
    return False
 
 
# Driver code
n = 55
if (isTriangular(n)):
    print("The number is a triangular number")
else:
    print("The number is NOT a triangular number")
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to check if a number is a triangular
// number using quadratic equation.
using System;
 
class GFG {
     
    // Returns true if num is triangular
    static bool isTriangular(int num)
    {
        if (num < 0)
            return false;
     
        // Considering the equation n*(n+1)/2 = num
        // The equation is : a(n^2) + bn + c = 0";
        int c = (-2 * num);
        int b = 1, a = 1;
        int d = (b * b) - (4 * a * c);
     
        if (d < 0)
            return false;
     
        // Find roots of equation
        float root1 = ( -b + (float)Math.Sqrt(d))
                                        / (2 * a);
                                         
        float root2 = ( -b - (float)Math.Sqrt(d)) 
                                        / (2 * a);
     
        // checking if root1 is natural
        if (root1 > 0 && Math.Floor(root1) == root1)
            return true;
     
        // checking if root2 is natural
        if (root2 > 0 && Math.Floor(root2) == root2)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void Main () {
         
        int num = 55;
        if (isTriangular(num))
            Console.WriteLine("The number is a "
                            + "triangular number");
        else
            Console.WriteLine ("The number is NOT "
                          + "a triangular number");
    }
}
 
//This code is contributed by vt_m.


PHP
 0 && floor($root1) == $root1)
        return true;
 
    // checking if root2 is natural
    if ($root2 > 0 && floor($root2) == $root2)
        return true;
 
    return false;
}
 
// Driver code
$num = 55;
if (isTriangular($num))
    echo("The number is" .
         " a triangular number");
else
    echo ("The number " .
          "is NOT a triangular number");
 
// This code is contributed
// by Code_Mech.
?>


Javascript


输出:

The number is a triangular number 

方法2(使用二次方程式的根公式)
我们通过将数字等于第一个“ n”个自然数之和的公式来形成二次方程,如果我们得到的至少一个值“ n”是自然数,则我们说该数是一个三角数。

Let the input number be 'num'. We consider,

n*(n+1) = num

as,

 n2 + n + (-2 * num) = 0 

以下是上述想法的实现。

C++

// C++ program to check if a number is a triangular number
// using quadratic equation.
#include 
using namespace std;
 
// Returns true if num is triangular
bool isTriangular(int num)
{
    if (num < 0)
        return false;
 
    // Considering the equation n*(n+1)/2 = num
    // The equation is  : a(n^2) + bn + c = 0";
    int c = (-2 * num);
    int b = 1, a = 1;
    int d = (b * b) - (4 * a * c);
 
    if (d < 0)
        return false;
 
    // Find roots of equation
    float root1 = ( -b + sqrt(d)) / (2 * a);
    float root2 = ( -b - sqrt(d)) / (2 * a);
 
    // checking if root1 is natural
    if (root1 > 0 && floor(root1) == root1)
        return true;
 
    // checking if root2 is natural
    if (root2 > 0 && floor(root2) == root2)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int num = 55;
    if (isTriangular(num))
        cout << "The number is a triangular number";
    else
        cout << "The number is NOT a triangular number";
 
    return 0;
}

Java

// Java program to check if a number is a
// triangular number using quadratic equation.
import java.io.*;
 
class GFG {
 
    // Returns true if num is triangular
    static boolean isTriangular(int num)
    {
        if (num < 0)
            return false;
     
        // Considering the equation
        // n*(n+1)/2 = num
        // The equation is :
        // a(n^2) + bn + c = 0";
        int c = (-2 * num);
        int b = 1, a = 1;
        int d = (b * b) - (4 * a * c);
     
        if (d < 0)
            return false;
     
        // Find roots of equation
        float root1 = ( -b +
           (float)Math.sqrt(d)) / (2 * a);
            
        float root2 = ( -b -
           (float)Math.sqrt(d)) / (2 * a);
     
        // checking if root1 is natural
        if (root1 > 0 && Math.floor(root1)
                                  == root1)
            return true;
     
        // checking if root2 is natural
        if (root2 > 0 && Math.floor(root2)
                                  == root2)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void main (String[] args) {
        int num = 55;
        if (isTriangular(num))
            System.out.println("The number is"
                    + " a triangular number");
        else
            System.out.println ("The number "
              + "is NOT a triangular number");
    }
}
 
//This code is contributed by vt_m.

Python3

# Python3 program to check if a number is a
# triangular number using quadratic equation.
import math
 
# Returns True if num is triangular
def isTriangular(num):
 
    if (num < 0):
        return False
 
    # Considering the equation n*(n+1)/2 = num
    # The equation is : a(n^2) + bn + c = 0
    c = (-2 * num)
    b, a = 1, 1
    d = (b * b) - (4 * a * c)
 
    if (d < 0):
        return False
 
    # Find roots of equation
    root1 = ( -b + math.sqrt(d)) / (2 * a)
    root2 = ( -b - math.sqrt(d)) / (2 * a)
 
    # checking if root1 is natural
    if (root1 > 0 and math.floor(root1) == root1):
        return True
 
    # checking if root2 is natural
    if (root2 > 0 and math.floor(root2) == root2):
        return True
 
    return False
 
 
# Driver code
n = 55
if (isTriangular(n)):
    print("The number is a triangular number")
else:
    print("The number is NOT a triangular number")
 
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to check if a number is a triangular
// number using quadratic equation.
using System;
 
class GFG {
     
    // Returns true if num is triangular
    static bool isTriangular(int num)
    {
        if (num < 0)
            return false;
     
        // Considering the equation n*(n+1)/2 = num
        // The equation is : a(n^2) + bn + c = 0";
        int c = (-2 * num);
        int b = 1, a = 1;
        int d = (b * b) - (4 * a * c);
     
        if (d < 0)
            return false;
     
        // Find roots of equation
        float root1 = ( -b + (float)Math.Sqrt(d))
                                        / (2 * a);
                                         
        float root2 = ( -b - (float)Math.Sqrt(d)) 
                                        / (2 * a);
     
        // checking if root1 is natural
        if (root1 > 0 && Math.Floor(root1) == root1)
            return true;
     
        // checking if root2 is natural
        if (root2 > 0 && Math.Floor(root2) == root2)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void Main () {
         
        int num = 55;
        if (isTriangular(num))
            Console.WriteLine("The number is a "
                            + "triangular number");
        else
            Console.WriteLine ("The number is NOT "
                          + "a triangular number");
    }
}
 
//This code is contributed by vt_m.

的PHP

 0 && floor($root1) == $root1)
        return true;
 
    // checking if root2 is natural
    if ($root2 > 0 && floor($root2) == $root2)
        return true;
 
    return false;
}
 
// Driver code
$num = 55;
if (isTriangular($num))
    echo("The number is" .
         " a triangular number");
else
    echo ("The number " .
          "is NOT a triangular number");
 
// This code is contributed
// by Code_Mech.
?>

Java脚本


输出:

The number is a triangular number