算法来确定一个给定的数目N是否可以成为直角三角形的与所有3积分两侧斜边斜边、角形、直角、算法

由网友(暴富少女)分享简介:假设你将得到一个直角三角形斜边,那么你怎么能确定是否有两个整体较小双方可能与给定的斜边。Suppose you are given hypotenuse of a right angled triangle,then how can you determine whether there are two integr...

假设你将得到一个直角三角形斜边,那么你怎么能确定是否有两个整体较小双方可能与给定的斜边。

Suppose you are given hypotenuse of a right angled triangle,then how can you determine whether there are two integral smaller sides possible with the given hypotenuse.

例如,您将得到斜边为5.Then你要确定你是否有较小的整体侧面赋予的权利triangle.The答案将是,因为我们可以具有较小的侧面3和4,从而得到一个3-4-5直角三角形

For example, you are given hypotenuse as 5.Then you have to determine whether you have smaller integral sides for the given right triangle.The answer will be yes because we can have smaller sides as 3 and 4,hence getting a 3-4-5 right triangle.

同样,对于斜边为7,我们可以没有这样的直角三角形。

Similarly,for hypotenuse as 7 we can have no such right triangle.

换句话说,我们必须找到一个给定数n是否可以作为斜边的直角三角形的所有三面为整数。

我通过整个文章接着毕达哥拉斯三元组但仍然没有success.I很困惑什么条件check.P​​lease帮助。

I went through entire article on Pythagorean triples but still no success.I am confused what conditions to check.Please help.

推荐答案

您有一个的原始的毕达哥拉斯三:

You have for a primitive pythagorean triple:

(p^2 - q^2)^2 + (2 * p * q))^2 = (p^2 + q^2)^2 = N^2

假设p> = Q。然后我们有

Suppose p >= q. Then we have

N >= 2 * q^2 or q <= sqrt(N / 2)

设N = 13,那么我们需要的Q&LT; =开方(13/2)= 2.54

Suppose N = 13. Then we need q <= sqrt(13 / 2) = 2.54

Q = 2 =>点^ 2 = 13 - 4 = 9,这是一个正方形

q = 2 => p^2 = 13 - 4 = 9, which is a square.

因此​​,你可以有数字的小环的i从1..sqrt(N / 2),并检查是否N - (I ^ 2)是一个方形

Hence you can have a small loop of numbers 'i' from 1..sqrt(N/2) and check if N - (i^2) is a square.

这将是 O(开方(N))作为一个成员的原始的毕达哥拉斯元组。

This will be O(sqrt(N)) for a member of a primitive pythagorean tuple.

样品code在C / C ++:

Sample code in C/C++:

#include <stdio.h>
#include <math.h>

void CheckTuple(int n)
{
    int k = sqrt(n/2.0);

    for (int i = 1; i <= k; i++) {
        int tmp = sqrt((double)(n - i * i));
        if ((tmp * tmp) == (n - i * i)) {
            printf("%d^2 + %d^2 = %d^2n", (tmp*tmp - i*i), 2 * tmp * i, n);
            return;
        }
    }
    printf("%d is not part of a tuplen", n);
    return;
}


int main(int argc, char *argv[])
{

    CheckTuple(5);
    CheckTuple(6);
    CheckTuple(13);
    CheckTuple(10);
    CheckTuple(25);

    return 0;
}

输出:

3^2 + 4^2 = 5^2
6 is not part of a tuple
5^2 + 12^2 = 13^2
8^2 + 6^2 = 10^2
7^2 + 24^2 = 25^2
阅读全文

相关推荐

最新文章