题目9:找出唯一的满足a + b + c = 1000的毕达哥拉斯三元组{a, b, c}
本帖最后由 不二如是 于 2017-6-11 06:24 编辑Special Pythagorean triplet
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
题目:
一个毕达哥拉斯三元组是一个包含三个自然数的集合,a < b < c,满足条件:
a2 + b2 = c2
例如:32 + 42 = 9 + 16 = 25 = 52。
已知存在并且只存在一个毕达哥拉斯三元组满足条件 a + b + c = 1000。
找出该三元组中 abc 的乘积。
本帖最后由 s1986q 于 2015-4-21 11:11 编辑
答案是:200 ,375, 425。 本帖最后由 翅膀团 于 2015-11-16 14:11 编辑
#include <stdio.h>
int a=0,b=1,c=2;
int main(void)
{
void comparison();
while(!((a*a+b*b==c*c) && (a+b+c==1000)))
{
comparison();
}
printf("a=%d,b=%d,c=%d\n",a,b,c);
}
void comparison()
{
if( b-a == 1)
{
if( c-b == 1)
{
c++;
a=0,b=1;
return;
}
else
{
b++;
a=0;
return;
}
}
else
{
a++;
return;
}
}
如果有错误希望指出 #include<stdio.h>
#include<math.h>
void main()
{
int a, b, c,t;
for(a = 3;a<500;a++) //a = 3 起递增
{
for(b = a+1;b<500;b++) // b = a + 1 起递增 应为 b > a
{
t = a*a+b*b; //t = a 的平方 + b 的平方
c = sqrt(t);// c = t 的平方根
if(c*c==t) //如果 c 的平方 = t 说明 a^2+b^2=c^2成立
{
if(a + b + c == 1000) //判断三个数的和是否为1000
{
printf("%d^2 + %d^2 = %d^2 %d * %d * %d = %d\n\n",a, b, c, a, b, c, a*b*c);
goto s;
}
}
}
}
s:printf("\n");
} (200, 375, 425)-->31875000 #python 2.7.11
for a in range(1000):
for b in range(a,1000):
c = 1000 - a- b
if a*a+b*b == c*c and c>b:
print a*b*c
本帖最后由 飘飞的白杨 于 2016-6-15 12:03 编辑
31875000 for a in range(1,1000):
for b in range(a,1000):
if a**2+b**2==(1000-a-b)**2:
print(a,b,1000-a-b)
break
200 375 425 def Pythagorean():
for a in range(1,999):
for b in range(a,999):
for c in range(b,999):
if a+b+c==1000:
if (pow(a, 2) + pow(b, 2) == pow(c, 2)):
print(a,b,c)
return 0
Pythagorean()
#include<stdio.h>
#include<math.h>
int a,b,c;
#define NUM 1000
int main(int argc,char *argv[])//
{
for(a=333;a<NUM;a++)
for(b=1;b<a;b++)
{
c=NUM-a-b;
if(judeg()==1)
{
printf("the answer was a=%d,b=%d,c=%d\n",a,b,c);
goto loop1;
}
}
loop1:
printf("the end!!!");
}
int judeg(void)
{
if(a*a==b*b+c*c)
return 1;
else return 0;
} for a in range(1,333):
for b in range(max(a,500-a),500):
if a**2+b**2 == (1000-a-b)**2:
print(a,b,1000-a-b)
def euler(x):
for i in range(1,1000):
for j in range(i,1000):
if i + j + (i**2+j**2)**0.5 == x:
return i*j*((i**2+j**2)**0.5)
if __name__ == '__main__':
print(euler(1000)) for a in range(500):
for b in range(a,500):
for c in range(b,500):
if (a*a + b*b == c*c)&(a+b+c == 1000):
print(a,b,c,a*b*c)
"""
一个毕达哥达斯三元组是一个包含三个自然数的集合
a<b<c,且满足条件:
a*a+b*b=c*c
如3*3+4*4=5*5
已知存在并且只存在一个毕达哥拉斯三元组满足条件a+b+c=1000
找出该三元组中abc的乘积
"""
import time
start=time.clock()
#先定义一个判断是否为毕达哥拉斯三元组的函数
def isPytha(a,b,c):
if (a+b)<c:
return False
elif a*a+b*b==c*c:
return True
else:
return False
def findS(num):
for a in range(1,num//3):
for b in range(a+1,num-1):
for c in range(b+1,num):
if isPytha(a,b,c) and a+b+c==num:
print("满足条件的三元组三个数字分别为:")
print("a="+str(a))
print("b="+str(b))
print("c="+str(c))
print("三数乘积为:"+str(a*b*c))
findS(1000)
end=time.clock()
print("运行耗时:"+str(end-start)+"秒")
效率比较低。。。 # Python 3.5实现毕达哥拉斯三元组
# a < b < c
# a + b + c = 1000且a^2 + b^2 = c^2
def pythagoreanTriplet(n):
upLimit = n //2 # n
for a in range(1, upLimit):
for b in range(a + 1, upLimit):
c = n - (a + b)
if c > b:
if a**2 + b**2 == c**2:
return (a, b, c)
n = 1000
PT = pythagoreanTriplet(n)
print('满足a + b + c = %d的毕达哥拉斯三元组为a = %d, b = %d, c = %d'\
%(n, PT,PT,PT))
product = PT * PT * PT
print('该三元组中abc的乘积为%d'%product)
>>>
满足a + b + c = 1000的毕达哥拉斯三元组为a = 200, b = 375, c = 425
该三元组中abc的乘积为31875000 本帖最后由 lyciam 于 2016-11-18 10:09 编辑
print({a*b*(1000-a-b): for a in range(1,1000) for b in range(a,1000) if a**2+b**2 == (1000-a-b)**2})
{31875000: }
time:0.4845299631912195 # encoding:utf-8
from time import time
def euler008():
for z in range(334, 1000):
for y in range(2, z):
for x in range(1, y):
if (x + y + z == 1000) and (x ** 2 + y ** 2 == z ** 2):
print ('x = %d, y = %d, z = %d, x * y * z = %d' % (x, y, z, x * y * z))
return
start = time()
euler008()
print('cost %.3f sec' % (time() - start))
此代码使用matlab编程
Problem9所用时间为0.0090387秒
Problem9的答案为31875000
%题目9:找出唯一的满足a + b + c = 1000的毕达哥拉斯三元组{a, b, c}
function Output=Problem9(Input)
tic
if nargin==0
Input=1000;
end
Flag=0;
for ii=Input-2:-1:1
C=ii;
for jj=Input-C-1:-1:1
B=jj;
A=Input-C-B;
if C^2==A^2+B^2;
Output=A*B*C;
Flag=1;
break
end
end
if Flag==1
break
end
end
disp('此代码使用matlab编程')
disp(['Problem9所用时间为',num2str(toc),'秒'])
disp(['Problem9的答案为',num2str(Output)])
end
'''
思路:
根据a、b、c的关系,可推出a、b、c为直角三角形的三条边,其中c为斜边。
那么有c < a + b,a < 1000 // 3,b < 500
'''
import time
def find_product(sum_abc=1000):
'找出唯一的满足a + b + c = 1000的毕达哥拉斯三元组{a, b, c}'
list_abc = []
for a in range(1, 1000 // 3):
for b in range(a, 500):
if a ** 2 + b ** 2 == (sum_abc - a - b) ** 2:
list_abc.append(a)
list_abc.append(b)
list_abc.append(sum_abc - a - b)
return list_abc
start = time.clock()
product = find_product()
print('a、b、c为', product)
print('a、b、c的乘积为%d' %(product * product * product))
end = time.clock()
print('程序执行了%fs。' %(end - start))
执行结果:
a、b、c为
a、b、c的乘积为31875000
程序执行了0.147941s。 借鉴了一下楼上的思路,用c写的
/*题目:找出唯一的满足a + b + c = 1000的毕达哥拉斯三元组{a, b, c} 并输出其积*/
#include <stdio.h>
int main()
{
int a, b, c;
for(a=1; a<=333; a++)
{
for(b=1; b<=500; b++)
{
c = 1000 - a - b;
if(a*a + b*b == c*c)
{
goto z;
}
}
}
z: printf("a = %d, b = %d, c = %d, a * b * c = %d\n", a, b, c, a*b*c);
return 0;
}
a = 200, b = 375, c = 425, a * b * c = 31875000
Process returned 0 (0x0) execution time : 0.019 s