c - C 程序在查找整数三元组 (x,y,z) 时出错,使得 n^x + n^y = n^z 对于给定的 n 范围

标签 c algorithm numbers number-theory

我想制作一个与 DEV-C++ 4.9.9.2 兼容的 C 程序来查找整数三元组 (x,y,z) 这样对于任何整数 n 方程 n^x + n^y = n^z 成立,其中 n[a,b] 范围内的任何整数。 c 程序将只有 ab 的输入并找到这样可能的三元组。

我写的代码不工作。它有什么错误?

for (n = a ; n <= b ; n++) {
    for (x = a ; x < b ; x++) {
        for (y = a ; y < b ; y++) {
            for (z = a ; z = b ; z++) {
                c = pow(n, x); 
                d = pow(n, y);
                e = pow(n, z);
                f = c + d;
                if (e = f) {
                    printf("(%d , %d , %d) : %d", x,y,z,n);
                }
            }
        }
    }
}

我是 C 的新手。

最佳答案

C修正

尝试改变

if (e=f)

进入

if (e==f)

第一个做赋值,第二个测试相等性。

(请注意,如果测试的数字大于您的数据类型,您也可能会溢出。)

数学方法

如果 y==x,则:

n^x + n^x = n^z
2n^x =  n^z
=> n == 0 or n == 2 

现在,假设 y>x 和 n!=0。

 n^x + n^y = n^z
 n^x ( 1 + n^(y-x)) = n^z
 => 1+n^(y-x) = n^(z-x)
 => 1 = 0 ( modulo n)
 => impossible unless n==0 (in which case any x,y works) or n==1 (which does not work)

所以如果 n==0,这个方程对任何 x,y 都有解。 否则,唯一的解决方案是 n==2、x==y 和 z=x+1。

关于c - C 程序在查找整数三元组 (x,y,z) 时出错,使得 n^x + n^y = n^z 对于给定的 n 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088776/

相关文章:

javascript - 找到最近的下一个偶数 100

javascript - 如何在 JavaScript 中从字符串中提取数字?

c - Kernighan 和 Ritchie 的 "C Programming Language"第二版的良好后续

python - Linux:如何模拟接口(interface)上的传入数据包?

c - 列出链表

algorithm - 整数时间复杂度的位计数算法 (Brian Kernighan)

algorithm - 确定掷骰子中数字出现的频率

c - 生成随机数

c++ - 更改浮点舍入模式

algorithm - 最大二分匹配