📌  相关文章
📜  检查一个数是否可以表示为 2 个三角形数的和

📅  最后修改于: 2021-10-27 07:51:15             🧑  作者: Mango

给定一个整数N ,任务是找出它是否可以写成 2 个三角形数的和(可能不同,也可能不同)。
例子:

Input: N = 24
Output: YES
24 can be represented as 3+21.

Input: N = 15
Output: NO

方法:
考虑所有小于N 的三角形数,即 sqrt(N) 数。让我们将它们添加到一个集合中,对于每个三角形数X,我们检查集合中是否存在NX 。如果任何三角数都为真,则答案为“是”,否则答案为“否”。
下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to check if it is possible or not
bool checkTriangularSumRepresentation(int n)
{
    unordered_set tri;
    int i = 1;
 
    // Store all triangular numbers up to N in a Set
    while (1) {
        int x = i * (i + 1) / 2;
        if (x >= n)
            break;
        tri.insert(x);
        i++;
    }
  
    // Check if a pair exists
    for (auto tm : tri)
        if (tri.find(n - tm) != tri.end())
            return true;
    return false;
}
 
// Driver Code
int main()
{
    int n = 24;
    checkTriangularSumRepresentation(n) ? cout << "Yes"
                                        : cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function to check if it is possible or not
    static boolean checkTriangularSumRepresentation(int n)
    {
        HashSet tri = new HashSet<>();
        int i = 1;
 
        // Store all triangular numbers up to N in a Set
        while (true)
        {
            int x = i * (i + 1) / 2;
            if (x >= n)
            {
                break;
            }
            tri.add(x);
            i++;
        }
 
        // Check if a pair exists
        for (Integer tm : tri)
        {
            if (tri.contains(n - tm) && (n - tm) !=
                (int) tri.toArray()[tri.size() - 1])
            {
                return true;
            }
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 24;
        if (checkTriangularSumRepresentation(n))
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
    }
}
 
// This code has been contributed by 29AjayKumar


Python 3
# Python3 implementation of the above approach
 
# Function to check if it is possible or not
def checkTriangularSumRepresentation(n) :
     
    tri = list();
    i = 1;
 
    # Store all triangular numbers
    # up to N in a Set
    while (1) :
        x = i * (i + 1) // 2;
        if (x >= n) :
            break;
             
        tri.append(x);
        i += 1;
 
    # Check if a pair exists
    for tm in tri :
        if n - tm in tri:
            return True;
             
    return False;
 
# Driver Code
if __name__ == "__main__" :
    n = 24;
     
    if checkTriangularSumRepresentation(n) :
        print("Yes")
    else :
        print("No")
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to check if it is possible or not
    static bool checkTriangularSumRepresentation(int n)
    {
        HashSet tri = new HashSet();
        int i = 1;
 
        // Store all triangular numbers up to N in a Set
        while (true)
        {
            int x = i * (i + 1) / 2;
            if (x >= n)
            {
                break;
            }
            tri.Add(x);
            i++;
        }
 
        // Check if a pair exists
        foreach (int tm in tri)
        {
            if (tri.Contains(n - tm))
            {
                return true;
            }
        }
        return false;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 24;
        if (checkTriangularSumRepresentation(n))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
 
// This code contributed by Rajput-Ji


PHP
= $n)
            break;
             
        array_push($tri, $x);
        $i += 1;
    }
     
    // Check if a pair exists
    foreach($tri as $tm)
        if (in_array($n - $tm, $tri))
            return true;
             
    return false;
}
 
// Driver Code
$n = 24;
 
if (checkTriangularSumRepresentation($n))
    print("Yes");
else
    print("No");
 
// This code is contributed by mits
?>


Javascript


C++
// C++ implementation of the above approach
#include 
using namespace std;
#define ll long long
int main()
{
    ll n;
    cin >> n;
    ll dig = 2 * n;
    bool flag= 0;
    for (ll i = 1; i < sqrt(2 * n + 1); i++) {
        if (i * i + i > dig)
            break;
        ll rest = dig - i * i - i;
        ll left = 1;
        ll right = dig;
        while (left <= right) {
            ll mid = (left + right) / 2;
            if (mid * mid + mid > rest)
                right = mid - 1;
            else if (mid * mid + mid == rest) {
                flag = 1;
                break;
            }
            else
                left = mid + 1;
        }
        if (flag)
            break;
    }
    if (flag)
        cout<<"YES"<


输出:
Yes

时间复杂度: O(Sqrt(N))

第二种方法:很明显 N 是正数,因为三角形数是可以表示为 k*(k+1)/2 的数字,其中 k 是某个正整数,由此我们也得到每一项都将小于 N。这意味着我们采用并迭代其他术语之一。如果我们以递增的顺序迭代第一项,那么我们可以使用两个指针,这为我们提供了一个 O(Sqrt(N)) 的解决方案。

C++

// C++ implementation of the above approach
#include 
using namespace std;
#define ll long long
int main()
{
    ll n;
    cin >> n;
    ll dig = 2 * n;
    bool flag= 0;
    for (ll i = 1; i < sqrt(2 * n + 1); i++) {
        if (i * i + i > dig)
            break;
        ll rest = dig - i * i - i;
        ll left = 1;
        ll right = dig;
        while (left <= right) {
            ll mid = (left + right) / 2;
            if (mid * mid + mid > rest)
                right = mid - 1;
            else if (mid * mid + mid == rest) {
                flag = 1;
                break;
            }
            else
                left = mid + 1;
        }
        if (flag)
            break;
    }
    if (flag)
        cout<<"YES"<

输出:

YES

时间复杂度:O(Sqrt(N))