考虑x ^ i + y ^ i = z ^ i,x< = y< = z< = m且2< = i< = n(m和n是输入)m可以在5到100之间变化n可以从2变化到100

时间:2015-10-12 17:33:38

标签: python c optimization

考虑x ^ i + y ^ i = z ^ i,x< = y< = z< = m且2< = i< = n(m和n是输入)m可以在5到100 n之间变化从2到100

这是c程序

int main()
{
  int m, n;
  long long int x, y, z, j;
  long long int xe, ye, ze, se;
  long long int sum = 0;
  scanf("%d", &m);
  scanf("%d", &n);

  for (j = 2; j <= n; j++)
  {
    for (x = 0; x <= m; x++)
    {
      for (y = x; y <= m; y++)
      {
        for (z = y; z <= m; z++)
        {
          xe = pow(x, j);
          ye = pow(y, j);
          ze = pow(z, j);
          se = (xe + ye);
          if (ze == se)
          {
            printf("\n i = %lld", j);
            sum++;
          }
        }
      }
    }
  }
  printf("sum= %lld ", sum);
  return 0;
}

因为它不适用于大值(我不想使用大整数:P) 我写了一个相当于它的python ---

import math
m=0 
n=0 
x=y=z=j=0
xe=ye=ze=se=0
sum=0;  
m = int(input("Enter a number: "))
n = int(input("Enter a number: "))   

for j in range (2, n):

  for x in range (0, m):

    for y in range (x, m):

        for z in range (y, m):
            xe=math.pow(x,j)
            ye=math.pow(y,j)
            ze=math.pow(z,j)
            se=(xe+ye)
            if (ze == se): 
                sum=sum+1
print "sum=", sum  

但它也无效。有谁能说出原因?

2 个答案:

答案 0 :(得分:2)

有一个流行的数学定理 - Fermat's Great (Last) Theorem - 表示任何整数n > 2 不存在非零整数a,{{1 } {},bc

答案 1 :(得分:0)

您刚刚遇到语法错误。以下是为我工作的内容:

import math
m=0 
n=0 
x=y=z=j=0
xe=ye=ze=se=0
sum=0;  
m = int(input("Enter a number: "))
n = int(input("Enter a number: "))   

for j in range (2, n):

  for x in range (0, m):

    for y in range (x, m):

        for z in range (y, m):
            xe=x**j
            ye=y**j
            ze=z**j
            se=(xe+ye)
            if (ze == se): 
                sum=sum+1
print ("sum = "+str(sum))

我正在使用输入m = 100 n = 10进行测试,结果我得到850

但是,如果您正试图测试费马的最后定理:

import math
m=0 
n=0 
x=y=z=j=0
xe=ye=ze=se=0
sum=0;  
m = int(input("Enter a number: "))
n = int(input("Enter a number: "))   

for j in range (3, n):

  for x in range (1, m):

    for y in range (x, m):

        for z in range (y, m):
            xe=x**j
            ye=y**j
            ze=z**j
            se=(xe+ye)
            if (ze == se): 
                sum=sum+1
print ("sum = "+str(sum))

使用^。